谁能说明为什么给定代码使用泰勒级数展开来计算sin(x)的值不能正常工作?

这是我尝试的代码,无法正常工作,我不知道问题所在

import math as m
def sinse(x,n):
    sume=0
    for i in range(n+1):
        sume+=(m.pow(-1,i)*m.pow(x,2*i+1)/m.factorial(2*i+1))
        return sume
n=int(input('Enter the value of n:'))
x=int(input('enter the value of X:'))
print(sinse(x,n))

如果这不是正确的代码,请在答案中添加其他任何代码

fderfr 回答:谁能说明为什么给定代码使用泰勒级数展开来计算sin(x)的值不能正常工作?

return sume中断了for循环。我不确定您的公式的准确性,但我认为以1缩进该行(因此发生在for循环之后但在函数内部)应该可以使其正常工作。

,

修正缩进将给出以下内容:

import math as m
def sinse(x,n):
    sume=0
    for i in range(n+1):
        sume+=(m.pow(-1,i)*m.pow(x,2*i+1)/m.factorial(2*i+1))
    return sume
n=int(input('Enter the value of n:'))
x=int(input('enter the value of X:'))
print(sinse(x,n))
本文链接:https://www.f2er.com/3067500.html

大家都在问