Python实现之初等函数二之反函数,


 

一般来说,设函数y=f(x)(x∈A)的值域是C,若找得到一个函数g(y)在每一处g(y)都等于x,这样的函数x= g(y)(y∈C)叫做函数y=f(x)(x∈A)的反函数,记作x=f-1(y) 。反函数x=f -1(y)的定义域、值域分别是函数y=f(x)的值域、定义域。最具有代表性的反函数就是对数函数与指数函数。

  1. #!/usr/bin/env python 
  2. # -*- coding: UTF-8 -*- 
  3. #                     _ooOoo_ 
  4. #                   o8888888o 
  5. #                    88" . "88 
  6. #                 ( | -  _  - | ) 
  7. #                     O\ = /O 
  8. #                 ____/`---'\____ 
  9. #                  .' \\| |// `. 
  10. #                 / \\|||:|||// \ 
  11. #               / _|||||-:- |||||- \ 
  12. #                | | \\\ - /// | | 
  13. #              | \_| ''\---/'' | _/ | 
  14. #               \ .-\__ `-` ___/-. / 
  15. #            ___`. .' /--.--\ `. . __ 
  16. #         ."" '< `.___\_<|>_/___.' >'"". 
  17. #       | | : `- \`.;`\  _ /`;.`/ - ` : | | 
  18. #          \ \ `-. \_ __\ /__ _/ .-` / / 
  19. #      ==`-.____`-.___\_____/___.-`____.-'== 
  20. #                     `=---=' 
  21. ''' 
  22. @Project :pythonalgorithms  
  23. @File :Inversefunction.py 
  24. @Author :不胜人生一场醉@Date :2021/7/29 23:17  
  25. ''' 
  26. import matplotlib.pyplot as plt 
  27. import numpy as np 
  28.  
  29. if __name__ == '__main__': 
  30.    inversefunction() 
  1. def inversefunction(): 
  2.    plt.figure(figsize=(5, 15)) 
  3.    ax = plt.gca()  # 通过gca:get current axis得到当前轴 
  4.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 绘图中文 
  5.    plt.rcParams['axes.unicode_minus'] = False  # 绘图负号 
  6.    x = np.linspace(-2, 2, 100) 
  7.    y1 = np.power(x, 3) 
  8.    y2 = np.power(abs(x), 1 / 3) * np.sign(x) 
  9.    y3 = x 
  10.    label = 'np.power(x,3)' 
  11.    plt.plot(x, y1, label=label) 
  12.    label = 'np.power(x,1/3)' 
  13.    plt.plot(x, y2, label=label) 
  14.    # plt.plot(y1,x,label=label) 
  15.    # np.power(x,1/3)和x,y1调换一下是等价的 
  16.    label = 'y=x' 
  17.    plt.plot(x, y3, label=label) 
  18.    # 设置图片的右边框和上边框为不显示 
  19.    ax.spines['right'].set_color('none') 
  20.    ax.spines['top'].set_color('none') 
  21.  
  22.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  23.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  24.    ax.spines['bottom'].set_position(('data', 0)) 
  25.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  26.    # ax.spines['left'].set_position(('axes', 0.5)) 
  27.    ax.spines['left'].set_position(('data', 0)) 
  28.    plt.title("反函数") 
  29.    plt.legend(loc='upper right') 
  30.    plt.show() 
  31.  
  32.    # 反函数与原函数的复合函数等于x 
  33.    plt.figure(figsize=(5, 5)) 
  34.    ax = plt.gca()  # 通过gca:get current axis得到当前轴 
  35.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 绘图中文 
  36.    plt.rcParams['axes.unicode_minus'] = False  # 绘图负号 
  37.    x = np.linspace(-2, 2, 100) 
  38.    y1 = np.power(x, 3) 
  39.    y2 = np.power(abs(y1), 1 / 3) * np.sign(y1) 
  40.    label = 'np.power(abs(np.power(x, 3)), 1 / 3) * np.sign(np.power(x, 3))' 
  41.    plt.plot(x, y2, label=label) 
  42.    ax.spines['right'].set_color('none') 
  43.    ax.spines['top'].set_color('none') 
  44.  
  45.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  46.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  47.    ax.spines['bottom'].set_position(('data', 0)) 
  48.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  49.    # ax.spines['left'].set_position(('axes', 0.5)) 
  50.    ax.spines['left'].set_position(('data', 0)) 
  51.    plt.title("反函数与原函数的复合函数") 
  52.    plt.legend(loc='upper right') 
  53.    plt.show() 

评论关闭