首页 > 解决方案 > 为什么我的代码与我的计算器和在线计算器相比是错误的,有人可以让我知道它有什么问题吗

问题描述

 from math import cos
 from math import  sin
 from math import pi
 a0 = int(input("a0:"))
 b0 = int(input("b0:"))
 N = int(input("N:"))
 L = int(input("L:"))
 X = int(input("X:"))
 n = 0
 an = a0
 bn = b0
 y=0
 for i in range(N):
    n = n+1
    an = an + 10   
    bn = bn * 10   
    y = an * (cos(((n*pi*X)/(L))))+ (bn*(sin((n*pi*X)/(L))))
 total = a0 + y
 print(total)

我假设 y = .... 代码是错误的,因为 an 和 bn 代码工作正常 lmk Equation

标签: python

解决方案


由于这是一个总和,因此您需要在循环内跟踪它。现在y将只是最后一次迭代的结果。

 from math import cos
 from math import  sin
 from math import pi
 a0 = int(input("a0:"))
 b0 = int(input("b0:"))
 N = int(input("N:"))
 L = int(input("L:"))
 X = int(input("X:"))
 n = 0
 an = a0
 bn = b0
 y=0
 for i in range(N):
    n = n+1
    an = an + 10   
    bn = bn * 10   
    y += an * (cos(((n*pi*X)/(L))))+ (bn*(sin((n*pi*X)/(L))))
 total = a0 + y
 print(total)

推荐阅读