Home > Enterprise >  For Loop Function Iteration
For Loop Function Iteration

Time:08-04

I am attempting to use a for loop to run a function f(x) on a range of values. I am running into an issue when I want to recalculate my input on the next step dependent on the previous step. Below is some pseudo code that might explain my issue better and below that is the actual code I am attempting to write. The goal is to have a loop that calculates the results on one step and recalculates an input on the next step dependent on the results. IE:

for i in range(10):
    H = i - 1
    result_up = f(H)
    H_delta = (nsolve(Eq(result_up,A(H1)),H1,1))
    i  = H_delta

The idea is that in the next iteration result_up = f(i = H_delta) so on and so forth.

from sympy import *
USHead = np.linspace(1,3,25)
PHeight = 1.5
WH = Symbol('WH')

for i in USHead:
  if i <= PHeight:
    Oh = i-1
    OD = ((Cd * 0.738 * math.sqrt(2 * 32.2 * (Oh)))* 2)
    i_delta = (nsolve(Eq(OD,(0.0612*(TwH1/1) 3.173)*(6-0.003)*(TwH1 .003)**(1.5)), TwH1,1))  
    i  = i_delta  

My understanding of for loops is that you have the ability to recalculate i as you continue through the iteration but am thinking the issue is because I have defined my range as a list?

The step size of the list is .083 starting at 1 and ending at 3.

CodePudding user response:

You can't use a for loop if you want to update the iteration variable yourself, since it will override that with the next value from the range. Use a while loop.

i = 0
while i < 10:
    H = i - 1
    result_up = f(H)
    H_delta = (nsolve(Eq(result_up,A(H1)),H1,1))
    i  = H_delta
  • Related