I'm trying to figure out how to write a loop that implements a chaotic map defined by f(n 1)=ln |f(n)|, where f(0)is any real number excluding 1 and e.
import math
num_iters = 10
f = lambda f: math.log(math.fabs(f))
for ii in range(num_iters):
f = f(ii)
print(f)
Im getting confused by how to implement f(n 1) and exclude 1 and e. So what I've done is I define the function with the lambda and then i loop through it with the last part of the code but when that happens I get a math domain error. Also I don't feel like this is the right approach.
Another thing I tried was this:
import math
num_iters = 10
f = lambda x: math.log(math.fabs(f(x)))
for ii in range(num_iters):
f = f(ii)
print(f)
Which I feel is closer to the correct implementation since I'm getting this error now:
RecursionError: maximum recursion depth exceeded
But I am pretty sure I solved it using numpy, would still like to know how to implement it using just math though.
import numpy as np
num_iters = 100
f_values = np.empty(num_iters)
f_values[0] = np.log(np.abs(f_values[0]))
for ii in range(1, num_iters):
f_values[ii] = np.log(np.abs(f_values[ii]))
print(f_values)
CodePudding user response:
The function may be implemented this way.
import math
def f(n, f0):
if abs(f0) < 1e-6 or abs(math.log(f0)) < 1e-6:
return None
if n == 0:
return f0
return abs(math.log(f(n - 1, f0)))