Python實現之初等函數三之三角函數
本文轉載自微信公眾號「python與大數據分析」,作者一只小小鳥鳥。轉載本文請聯系python與大數據分析公眾號。
三角函數在python和numpy中實現的不夠全面,主要包括cos, cosh, sin sinh, tan, tanh三角函數和arccos, arccosh, arcsin, arcsinh, arctan, arctanh反三角函數,cot,sec,csc,arccot,arcsec,arccsc均為提供,不過可以通過其他函數進行組合或變形得以實現。
三角函數是基本初等函數之一,是以角度(數學上最常用弧度制,下同)為自變量,角度對應任意角終邊與單位圓交點坐標或其比值為因變量的函數。也可以等價地用與單位圓有關的各種線段的長度來定義。三角函數在研究三角形和圓等幾何形狀的性質時有重要作用,也是研究周期性現象的基礎數學工具。在數學分析中,三角函數也被定義為無窮級數或特定微分方程的解,允許它們的取值擴展到任意實數值,甚至是復數值。
反三角函數是一種基本初等函數。它是反正弦arcsin x,反余弦arccos x,反正切arctan x,反余切arccot x,反正割arcsec x,反余割arccsc x這些函數的統稱,各自表示其正弦、余弦、正切、余切 ,正割,余割為x的角
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- # _ooOoo_
- # o8888888o
- # 88" . "88
- # ( | - _ - | )
- # O\ = /O
- # ____/`---'\____
- # .' \\| |// `.
- # / \\|||:|||// \
- # / _|||||-:- |||||- \
- # | | \\\ - /// | |
- # | \_| ''\---/'' | _/ |
- # \ .-\__ `-` ___/-. /
- # ___`. .' /--.--\ `. . __
- # ."" '< `.___\_<|>_/___.' >'"".
- # | | : `- \`.;`\ _ /`;.`/ - ` : | |
- # \ \ `-. \_ __\ /__ _/ .-` / /
- # ==`-.____`-.___\_____/___.-`____.-'==
- # `=---='
- '''
- @Project :pythonalgorithms
- @File :trigonometric.py
- @Author :不勝人生一場醉@Date :2021/7/26 23:28
- '''
- import matplotlib.pyplot as plt
- import numpy as np
- import math
- import mpl_toolkits.axisartist as axisartist # 導入坐標軸加工模塊
- # 三角函數是基本初等函數之一,是以角度(數學上最常用弧度制,下同)為自變量,角度對應任意角終邊與單位圓交點坐標或其比值為因變量的函數。
- # 也可以等價地用與單位圓有關的各種線段的長度來定義。三角函數在研究三角形和圓等幾何形狀的性質時有重要作用,
- # 也是研究周期性現象的基礎數學工具。
- # 在數學分析中,三角函數也被定義為無窮級數或特定微分方程的解,允許它們的取值擴展到任意實數值,甚至是復數值。
- # 正弦函數 :y =sin x
- # 正弦(sine),數學術語,在直角三角形中,任意一銳角∠A的對邊與斜邊的比叫做∠A的正弦,記作sinA(由英語sine一詞簡寫得來),即sinA=∠A的對邊/斜邊。
- # 余弦函數 :y =cos x
- # 余弦(余弦函數)。在Rt△ABC(直角三角形)中,∠C=90°(如概述圖所示),∠A的余弦是它的鄰邊比三角形的斜邊,即cosA=b/c,也可寫為cosa=AC/AB。余弦函數:f(x)=cosx(x∈R)
- # 平方和關系
- # (sinα)^2 +(cosα)^2=1
- # 積的關系
- # sinα = tanα × cosα(即sinα / cosα = tanα )
- # cosα = cotα × sinα (即cosα / sinα = cotα)
- # tanα = sinα × secα (即 tanα / sinα = secα)
- # 倒數關系
- # tanα × cotα = 1
- # sinα × cscα = 1
- # cosα × secα = 1
- # 商的關系
- # sinα / cosα = tanα = secα / cscα
- # 和角公式
- # sin ( α ± β ) = sinα · cosβ ± cosα · sinβ
- # sin ( α + β + γ ) = sinα · cosβ · cosγ + cosα · sinβ · cosγ + cosα · cosβ · sinγ - sinα · sinβ · sinγ
- # cos ( α ± β ) = cosα cosβ ∓ sinβ sinα
- # tan ( α ± β ) = ( tanα ± tanβ ) / ( 1 ∓ tanα tanβ )
- # 倍角半角公式
- # sin ( 2α ) = 2sinα · cosα [1]
- # sin ( 3α ) = 3sinα - 4sin & sup3 ; ( α ) = 4sinα · sin ( 60 + α ) sin ( 60 - α )
- # sin ( α / 2 ) = ± √( ( 1 - cosα ) / 2)
- # 級數展開
- # sin x = x - x3 / 3! + x5 / 5! - ... ( - 1 ) k - 1 * x 2 k - 1 / ( 2k - 1 ) ! + ... ( - ∞ < x < ∞ )
- # 導數
- # ( sinx ) ' = cosx
- # ( cosx ) ' = ﹣ sinx
- if __name__ == "__main__":
- sincosfunction()
- tanctnfunction()
- seccscfunction()
- arcsincosfunction()
- arccscfunction()
- def sincosfunction():
- plt.figure(figsize=(10, 5))
- ax = plt.gca() # 通過gca:get current axis得到當前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- x = np.linspace(-np.pi*2, np.pi*2, 200)
- y = np.sin(x)
- label = 'np.sin(x)'
- plt.plot(x, y, label=label)
- y = np.cos(x)
- label = 'np.cos(x)'
- plt.plot(x, y, label=label)
- y = np.power(np.sin(x),2)
- label = 'np.sin(x)^2'
- plt.plot(x, y, label=label)
- y = np.power(np.cos(x),2)
- label = 'np.cos(x)^2'
- plt.plot(x, y, label=label)
- y = np.power(np.cos(x), 2)+np.power(np.sin(x),2)
- label = 'np.sin(x)^2+np.cos(x)^2'
- plt.plot(x, y, label=label)
- # 設置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("sin&cos三角指數")
- plt.legend(loc='upper right')
- plt.show()
- # 正切函數 :y =tan x
- # 余切函數 :y =cot x
- def tanctnfunction():
- #np.tan()
- plt.figure(figsize=(10, 8))
- plt.subplot(1, 2, 1)
- ax = plt.gca() # 通過gca:get current axis得到當前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- x = np.append(np.linspace(-np.pi*3/2+0.01, -np.pi/2-0.01, 120),np.linspace(-np.pi/2+0.01, np.pi/2-0.01, 120))
- x = np.append(x,np.linspace(np.pi/2+0.01, np.pi*3/2-0.01, 120))
- y = np.tan(x)
- label = 'np.tan(x)'
- plt.plot(x, y, label=label)
- # 設置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("tan三角指數")
- plt.legend(loc='upper right')
- plt.subplot(1, 2, 2)
- ax = plt.gca() # 通過gca:get current axis得到當前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- x = np.append(np.linspace(-np.pi+ 0.01, - 0.01, 120),
- np.linspace( 0.01, np.pi - 0.01, 120))
- y = 1/np.tan(x)
- label = 'np.ctn(x)'
- plt.plot(x, y, label=label)
- # 設置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置
- ax.spines['left'].set_position(('axes', 0.5))
- #ax.spines['left'].set_position(('data', 0))
- plt.title("ctan三角指數")
- plt.legend(loc='upper right')
- plt.show()
- # 正割函數 :y =sec x = 1/cos(x)
- # 余割函數 :y =csc x = 1/sin(x)
- def seccscfunction():
- plt.figure(figsize=(10, 5))
- ax = plt.gca() # 通過gca:get current axis得到當前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- #x = np.linspace(-np.pi*2, np.pi*2, 200)
- x = np.append(np.linspace(-np.pi * 3 / 2 + 0.01, -np.pi - 0.01, 120),
- np.linspace(-np.pi + 0.01, -np.pi / 2 - 0.01, 120))
- x = np.append(x, np.linspace(-np.pi / 2 + 0.01, - 0.01, 120))
- x = np.append(x, np.linspace(0.01, np.pi / 2 - 0.01, 120))
- x = np.append(x, np.linspace(np.pi / 2 + 0.01, np.pi - 0.01, 120))
- x = np.append(x, np.linspace(np.pi + 0.01, np.pi * 3 / 2 - 0.01, 120))
- y = 1/np.sin(x)
- label = 'np.csc(x)'
- plt.plot(x, y, label=label)
- y = 1/np.cos(x)
- label = 'np.sec(x)'
- plt.plot(x, y, label=label)
- # 設置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("csc&sec三角指數")
- plt.legend(loc='upper right')
- plt.show()
- ef arcsincosfunction():
- plt.figure(figsize=(5, 10))
- ax = plt.gca() # 通過gca:get current axis得到當前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- x = np.linspace(-1, 1, 200)
- y = np.arcsin(x)
- label = 'np.arcsin(x)'
- plt.plot(x, y, label=label)
- y = np.arccos(x)
- label = 'np.arccos(x)'
- plt.plot(x, y, label=label)
- # 設置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("arcsin&arccos三角指數")
- plt.legend(loc='upper right')
- plt.show()
- # 反正切函數
- # 正切函數y=tan x在(-π/2,π/2)上的反函數,叫做反正切函數。記作arctanx,表示一個正切值為x的角,該角的范圍在(-π/2,π/2)區間內。
- # 定義域R,值域(-π/2,π/2)。
- # numpy.arctan()
- # 反余切函數
- # 余切函數y=cot x在(0,π)上的反函數,叫做反余切函數。記作arccotx,表示一個余切值為x的角,該角的范圍在(0,π)區間內。
- # 定義域R,值域(0,π)。
- # 反正割函數
- # 正割函數 :y =sec x = 1/cos(x)
- # 正割函數y=sec x在[0,π/2)U(π/2,π]上的反函數,叫做反正割函數。記作arcsecx,表示一個正割值為x的角,該角的范圍在[0,π/2)U(π/2,π]區間內。
- # 定義域(-∞,-1]U[1,+∞),值域[0,π/2)U(π/2,π]。
- # 反余割函數
- # 余割函數 :y =csc x = 1/sin(x)
- # 余割函數y=csc x在[-π/2,0)U(0,π/2]上的反函數,叫做反余割函數。記作arccscx,表示一個余割值為x的角,該角的范圍在[-π/2,0)U(0,π/2]區間內。
- # 定義域(-∞,-1]U[1,+∞),值域[-π/2,0)U(0,π/2]。
- def arccscfunction():
- plt.figure(figsize=(10, 5))
- ax = plt.gca() # 通過gca:get current axis得到當前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- x = np.append(np.linspace(0.01, np.pi / 2 - 0.01, 120),
- np.linspace(np.pi/2+0.01, np.pi - 0.01, 120))
- y = 1/np.cos(x)
- # 正割函數 sec(x)=1/cos(x)
- # 反正割函數 顛倒x,y值即可
- label = 'np.arcsecx(x)'
- plt.plot(y, x, label=label)
- # 設置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("arcsin&arccos三角指數")
- plt.legend(loc='upper right')
- plt.show()