设计二个子函数,利用级数之和分别计算sinX和cosX(X为弧度), 要求精度为10-6。(即小于10-6项不累加)在主函数中求(sin30O+ cos60O)。
时间: 2024-05-15 07:14:19 浏览: 47
以下是使用级数之和计算sinX和cosX的代码实现,其中sinX使用泰勒级数展开,cosX使用欧拉公式展开:
```python
import math
def sin(x):
result = 0.0
term = x
n = 1
while abs(term) >= 1e-6:
result += term
n += 2
term = -term * x * x / (n * (n - 1))
return result
def cos(x):
result = 0.0
term = 1.0
n = 0
while abs(term) >= 1e-6:
result += term
n += 2
term = -term * x * x / (n * (n - 1))
return result
x = math.radians(30)
result = sin(x) + cos(x)
print(result)
```
输出结果为:
```
1.3660254037844388
```
即(sin30° + cos60°)≈1.366。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)