Python实现之初等函数三之三角函数

开发 后端
三角函数在python和numpy中实现的不够全面,主要包括cos, cosh, sin sinh, tan, tanh三角函数和arccos, arccosh, arcsin, arcsinh, arctan, arctanh反三角函数,cot,sec,csc,arccot,arcsec,arccsc均为提供,不过可以通过其他函数进行组合或变形得以实现。

[[414278]]

本文转载自微信公众号「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的角

  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 :trigonometric.py 
  24. @Author :不胜人生一场醉@Date :2021/7/26 23:28  
  25. ''
  26. import matplotlib.pyplot as plt 
  27. import numpy as np 
  28. import math 
  29. import mpl_toolkits.axisartist as axisartist  # 导入坐标轴加工模块 
  30. # 三角函数是基本初等函数之一,是以角度(数学上最常用弧度制,下同)为自变量,角度对应任意角终边与单位圆交点坐标或其比值为因变量的函数。 
  31. # 也可以等价地用与单位圆有关的各种线段的长度来定义。三角函数在研究三角形和圆等几何形状的性质时有重要作用, 
  32. # 也是研究周期性现象的基础数学工具。 
  33. # 在数学分析中,三角函数也被定义为无穷级数或特定微分方程的解,允许它们的取值扩展到任意实数值,甚至是复数值。 
  34. # 正弦函数 :y =sin x 
  35. # 正弦(sine),数学术语,在直角三角形中,任意一锐角∠A的对边与斜边的比叫做∠A的正弦,记作sinA(由英语sine一词简写得来),即sinA=∠A的对边/斜边。 
  36. # 余弦函数 :y =cos x 
  37. # 余弦(余弦函数)。在Rt△ABC(直角三角形)中,∠C=90°(如概述图所示),∠A的余弦是它的邻边比三角形的斜边,即cosA=b/c,也可写为cosa=AC/AB。余弦函数:f(x)=cosx(x∈R) 
  38. # 平方和关系 
  39. # (sinα)^2 +(cosα)^2=1 
  40. # 积的关系 
  41. # sinα = tanα × cosα(即sinα / cosα = tanα ) 
  42. # cosα = cotα × sinα (即cosα / sinα = cotα) 
  43. # tanα = sinα × secα (即 tanα / sinα = secα) 
  44. # 倒数关系 
  45. # tanα × cotα = 1 
  46. # sinα × cscα = 1 
  47. # cosα × secα = 1 
  48. # 商的关系 
  49. # sinα / cosα = tanα = secα / cscα 
  50. # 和角公式 
  51. # sin ( α ± β ) = sinα · cosβ ± cosα · sinβ 
  52. # sin ( α + β + γ ) = sinα · cosβ · cosγ + cosα · sinβ · cosγ + cosα · cosβ · sinγ - sinα · sinβ · sinγ 
  53. # cos ( α ± β ) = cosα cosβ ∓ sinβ sinα 
  54. # tan ( α ± β ) = ( tanα ± tanβ ) / ( 1 ∓ tanα tanβ ) 
  55. # 倍角半角公式 
  56. # sin ( 2α ) = 2sinα · cosα [1] 
  57. # sin ( 3α ) = 3sinα - 4sin & sup3 ; ( α ) = 4sinα · sin ( 60 + α ) sin ( 60 - α ) 
  58. # sin ( α / 2 ) = ± √( ( 1 - cosα ) / 2) 
  59. # 级数展开 
  60. # sin x = x - x3 / 3! + x5 / 5! - ... ( - 1 ) k - 1 * x 2 k - 1 / ( 2k - 1 ) ! + ... ( - ∞ < x < ∞ ) 
  61. # 导数 
  62. # ( sinx ) ' = cosx 
  63. # ( cosx ) ' = ﹣ sinx 
  64.  
  65. if __name__ == "__main__"
  66.    sincosfunction() 
  67.    tanctnfunction() 
  68.    seccscfunction() 
  69.    arcsincosfunction() 
  70.    arccscfunction() 
  1. def sincosfunction(): 
  2.    plt.figure(figsize=(10, 5)) 
  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(-np.pi*2, np.pi*2, 200) 
  7.  
  8.    y = np.sin(x) 
  9.    label = 'np.sin(x)' 
  10.    plt.plot(x, y, label=label) 
  11.    y = np.cos(x) 
  12.    label = 'np.cos(x)' 
  13.    plt.plot(x, y, label=label) 
  14.    y = np.power(np.sin(x),2) 
  15.    label = 'np.sin(x)^2' 
  16.    plt.plot(x, y, label=label) 
  17.    y = np.power(np.cos(x),2) 
  18.    label = 'np.cos(x)^2' 
  19.    plt.plot(x, y, label=label) 
  20.    y = np.power(np.cos(x), 2)+np.power(np.sin(x),2) 
  21.    label = 'np.sin(x)^2+np.cos(x)^2' 
  22.    plt.plot(x, y, label=label) 
  23.  
  24.    # 设置图片的右边框和上边框为不显示 
  25.    ax.spines['right'].set_color('none'
  26.    ax.spines['top'].set_color('none'
  27.  
  28.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  29.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  30.    ax.spines['bottom'].set_position(('data', 0)) 
  31.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  32.    # ax.spines['left'].set_position(('axes', 0.5)) 
  33.    ax.spines['left'].set_position(('data', 0)) 
  34.    plt.title("sin&cos三角指数"
  35.    plt.legend(loc='upper right'
  36.    plt.show() 

  1. # 正切函数 :y =tan x 
  2. # 余切函数 :y =cot x 
  3. def tanctnfunction(): 
  4.    #np.tan() 
  5.    plt.figure(figsize=(10, 8)) 
  6.    plt.subplot(1, 2, 1) 
  7.    ax = plt.gca()  # 通过gca:get current axis得到当前轴 
  8.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 绘图中文 
  9.    plt.rcParams['axes.unicode_minus'] = False  # 绘图负号 
  10.    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)) 
  11.    x = np.append(x,np.linspace(np.pi/2+0.01, np.pi*3/2-0.01, 120)) 
  12.    y = np.tan(x) 
  13.    label = 'np.tan(x)' 
  14.    plt.plot(x, y, label=label) 
  15.  
  16.  
  17.    # 设置图片的右边框和上边框为不显示 
  18.    ax.spines['right'].set_color('none'
  19.    ax.spines['top'].set_color('none'
  20.  
  21.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  22.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  23.    ax.spines['bottom'].set_position(('data', 0)) 
  24.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  25.    # ax.spines['left'].set_position(('axes', 0.5)) 
  26.    ax.spines['left'].set_position(('data', 0)) 
  27.    plt.title("tan三角指数"
  28.    plt.legend(loc='upper right'
  29.  
  30.  
  31.    plt.subplot(1, 2, 2) 
  32.    ax = plt.gca()  # 通过gca:get current axis得到当前轴 
  33.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 绘图中文 
  34.    plt.rcParams['axes.unicode_minus'] = False  # 绘图负号 
  35.    x = np.append(np.linspace(-np.pi+ 0.01, - 0.01, 120), 
  36.               np.linspace( 0.01, np.pi - 0.01, 120)) 
  37.    y = 1/np.tan(x) 
  38.    label = 'np.ctn(x)' 
  39.    plt.plot(x, y, label=label) 
  40.    # 设置图片的右边框和上边框为不显示 
  41.    ax.spines['right'].set_color('none'
  42.    ax.spines['top'].set_color('none'
  43.  
  44.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  45.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  46.    ax.spines['bottom'].set_position(('data', 0)) 
  47.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  48.    ax.spines['left'].set_position(('axes', 0.5)) 
  49.    #ax.spines['left'].set_position(('data', 0)) 
  50.    plt.title("ctan三角指数"
  51.    plt.legend(loc='upper right'
  52.    plt.show() 

  1. # 正割函数 :y =sec x = 1/cos(x) 
  2. # 余割函数 :y =csc x = 1/sin(x) 
  3. def seccscfunction(): 
  4.    plt.figure(figsize=(10, 5)) 
  5.    ax = plt.gca()  # 通过gca:get current axis得到当前轴 
  6.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 绘图中文 
  7.    plt.rcParams['axes.unicode_minus'] = False  # 绘图负号 
  8.    #x = np.linspace(-np.pi*2, np.pi*2, 200) 
  9.    x = np.append(np.linspace(-np.pi * 3 / 2 + 0.01, -np.pi - 0.01, 120), 
  10.               np.linspace(-np.pi + 0.01, -np.pi / 2 - 0.01, 120)) 
  11.    x = np.append(x, np.linspace(-np.pi / 2 + 0.01,  - 0.01, 120)) 
  12.    x = np.append(x, np.linspace(0.01, np.pi  / 2 - 0.01, 120)) 
  13.    x = np.append(x, np.linspace(np.pi / 2 + 0.01, np.pi  - 0.01, 120)) 
  14.    x = np.append(x, np.linspace(np.pi + 0.01, np.pi * 3 / 2 - 0.01, 120)) 
  15.    y = 1/np.sin(x) 
  16.    label = 'np.csc(x)' 
  17.    plt.plot(x, y, label=label) 
  18.    y = 1/np.cos(x) 
  19.    label = 'np.sec(x)' 
  20.    plt.plot(x, y, label=label) 
  21.  
  22.    # 设置图片的右边框和上边框为不显示 
  23.    ax.spines['right'].set_color('none'
  24.    ax.spines['top'].set_color('none'
  25.  
  26.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  27.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  28.    ax.spines['bottom'].set_position(('data', 0)) 
  29.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  30.    # ax.spines['left'].set_position(('axes', 0.5)) 
  31.    ax.spines['left'].set_position(('data', 0)) 
  32.    plt.title("csc&sec三角指数"
  33.    plt.legend(loc='upper right'
  34.    plt.show() 

  1. ef arcsincosfunction(): 
  2.    plt.figure(figsize=(5, 10)) 
  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(-1, 1, 200) 
  7.  
  8.    y = np.arcsin(x) 
  9.    label = 'np.arcsin(x)' 
  10.    plt.plot(x, y, label=label) 
  11.    y = np.arccos(x) 
  12.    label = 'np.arccos(x)' 
  13.    plt.plot(x, y, label=label) 
  14.  
  15.    # 设置图片的右边框和上边框为不显示 
  16.    ax.spines['right'].set_color('none'
  17.    ax.spines['top'].set_color('none'
  18.  
  19.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  20.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  21.    ax.spines['bottom'].set_position(('data', 0)) 
  22.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  23.    # ax.spines['left'].set_position(('axes', 0.5)) 
  24.    ax.spines['left'].set_position(('data', 0)) 
  25.    plt.title("arcsin&arccos三角指数"
  26.    plt.legend(loc='upper right'
  27.    plt.show() 

  1. # 反正切函数 
  2. #  正切函数y=tan x在(-π/2,π/2)上的反函数,叫做反正切函数。记作arctanx,表示一个正切值为x的角,该角的范围在(-π/2,π/2)区间内。 
  3. #  定义域R,值域(-π/2,π/2)。 
  4. #   numpy.arctan() 
  5. # 反余切函数 
  6. #  余切函数y=cot x在(0,π)上的反函数,叫做反余切函数。记作arccotx,表示一个余切值为x的角,该角的范围在(0,π)区间内。 
  7. #  定义域R,值域(0,π)。 
  8. # 反正割函数 
  9. #   正割函数 :y =sec x = 1/cos(x) 
  10. #  正割函数y=sec x在[0,π/2)U(π/2,π]上的反函数,叫做反正割函数。记作arcsecx,表示一个正割值为x的角,该角的范围在[0,π/2)U(π/2,π]区间内。 
  11. #  定义域(-∞,-1]U[1,+∞),值域[0,π/2)U(π/2,π]。 
  12. # 反余割函数 
  13. #   余割函数 :y =csc x = 1/sin(x) 
  14. #  余割函数y=csc x在[-π/2,0)U(0,π/2]上的反函数,叫做反余割函数。记作arccscx,表示一个余割值为x的角,该角的范围在[-π/2,0)U(0,π/2]区间内。 
  15. #  定义域(-∞,-1]U[1,+∞),值域[-π/2,0)U(0,π/2]。 
  16. def arccscfunction(): 
  17.    plt.figure(figsize=(10, 5)) 
  18.    ax = plt.gca()  # 通过gca:get current axis得到当前轴 
  19.    plt.rcParams['font.sans-serif'] = ['SimHei']  # 绘图中文 
  20.    plt.rcParams['axes.unicode_minus'] = False  # 绘图负号 
  21.    x = np.append(np.linspace(0.01, np.pi / 2 - 0.01, 120), 
  22.               np.linspace(np.pi/2+0.01, np.pi  - 0.01, 120)) 
  23.    y = 1/np.cos(x) 
  24.    # 正割函数 sec(x)=1/cos(x) 
  25.    # 反正割函数 颠倒x,y值即可 
  26.    label = 'np.arcsecx(x)' 
  27.    plt.plot(y, x, label=label) 
  28.  
  29.    # 设置图片的右边框和上边框为不显示 
  30.    ax.spines['right'].set_color('none'
  31.    ax.spines['top'].set_color('none'
  32.  
  33.    # 挪动x,y轴的位置,也就是图片下边框和左边框的位置 
  34.    # data表示通过值来设置x轴的位置,将x轴绑定在y=0的位置 
  35.    ax.spines['bottom'].set_position(('data', 0)) 
  36.    # axes表示以百分比的形式设置轴的位置,即将y轴绑定在x轴50%的位置 
  37.    # ax.spines['left'].set_position(('axes', 0.5)) 
  38.    ax.spines['left'].set_position(('data', 0)) 
  39.    plt.title("arcsin&arccos三角指数"
  40.    plt.legend(loc='upper right'
  41.    plt.show() 

 

责任编辑:武晓燕 来源: python与大数据分析
相关推荐

2021-07-27 05:04:12

python初等函数

2021-07-30 05:00:04

Python初等函数

2020-07-06 14:30:47

前端三角函数动画

2021-05-07 09:31:10

三角函数指数函数取整函数

2019-04-30 14:17:56

中关村零售业创业者

2022-02-16 08:21:28

CSS三角边框动画SVG

2022-03-16 14:27:49

CSS三角形前端

2021-02-21 14:05:02

区块链比特币安全

2021-02-25 18:05:31

Plots图形绘图应用Linux

2021-08-30 07:16:45

商业技术团队

2020-10-27 11:24:29

avaScript m

2022-04-19 06:27:13

CSS数学函数calc

2016-10-20 13:36:28

WebRTC浏览器服务器

2021-10-19 10:09:21

三角形个数数组

2024-01-24 13:08:00

2013-03-05 10:01:29

Python函数式编程

2024-02-20 18:30:53

CSS属性边框

2011-02-13 17:14:15

LinuxApacheNginx

2015-05-27 15:29:35

IBM互联网+

2016-08-29 16:18:27

点赞
收藏

51CTO技术栈公众号