简体   繁体   中英

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)

Im assuming that the y =.... code is wrong since the an and bn code works fine lmk Equation

Since this is a sum you need to be keeping track of that inside the loop. Right now y will just be the result of that last iteration.

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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