After read Michael answer, code use NestList
to compute and feed in numerical integration. I want to learn this idea into my code.
In the NestList
document, this function have three parameter, eg. f, expr, n
.if define f
, NestList
seems to map expr to f[expr]
,'f[f[expr]]',... n times.
So I refer Michael code,
deltat = 1*^-3;
integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /.
FindRoot[{x[1] - x[0] == deltat,
y[1] - y[0] == deltat}, {{x[0], x0}, {y[0], y0}}];
integrator[x, y, {0, 0}]
And
NestList[integrator[x, y, {y[0], y[0]}], {x[0], y[0]}(* condition *), 4]
I want calculate 4 times nesting, at beginning, $x0,y0$ is the initial value, and call integrator
to get $x1,y1$, and repeat, $x1,y1$ as initial point, call integrator
again, and find root again. Above code's output is not what I expected.
I still don't understand NestList
enough, I seem to only know this form, but don’t know how to use it for specific problems.
Update(I still have a bugs with my code, sad, Others gave the right answer, but I still failed)
ClearAll[integrator, myintegrator, stepintegrator];
deltat = 1*^-3;
integrator[x_, y_, {x0_, y0_}] := {x[1], y[1]} /.
FindRoot[{x[1] - x[0] == deltat,
y[1] - y[0] == deltat}, {{x[0], x0}, {y[0], y0}}];
(*integrator[x,y,{0,0}]*)
myintegrator[x_, y_][x0_,
y0_] := {Sequence @@ integrator[x, y, {x0, y0}]}
stepintegrator = myintegrator[x, y];
NestList[stepintegrator, {0, 0}, 2]
f[{x_, y_}] := {x + 1, y + 1};
is a function that takes a list and returns a list. Execute:NestList[f, {x, y}, 4]
to see the result. $\endgroup$myIntegrator[param1, param2,..][{x0, y0}]
to return a point in the trajectory. (note that this is of the formf[a][b]
—f[a]
is a function!) He then takesstepIntegrator = myIntegrator[param1, param2,..]
, which is, as mentioned, a function.NestList
always takes a function as its first argument; see the docs. You should useintegrator[x_, y_][{x0_, y0_}]
andintegrator[x,y]
inNestList
instead. $\endgroup$NestList
works.NestList
can keep applying the function at the last result. $\endgroup$