Repeating an iterating function N times

66 views Asked by At

I have an iterative function, f(x) = ax + b, which I want to repeat N times. My steps are as follows
1.) Define the iterative function
2.) Assign a value to each repetition
3.) Stop at the Nth repetition

# Define repeating function
f += a*f + b

# Assign values to iterations 


# Stop iterating at Nth value

for i in range(N+1):
    f += f(i)

I'm unsure of how to label each iteration of the function. My first thought is to create a list of all values of the function.

1

There are 1 answers

1
patoba On BEST ANSWER

The first thing is to declare your function, in Python there are several ways to do it. When a function is small it is usually defined as a lambda. A lambda follows the syntax.

func = lambda parameter1, parameter2, ..., parameter: <return value>

Then we would have to define the initial values, in this case I will call the final value v and the number of iterations N

The last thing is to call the function N times, for this I use a for loop

# Define repeating function
a = 5
b = 1
f = lambda x : a * x + b

# Assign values to iterations 
v = 0
N = 10

# Stop iterating at Nth value
for i in range(N):
    v = f(v)