sums = [0] * N
for x in range (N2):
I = 1 #resets the variables
Q = 0
U = 0
for x in range (N):
theta = 90 + randint(-dtheta, dtheta) #random component
#you don't need to copy these variables, they're redundant
#Q0=Q
#U0=U
#I0=I
#reuse the P-value from previous iteration
I = (I * cosh(dTp) + S * sinh(dTp)) * exp(-dT)
Q = (Ip * cos(2 * theta) + Q) * exp(-dT)
U = (Ip * sin(2 * theta) + U) * exp(-dT)
P = 100 * sqrt(Q**2 + U**2) / I
print P
#add the value of P to the corresponding index at x in sums[]
sums[x] += P
#this is called a list comprehension
#it is a nicer way of looping over an iterable object (like a list)
avgs = [n / float(N2) for n in sums]
Python Find Average in Nested Loop [closed]
-
10-07-2023 - |
Вопрос
I was trying to do this using arrays, but my lack of any programming skills made that difficult. So here's to trying something else.
I have a program:
for x in range (0,N2):
I=1 #resets the variables
Q=0
U=0
for x in range (0,N):
theta= 90+randint (-dtheta,dtheta) #random component
Q0=Q
U0=U
I0=I
I=(I0*cosh(dTp)+S*sinh(dTp))*exp(-dT) #reuses P from previous run through
Q=(Ip*cos(2*theta)+Q0)*exp(-dT)
U=(Ip*sin(2*theta)+U0)*exp(-dT)
P=100*sqrt(Q**2+U**2)/I
print 'x=', x, 'P=', P
So, the program goes through the complicated equations to get a P-value, and loops through those equations N
number of times. Then it changes some variables randomly, and goes through the process N2
number of times.
What I am trying to do: every N2
time it hits a N
value, I want the average of those values.
This is what the program (printing x
and P
) currently prints.
x=0 P= 0.666656790299
x=1 P= 1.33305129414
x=2 P= 1.99135189726
x=3 P= 2.65356540458
x=4 P= 3.31718464722
x=5 P= 3.94383330744
x=6 P= 4.57470649236
x=7 P= 5.22041300059
x=8 P= 5.87783977662
x=9 P= 6.53297448834
x=0 P= 0.666656790299
x=1 P= 1.33244225853
x=2 P= 1.96631331155
x=3 P= 2.6285933052
x=4 P= 3.2901846442
x=5 P= 3.95565476517
x=6 P= 4.61500717059
x=7 P= 5.27548752021
x=8 P= 5.87881617052
x=9 P= 6.53895040683
where N2=2 and N=10. Do you see how there are two values like .66 (x=0
)? And two like 6.5 (x=9
)? I want to be able to have the average of all the numbers that have the same N value. So, the average of all the x=0 values (~.66) the x=1 values (~1.33) all the way to x=9 values (~6.65).
The end goal is to graph all these averages versus N.
Any help would be amazing, because I know next to nothing on programming.
Решение
Другие советы
I'm still not sure where in the loop you are requesting to average the value.
Put a counter variable outside of that. Count the times you calculate P. Then have a sum_p variable to maintains the total values of all P's calculated.
Then average is just sum_p/cnt.
When you want to start tracking a new average, you would set cnt = 0 and sum_p = 0. We have cnt for free with N.
for x in range (0,N2):
I=1 #resets the variables
Q=0
U=0
sum_p = 0
for x in range (0,N):
theta= 90+randint (-dtheta,dtheta) #random component
Q0=Q
U0=U
I0=I
I=(I0*cosh(dTp)+S*sinh(dTp))*exp(-dT) #reuses P from previous run through
Q=(Ip*cos(2*theta)+Q0)*exp(-dT)
U=(Ip*sin(2*theta)+U0)*exp(-dT)
P=100*sqrt(Q**2+U**2)/I
sum+p += P
print P
print 'Avg', sum_p//N
I'm not sure why you are creating a duplicate copy of Q,U,I. Instead of just using the previous copy in the calculation. This is perfectly valid, it uses the old value and sets the new value.
I=(I*cosh(dTp)+S*sinh(dTp))*exp(-dT) #reuses P from previous run through
Q=(Ip*cos(2*theta)+Q)*exp(-dT)
U=(Ip*sin(2*theta)+U)*exp(-dT)