Garry Saini Garry Saini - 3 years ago 119
Python Question

how to make output an input in python

>>> import math

#defining first function
>>> def f(a):
return a-math.sin(a)-math.pi/2

#defining second fuction
>>> def df(a):
return 1-math.cos(a)

#defining third function which uses above functions
>>> def alpha(a):
return a-f(a)/df(a)

How to write a code in which alpha(a) takes a starting value of a=2, and the solution of alpha(2) will become the input the next time. For eg: let's suppose alpha(2) comes to 2.39 , hence the next value would be alpha(2.39) and go on {upto 50 iterations}. Can somebody please help me a bit. thanks in advance.

Answer Source

You can let the program iterate with a for loop, and use a variable to store the intermediate results:

temp = 2                # set temp to the initial value
for _ in range(50):     # a for loop that will iterate 50 times
    temp = alpha(temp)  # call alpha with the result in temp
                        # and store the result back in temp
    print(temp)         # print the result (optional)

print(temp) will print the intermediate results. It is not required. It only demonstrates how the temp variable is updated throughout the process.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download