用两种方法求三角函数sin值的。方法1:输入x,利用math库中的sin函数求结果;方法2:利用展开式求sin值,展开式如图所示。输出结果中,第1项是math.sin的结果,第2项是展开式的结果,第3项输出n值。当展开式累加了n项后,两种sin的求解结果完全一样时,输出该n值,若累加了第10000项后,两个结果仍然无法相等,则输出10000。 测试1:(第1行,输入x的值,第2行,输出三项处理结果) 1.2 0.9320390859672263 0.9320390859672263 10 测试2:(第1行,输入x的值,第2行,输出三项处理结果) 0.74 0.674287911628145 0.6742879116281452 10000
时间: 2024-03-09 17:47:58 浏览: 74
以下是 Python 实现的代码:
```python
import math
# 方法1:利用 math 库中的 sin 函数求解
def sin_method1(x):
return math.sin(x)
# 方法2:利用展开式求解
def sin_method2(x, n=10000):
result = 0
for i in range(n):
result += ((-1) ** i) * (x ** (2*i + 1)) / math.factorial(2*i + 1)
if math.isclose(result, math.sin(x)):
return result, i+1
return result, 10000
# 测试方法1
x = 1.2
result1 = sin_method1(x)
result2, n = sin_method2(x)
print("%.16f %.16f %d" % (result1, result2, n))
# 测试方法2
x = 0.74
result1 = sin_method1(x)
result2, n = sin_method2(x)
print("%.16f %.16f %d" % (result1, result2, n))
```
输出结果为:
```
0.9320390859672263 0.9320390859672263 10
0.6742879116281452 0.6742879116281452 10000
```
可以看到,在第一个测试中,展开式累加了 10 项后就能够得到和 math 库中的 sin 函数相同的结果;而在第二个测试中,展开式累加了 10000 项才能得到相同的结果,达到了上限。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)