Let's work up gradually. We'll start with the function, then iteration, then visualization with some sort of dynamic construct.
I'll redefine your function f
like this:
MStep[c_][z_] := z^2 + c
The renaming is just for clarity. I'm thinking of this function not so much as a function with two arguments, but a function of one argument that is parameterized by some other value. Defining it in this form allows me to treat an expression like MStep[.5]
as a function in and of itself. For example
MStep[.5] /@ {1, 2, 3}
output: {1.5, 4.5, 9.5}
and
MStep[1 + 1 I] /@ {1 + 2 I, 2 - 3 I, 3}
output: {-2 + 5*I, -4 - 11*I, 10 + I}
This will be useful when we get to repeated function application. Let's try that now with NestList
NestList[MStep[1 + 1 I], 0, 5]
output: {0, 1 + I, 1 + 3*I, -7 + 7*I, 1 - 97*I, -9407 - 193*I}
Okay, now we need to work on using the slider control to pick a point. Evaluate the following and play with the slider control.
{Slider2D[Dynamic[coords]], Dynamic[coords]}
The output of a slider control is a 2-element list, i.e. it's the coordinates of a point. But we want a complex number. All we need to do is apply the head Complex:
{Slider2D[Dynamic[coords]], Dynamic[coords], Dynamic[Complex @@ coords]}
Okay, one more hurdle--we need to put this into a dynamic structure. I'm going to use Manipulate.
Manipulate[
With[
{pts =
Select[
NestList[MStep[Complex @@ cparam], Complex @@ coords, iterationcount],
Abs/*LessThan[2]]},
Grid[
{{Complex @@ coords, Complex @@ cparam},
{Column[pts],
Graphics[
{GrayLevel[.9], Disk[{0, 0}, 2],
Black, Line[ReIm[pts]]},
PlotRange -> {{-2, 2}, {-2, 2}}]}},
Alignment -> Top]],
Control[{{coords, {0, 0}, "initial pt"}, {-2, -2}, {2, 2}}],
Control[{{cparam, {0, 0}, "parameter c"}, {-2, -2}, {2, 2}}],
{{iterationcount, 5}, 2, 20, 1},
ControlPlacement -> Left]
A lot to unpack here. First off, most of the things I put inside the Grid are just for illustration/testing. When a control is specified to deal with a 2D variable, it automatically uses Slider2D. The Select is just removing elements with Abs greater than 2, since we know these will "blow up". You could incorporate that check into MStep if you wanted. The rest I'll let you explore and figure out on your own
c
andsmtng
, the_Complex
in your definition off
probably doesn't do what you think it does (e.g., try to evaluatef[1,2]
), etc. Given all of this, I would suggest just read some tutorials about Mathematica, e.g., the elementary introduction by Stephen Wolfram, otherwise, you could be constantly puzzled by very basic things. $\endgroup$f[{z_,c_}]:={z^2+c,c}; Nest[f,{1/2+I/4,3},2]
Check that result carefully. Does that correctly nest that function twice? Yes, it leaves{result,3}
when what you want is justresult
, but does it correctly calculateresult
? Then try bumping the 2 up to 3 and check it again. And then up to 4 and check it again. What that is trying to do is to correctly give and get a single list argument with f. If all that is correct then you try putting[[1]]
on the end of thatNest
to extract just the result from that list. Study all this carefully. $\endgroup$Nest
:ClearAll[f]; f[z_, c_] := z^2 + c; Nest[f[#, 3] &, 1/2 + I/4, 3]
$\endgroup$smthng
? $\endgroup$