Prachi Srivastava
Prachi Srivastava

Reputation: 23

Iteration for the last value of iteration in Python

How can I define a function in python in such a way that it takes the previous value of my iteration where I define the initial value.

My function is defined as following:

def Deulab(c, yh1, a, b):
     Deulab = c- (EULab(c, yh1, a, b)-1)*0.3
     return (Deulab,yh1, a,b)

Output is

Deulab(1.01, 1, 4, 2)
0.9964391705626454

Now I want to iterate keeping yh1, a ,b fixed and start with c0=1 and iterate recursively for c.

Upvotes: 1

Views: 90

Answers (3)

Amiga500
Amiga500

Reputation: 1275

CodeCupboard has supplied an example which should fit your needs.

This is a bit of a more persistent version of that, which would allow you to go back to where you were with multiple separate function calls

class classA:  

    #Declare initial values for class variables here
    fooResult = 0  #Say, taking 0 as an initial value, not unreasonable!

    def myFoo1(x):
        y = 2*x + fooResult  #A simple example function

        classA.fooResult = y  #This line is updating that class variable, so next time you come in, you'll be using it as part of calc'ing y

        return y  #and this will return the calculation back up to wherever you called it from


#Example call
rtn = classA.myFoo1(5)
#rtn1 will be 10, as this is the first call to the function, so the class variable had initial state of 0

#Example call2
rtn2 = classA.myFoo1(3)
#rtn2 will be 16, as the class variable had a state of 10 when you called classA.myFoo1()

So if you were working with a dataset where you didn't know what the second call would be (i.e. the 3 in call2 above was unknown), then you can revisit the function without having to worry about handling the data retention in your top level code. Useful for a niche case.

Of course, you could use it as per:

list1 = [1,2,3,4,5]
for i in list1:
    rtn = classA.myFoo1(i)

Which would give you a final rtn value of 30 when you exit the for loop.

Upvotes: 1

John Coleman
John Coleman

Reputation: 51998

The most pythonic way of doing this is to define an interating generator:

def iterates(f,x):
    while True:
        yield x
        x = f(x)

#test:
def f(x):
    return 3.2*x*(1-x)

orbit = iterates(f,0.1)

for _ in range(10):
    print(next(orbit))

Output:

0.1
0.2880000000000001
0.6561792000000002
0.7219457839595519
0.6423682207442558
0.7351401271107676
0.6230691859914625
0.7515327214700762
0.5975401280955426
0.7695549549155365

You can use the generator until some stop criterion is met. For example, in fixed-point iteration you might iterate until two successive iterates are within some tolerance of each other. The generator itself will go on forever, so when you use it you need to make sure that your code doesn't go into an infinite loop (e.g. don't simply assume convergence).

Upvotes: 1

CodeCupboard
CodeCupboard

Reputation: 1585

It sound like you are after recursion.

Here is a basic example

def f(x):
    x += 1
    if x < 10:
        x = f(x)
    return x

print (f(4))

In this example a function calls itself until a criteria is met.

Upvotes: 1

Related Questions