繁体   English   中英

为什么我的代码与我的计算器和在线计算器相比是错误的,有人可以告诉我它有什么问题吗

[英]Why is my code wrong incomparsion to my calculator and online calculators can someone let me know whats wrong with it

 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方程

由于这是一个总和,因此您需要在循环内跟踪它。 现在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM