I'm doing a simulation of an integral by taking numbers between 0 and 1, so I can get a force components. But I always get wrong cosine values, for example, when I take a=1 (so it makes a 45ยบ angle, and therefore "L" should be equal to "k"), I get a "k" bigger than "L".If someone can please look what's wrong I'd really apreciate.
from math import pi, sin,cos, atan
for n in s:
f=atan(n/a)
L=ko*(Q/N*)*cos(f)
k=ko*(Q/N)*sin(f)
y.append(k)
x.append(L)
yy=sum(y)
xx=sum(x)
print (xx,yy)
You should use np.array initialized to zero if you want to deal with big arrays (just a suggestion, it is working either way, i prefer to use arrays):
The small difference that is there is because of float cannot hold more precise value. You can use
np.pi/4
in place off
to get same values. If you want to use higher precision values, see this answer. If you want to simulate integration you might have to look into monte carlo simlations.