Search code examples
pythonfunctionloops

Iterate over a parameter of the function


I have this code that i want to compute for 0 < t < pi with 0.05 step, but each new function is a function of the previous one, and I can't really think of an easy way to iterate over it, any tips?

wf_0 = get_wf_0(xmin, xmax, Nx, neigs, t=0) #Ignore this since it's another function
wf_1 = get_wf(xmin, xmax, Nx, neigs, t=0.05 , wf = wf_0)
wf_2 = get_wf(xmin, xmax, Nx, neigs, t=0.1 , wf = wf_1)
wf_3 = get_wf(xmin, xmax, Nx, neigs, t=0.15 , wf = wf_2)

and so on up to t = pi


Solution

  • If you want to keep track of the intermediate values, append the results to a list and keep using the last value as the wf argument value for the next iteration:

    wf_list = [ get_wf_0(xmin, xmax, Nx, neigs, t=0) ]
    
    pi = 3.14159
    t = 0.05
    
    while t < pi:
      wf_list.append(get_wf(xmin, xmax, Nx, neigs, t=t , wf=wf_list[-1]))
      t += 0.05
    

    wf_list[n] now contains the value of each wf_n