import numpy as np import matplotlib.pyplot as plt def plot_trig_function(trig_func, start, end, step): x = np.arange(start, end, step) if trig_func == 'sin': y = np.sin(x) elif trig_func == 'cos': y = np.cos(x) elif trig_func == 'tan': y = np.tan(x) else: print('Invalid trig function') return plt.plot(x, y) plt.xlabel('x') plt.ylabel(trig_func + '(x)') plt.title(trig_func + ' function') plt.show()
时间: 2024-01-18 18:04:47 浏览: 144
这是我为您编写的程序,可以根据输入的三角函数类型和参数,绘制相应的曲线。您可以根据需要调整起始点、终止点和点数,以及选择其他三角函数来绘制相应的曲线。
示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
def plot_trig_function(trig_func, start, end, step):
x = np.arange(start, end, step)
if trig_func == 'sin':
y = np.sin(x)
elif trig_func == 'cos':
y = np.cos(x)
elif trig_func == 'tan':
y = np.tan(x)
else:
print('Invalid trig function')
return
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel(trig_func + '(x)')
plt.title(trig_func + ' function')
plt.show()
# 绘制sin函数从0到2π的曲线
plot_trig_function('sin', 0, 2*np.pi, 0.1)
```
输出结果为:
![sin函数曲线](https://img-blog.csdn.net/20180517204726173?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZpbGVoYW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
阅读全文