自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python實(shí)現(xiàn)之初等函數(shù)一

開發(fā) 后端
初等函數(shù)是由基本初等函數(shù)經(jīng)過有限次的四則運(yùn)算和復(fù)合運(yùn)算所得到的函數(shù)。基本初等函數(shù)和初等函數(shù)在其定義區(qū)間內(nèi)均為連續(xù)函數(shù)。高等數(shù)學(xué)將基本初等函數(shù)歸為五類:冪函數(shù)、指數(shù)函數(shù)、對數(shù)函數(shù)、三角函數(shù)、反三角函數(shù)。

[[413349]]

本文轉(zhuǎn)載自微信公眾號「python與大數(shù)據(jù)分析」,作者一只小小鳥鳥。轉(zhuǎn)載本文請聯(lián)系python與大數(shù)據(jù)分析公眾號。

初等函數(shù)是由基本初等函數(shù)經(jīng)過有限次的四則運(yùn)算和復(fù)合運(yùn)算所得到的函數(shù)。基本初等函數(shù)和初等函數(shù)在其定義區(qū)間內(nèi)均為連續(xù)函數(shù)。高等數(shù)學(xué)將基本初等函數(shù)歸為五類:冪函數(shù)、指數(shù)函數(shù)、對數(shù)函數(shù)、三角函數(shù)、反三角函數(shù)。

比較頭疼的是numpy中的冪函數(shù)不支持負(fù)數(shù)定義域,所以找了很多辦法來解決該問題。

主函數(shù)代碼如下:

  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 :basicfunction.py 
  24. @Author :不勝人生一場醉 
  25. @Date :2021/7/19 17:39  
  26. ''
  27. import matplotlib.pyplot as plt 
  28. import numpy as np 
  29. import math 
  30. import mpl_toolkits.axisartist as axisartist  # 導(dǎo)入坐標(biāo)軸加工模塊 
  31.  
  32.  
  33. -----------------函數(shù)------------------ 
  34. # 給定一個(gè)數(shù)集A,對A施加一個(gè)對應(yīng)的法則/映射f,記做:f(A),那么可以得到另外一個(gè)數(shù)集B,也就是可以認(rèn)為B=f(A); 
  35. # 那么這個(gè)關(guān)系就叫做函數(shù)關(guān)系式,簡稱函數(shù)。 
  36. # 三個(gè)重要的因素: 定義域A、值域B、對應(yīng)的映射法則f。 
  37.  
  38.  
  39. if __name__ == "__main__"
  40.     # 一次函數(shù) 
  41.     Linearfunction() 
  42.     # 二次函數(shù) 
  43.     Quadraticfunction() 
  44.     # 冪函數(shù) 
  45.     powerfunction() 
  46.     # 指數(shù)函數(shù) 
  47.     exponentialfunction() 
  48.     # 對數(shù)函數(shù) 
  49.     Logarithmicfunction() 

一次函數(shù)代碼如下:

  1. ---------------一次函數(shù)------------------ 
  2. # 一次函數(shù)是函數(shù)中的一種,一般形如y=ax+b(a,b是常數(shù),a≠0),其中x是自變量,y是因變量。 
  3. # 當(dāng)b=0時(shí),y=kx(k為常數(shù),k≠0),y叫做x的正比例函數(shù) 
  4. # 當(dāng)a=0時(shí),y=b,則為常函數(shù) 
  5. def Linearfunction(): 
  6.     fig = plt.figure(figsize=(10, 8)) 
  7.     plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  8.     plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  9.     x = np.linspace(-10, 10, 100) 
  10.     i = 1 
  11.     alist = [-1, 0, 1] 
  12.     blist = [-1, 0, 1] 
  13.     for a in alist: 
  14.         for b in blist: 
  15.             plt.subplot(3, 3, i) 
  16.             y = x * a + b 
  17.             label = '{}*x+{}'.format(a, b) 
  18.             plt.plot(x, y, label=label) 
  19.             plt.legend() 
  20.             i += 1 
  21.     plt.show() 
  22.  
  23.     for a in alist: 
  24.         for b in blist: 
  25.             y = x * a + b 
  26.             label = '{}*x+{}'.format(a, b) 
  27.             plt.plot(x, y, label=label) 
  28.             i += 1 
  29.     plt.title("一次函數(shù)"
  30.     plt.legend() 
  31.     plt.show() 

二次函數(shù)代碼如下:

  1. # 二次函數(shù)(quadratic function)的基本表示形式為y=ax²+bx+c(a≠0)。 
  2. # 二次函數(shù)最高次必須為二次, 二次函數(shù)的圖像是一條對稱軸與y軸平行或重合于y軸的拋物線。 
  3. # 其中a稱為二次項(xiàng)系數(shù),b為一次項(xiàng)系數(shù),c為常數(shù)項(xiàng)。x為自變量,y為因變量。等號右邊自變量的最高次數(shù)是2。 
  4. # 如果令y值等于零,則可得一個(gè)二次方程。該方程的解稱為方程的根或函數(shù)的零點(diǎn)。 
  5. # 頂點(diǎn)坐標(biāo) = (-b/2a,(4ac-b²)/4a 
  6. def Quadraticfunction(): 
  7.    fig = plt.figure(figsize=(10, 8)) 
  8.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  9.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  10.    x = np.linspace(-2, 2, 100) 
  11.  
  12.    alist = [-1, 1] 
  13.    blist = [-1, 0, 1] 
  14.    clist = [-1, 0, 1] 
  15.  
  16.    for a in alist: 
  17.       for b in blist: 
  18.          for c in clist: 
  19.             y = a * x * x + b * x + c 
  20.             label = '{}*x*x+{}*x+{}'.format(a, b, c) 
  21.             plt.plot(x, y, label=label) 
  22.    plt.title("二次函數(shù)"
  23.    plt.legend() 
  24.    plt.show() 

冪函數(shù)代碼如下:

  1. # 冪函數(shù)是基本初等函數(shù)之一。 
  2. # 一般地,y=xα(α為有理數(shù))的函數(shù),即以底數(shù)為自變量,冪為因變量,指數(shù)為常數(shù)的函數(shù)稱為冪函數(shù)。 
  3. # 例如函數(shù)y=x0 、y=x、y=x²、y=x³。 
  4. # a = 正數(shù) 
  5. # a 為>0 的自然數(shù)  x定義域(-∞,∞) 
  6. # a 為<0 的整數(shù)     X定義域(-∞,0),(0,∞) 
  7. # a >0 的分?jǐn)?shù) 
  8. # a=n/m m為奇數(shù),n為偶數(shù),x定義域(-∞,∞),y定義域[0,+∞) 
  9. # a=n/m m為奇數(shù),n為奇數(shù),x定義域(-∞,∞),y定義域(-∞,∞) 
  10. # a=n/m m為偶數(shù),n不限,x定義域[0,∞),y定義域[0,+∞) 
  11. # a <0 的分?jǐn)?shù) 
  12. # a=n/m m為奇數(shù),n為偶數(shù),x定義域(-∞,0),(0,∞),y定義域(0,+∞) 
  13. # a=n/m m為奇數(shù),n為奇數(shù),x定義域(-∞,0),(0,∞),y定義域(-∞,0),(0,∞) 
  14. # a=n/m m為偶數(shù),n不限,x定義域(0,∞),y定義域(0,+∞) 
  15. def powerfunction(): 
  16.    plt.figure(figsize=(10, 8)) 
  17.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  18.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  19.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  20.    x = np.linspace(-2, 2, 100) 
  21.    alist = [1, 2, 3, 4] 
  22.    for a in alist: 
  23.       y = np.power(x, a) 
  24.       label = 'math.pow(x,{}'.format(a) 
  25.       plt.plot(x, y, label=label) 
  26.  
  27.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  28.    ax.spines['right'].set_color('none'
  29.    ax.spines['top'].set_color('none'
  30.  
  31.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  32.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  33.    ax.spines['bottom'].set_position(('data', 0)) 
  34.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  35.    # ax.spines['left'].set_position(('axes', 0.5)) 
  36.    ax.spines['left'].set_position(('data', 0)) 
  37.    plt.title("冪指數(shù),a為正整數(shù)"
  38.    plt.legend() 
  39.    plt.show() 
  40.  
  41.    plt.figure(figsize=(10, 8)) 
  42.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  43.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  44.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  45.    x = np.append(np.linspace(-1, -0.01, 100), np.linspace(0.01, 1, 100)) 
  46.    alist = [-1, -2, -3] 
  47.    for a in alist: 
  48.       y = np.power(x, a) 
  49.       label = 'math.pow(x,{}'.format(a) 
  50.       plt.plot(x, y, label=label) 
  51.  
  52.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  53.    ax.spines['right'].set_color('none'
  54.    ax.spines['top'].set_color('none'
  55.  
  56.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  57.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  58.    ax.spines['bottom'].set_position(('data', 0)) 
  59.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  60.    # ax.spines['left'].set_position(('axes', 0.5)) 
  61.    ax.spines['left'].set_position(('data', 0)) 
  62.    plt.title("冪指數(shù),a為負(fù)整數(shù)"
  63.    plt.legend() 
  64.    plt.show() 
  65.  
  66.    # a >0 的分?jǐn)?shù) 
  67.    # a=n/m m為奇數(shù),n為奇數(shù),x定義域(-∞,∞),y定義域(-∞,∞) 4/3 
  68.    # a=n/m m為奇數(shù),n為偶數(shù),x定義域[0,+∞),y定義域[0,+∞)4/3 
  69.    # a=n/m m為偶數(shù),n不限,x定義域(-∞,∞),y定義域[0,+∞) 1/2,3/2 
  70.    plt.figure(figsize=(10, 8)) 
  71.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  72.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  73.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  74.    x = np.linspace(-2, 2, 100) 
  75.    alist = [1 / 3, 5 / 3, 7 / 3] 
  76.    for a in alist: 
  77.       # y = np.power(x, a) 
  78.       # RuntimeWarning: invalid value encountered in power 
  79.       y = np.float_power(abs(x), a) * np.sign(x) 
  80.       label = 'math.pow(x,{}'.format(a) 
  81.       plt.plot(x, y, label=label) 
  82.  
  83.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  84.    ax.spines['right'].set_color('none'
  85.    ax.spines['top'].set_color('none'
  86.  
  87.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  88.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  89.    ax.spines['bottom'].set_position(('data', 0)) 
  90.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  91.    # ax.spines['left'].set_position(('axes', 0.5)) 
  92.    ax.spines['left'].set_position(('data', 0)) 
  93.    plt.title("冪指數(shù),分子分母為奇數(shù)"
  94.    plt.legend() 
  95.    plt.show() 
  96.  
  97.    plt.figure(figsize=(10, 8)) 
  98.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  99.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  100.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  101.    x = np.linspace(0, 2, 100) 
  102.    alist = [1 / 8, 1 / 4, 1 / 2] 
  103.    for a in alist: 
  104.       y = np.power(x, a) 
  105.       # y = np.float_power(abs(x), a) * np.sign(x) 
  106.       label = 'math.pow(x,{}'.format(a) 
  107.       plt.plot(x, y, label=label) 
  108.  
  109.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  110.    ax.spines['right'].set_color('none'
  111.    ax.spines['top'].set_color('none'
  112.  
  113.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  114.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  115.    ax.spines['bottom'].set_position(('data', 0)) 
  116.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  117.    # ax.spines['left'].set_position(('axes', 0.5)) 
  118.    ax.spines['left'].set_position(('data', 0)) 
  119.    plt.title("冪指數(shù),分母為偶數(shù)"
  120.    plt.legend() 
  121.    plt.show() 
  122.  
  123.    plt.figure(figsize=(10, 8)) 
  124.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  125.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  126.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  127.    x = np.linspace(-2, 2, 100) 
  128.    alist = [2 / 3, 4 / 5, 6 / 7, 4 / 3, 8 / 5] 
  129.    for a in alist: 
  130.       y = np.power(abs(x), a) 
  131.       label = 'math.pow(x,{}'.format(a) 
  132.       plt.plot(x, y, label=label) 
  133.  
  134.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  135.    ax.spines['right'].set_color('none'
  136.    ax.spines['top'].set_color('none'
  137.  
  138.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  139.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  140.    ax.spines['bottom'].set_position(('data', 0)) 
  141.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  142.    # ax.spines['left'].set_position(('axes', 0.5)) 
  143.    ax.spines['left'].set_position(('data', 0)) 
  144.    plt.title("冪指數(shù),分子為偶數(shù)分母為奇數(shù)"
  145.    plt.legend() 
  146.    plt.show() 
  147.  
  148.  
  149. # 關(guān)于負(fù)數(shù)就不再重復(fù)敘述了 
  150. # a <0 的分?jǐn)?shù) 
  151. # a=n/m m為奇數(shù),x定義域(-∞,0),(0,∞),y定義域(0,+∞) 
  152. # a=n/m m為偶數(shù),x定義域(-∞,0),(0,∞),y定義域(-∞,0),(0,∞) 
  153. # a=n/m m為偶數(shù),n為不限,x定義域(0,∞),y定義域(0,+∞) 

指數(shù)函數(shù)代碼如下:

  1. # 指數(shù)函數(shù)是重要的基本初等函數(shù)之一。 
  2.  
  3. # 一般地,y=ax函數(shù)(a為常數(shù)且以a>0,a≠1)叫做指數(shù)函數(shù),函數(shù)的定義域是 R 。 [1] 
  4. # 注意,在指數(shù)函數(shù)的定義表達(dá)式中,在ax前的系數(shù)必須是數(shù)1,自變量x必須在指數(shù)的位置上,且不能是x的其他表達(dá)式, 
  5. # 否則,就不是指數(shù)函數(shù) 
  6. def exponentialfunction(): 
  7.    plt.figure(figsize=(10, 8)) 
  8.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  9.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  10.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  11.    # a>0 a!=1 
  12.    # 定義域?yàn)?(-∞,∞),值域?yàn)?0, +∞),都通過(0, 1)點(diǎn) 
  13.    x = np.linspace(-2, 2, 100) 
  14.    alist = [1 / 4, 1 / 3, 1 / 2, 2, 3, 4] 
  15.    for a in alist: 
  16.       y = np.power(a, x) 
  17.       label = 'math.pow(x,{}'.format(a) 
  18.       plt.plot(x, y, label=label) 
  19.  
  20.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  21.    ax.spines['right'].set_color('none'
  22.    ax.spines['top'].set_color('none'
  23.  
  24.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  25.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  26.    ax.spines['bottom'].set_position(('data', 0)) 
  27.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  28.    # ax.spines['left'].set_position(('axes', 0.5)) 
  29.    ax.spines['left'].set_position(('data', 0)) 
  30.    plt.title("指數(shù)指數(shù)"
  31.    plt.legend() 
  32.    plt.show() 

對數(shù)函數(shù)代碼如下:

  1. # 一般地,對數(shù)函數(shù)是以冪(真數(shù))為自變量,指數(shù)為因變量,底數(shù)為常量的函數(shù)。 
  2. # 對數(shù)函數(shù)是6類基本初等函數(shù)之一。其中對數(shù)的定義: 
  3. # 如果ax =N(a>0,且a≠1),那么數(shù)x叫做以a為底N的對數(shù),記作x=logaN,讀作以a為底N的對數(shù),其中a叫做對數(shù)的底數(shù),N叫做真數(shù)。 
  4. # 一般地,函數(shù)y=logaX(a>0,且a≠1)叫做對數(shù)函數(shù),也就是說以冪(真數(shù))為自變量,指數(shù)為因變量,底數(shù)為常量的函數(shù),叫對數(shù)函數(shù)。 
  5. # 其中x是自變量,函數(shù)的定義域是(0,+∞),即x>0。它實(shí)際上就是指數(shù)函數(shù)的反函數(shù),可表示為x=ay。因此指數(shù)函數(shù)里對于a的規(guī)定,同樣適用于對數(shù)函數(shù)。 
  6. def Logarithmicfunction(): 
  7.    plt.figure(figsize=(10, 8)) 
  8.    ax = plt.gca()  # 通過gca:get current axis得到當(dāng)前軸 
  9.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 繪圖中文 
  10.    plt.rcParams['axes.unicode_minus'] = False  # 繪圖負(fù)號 
  11.    # a>0 a!=1 
  12.    # 定義域?yàn)?(-∞,∞),值域?yàn)?0, +∞),都通過(0, 1)點(diǎn) 
  13.    # 當(dāng)a>1時(shí),單調(diào)遞增 
  14.    # 當(dāng)0<a<1時(shí),單調(diào)遞減 
  15.    x = np.linspace(0.0001, 2, 100) 
  16.    alist = [1 / 4, 1 / 3, 1 / 2, 2, 3, 4] 
  17.    for a in alist: 
  18.       y = np.log(x) / np.log(a) 
  19.       label = 'np.log(x) / np.log({})'.format(a) 
  20.       plt.plot(x, y, label=label) 
  21.  
  22.    # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示 
  23.    ax.spines['right'].set_color('none'
  24.    ax.spines['top'].set_color('none'
  25.  
  26.    # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置 
  27.    # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置 
  28.    ax.spines['bottom'].set_position(('data', 0)) 
  29.    # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置 
  30.    # ax.spines['left'].set_position(('axes', 0.5)) 
  31.    ax.spines['left'].set_position(('data', 0)) 
  32.    plt.title("對數(shù)指數(shù)"
  33.    plt.legend() 
  34.    plt.show() 

 

責(zé)任編輯:武曉燕 來源: python與大數(shù)據(jù)分析
相關(guān)推薦

2021-07-30 05:00:04

Python初等函數(shù)

2021-07-30 06:58:27

python實(shí)現(xiàn)三角函數(shù)

2021-08-11 07:02:04

Python激活函數(shù)

2016-11-14 15:16:42

Android

2020-06-05 14:29:07

PythonPandas數(shù)據(jù)分析

2021-08-31 20:21:11

VitessMySQL分庫

2021-03-31 07:39:18

pythonHIVEUDF函數(shù)

2018-04-08 07:49:05

應(yīng)用集成EAI應(yīng)用系統(tǒng)

2011-07-18 14:54:55

HTML 5

2013-03-04 09:47:08

Python函數(shù)式編程

2016-10-20 21:02:12

微信小程序javascript

2023-02-28 08:24:49

2024-04-29 14:58:48

Python內(nèi)置函數(shù)

2023-09-11 06:12:31

盒子模型CSS

2011-12-26 16:39:43

局部函數(shù)

2023-11-23 19:30:35

Python編程語言

2012-01-12 09:32:17

響應(yīng)式Web設(shè)計(jì)

2014-11-12 13:26:55

創(chuàng)業(yè)

2021-08-30 08:23:34

Go語言進(jìn)程

2009-10-30 09:45:55

VB.NET Web
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號