# Obtain numerical values with NestList on a custom function f?

Posted 2 years ago
2274 Views
|
7 Replies
|
0 Total Likes
|
 I define a function f[z_, c_] := (z^2) + c I evaluate it: In[9]:= f[3 + I, -1] Out[9]= 7 + 6 I Now, I want to iterate it 5 times starting from the value 1 : I do: NestList[f, 1, 5]But I don't see a list of numeric values, instead I see: {1, f[1], f[f[1]], f[f[f[1]]], f[f[f[f[1]]]], f[f[f[f[f[1]]]]]}If I try 1.0 I still get: In[15]:= NestList[f, 1.0, 5] Out[15]= {1., f[1.], f[f[1.]], f[f[f[1.]]], f[f[f[f[1.]]]], f[f[f[f[f[1.]]]]]} How can I tell Mathematica I want the values?for instance, like I get with the Cosine function I do get the values In[7]:= NestList[Cos, 1.0, 10] Out[7]= {1., 0.540302, 0.857553, 0.65429, 0.79348, 0.701369, 0.76396, \ 0.722102, 0.750418, 0.731404, 0.744237} 
7 Replies
Sort By:
Posted 2 years ago
 Found the problem f is a function of two variables.So the question now is: how can I iterate over a function of two variables?
Posted 2 years ago
 Can you adapt something like this? f[{z_,c_}]:={z^2,z-c}; f[{3+I,-1}] NestList[f,{3+I,-1},5] which accepts a list of two items and returns a list of two items.
Posted 2 years ago
 I followed your suggestion and I now write my function as follows: f[{z_, c_}] := z^2 + c; this yields 7 + 6 I for f[{3 + I, -1}] and In[64]:= NestList[f, {3 + I, -1}, 5] Out[64]= {{3 + I, -1}, 7 + 6 I, 13 + 84 I, -6887 + 2184 I, 42660913 - 30082416 I, 915001745596513 - 2566686663611616 I} So it looks like it is able to iterate now. Thanks! By the way, why does the { } solves the problem?
Posted 2 years ago
 Actually, I was wrong. Here is the correct way to get the iteration (still based on your answer): f[{z_, c_}] := {z^2 + c, c} and then NestList[f, {0, -1}, 5] which gives {{0, -1}, {-1, -1}, {0, -1}, {-1, -1}, {0, -1}, {-1, -1}} where the first argument is the iterated value of the function, and the second argument is just the constant c
Posted 2 years ago
 Another way to do it ClearAll[f] f[c_][z_] := z^2 + c (* c = -1, start with 0 *) NestList[f[-1], 0, 5] (* {0, -1, 0, -1, 0, -1} *) (* c = -1, start with 3 + I *) NestList[f[-1], 3 + I, 5] (* {3 + I, 7 + 6 I, 12 + 84 I, -6913 + 2016 I, 43725312 - 27873216 I, 1134986739314687 - 2437530132086784 I} *) 
 And yet another way to do it: with c=-1, and starting at 0: RecurrenceTable[{z[n + 1] == (z[n])^2 - 1, z[1] == 1}, z, {n, 1, 5}] which gives {0, -1, 0, -1, 0}