How can I implement this pseudocode in Mathematica?
Algorithm:
(* input: f, x0, xmax, y0, n *)
h = (xmax-x0)/n;
u[1] = x0;
v[1] = y0;
For i = 2,n+1:
u[i] = u[i-1] + h;
v[i] = v[i-1] + h * f( u[i-1] , v[i-1] );
End for.
Return {{u[1],v[1]},{u[2],v[2]},...,{u[n+1],v[n+1]}};
This is my take on it but it's not working properly.
MyEuler[f, x0, xmax, y0, n] = Module[{},
h = (xmax - x0)/n;
u[1] = x0;
v[1] = y0;
For[i = 2, i <= n + 2, i++,
u[i] = u[i - 1] + h;
v[i] = v[i - 1] + h*f (u[i - 1], v[i - 1]);]
Table[{u[k], v[k]}, {k, 1, n + 1}]]
f (u[i - 1], v[i - 1])
should probably bef[u[i - 1], v[i - 1]]
iff
is a function. And why end your loop at atn+2
instead ofn+1
is a difference, but perhaps an unimportant one. Other than that, read a good introductory text on how to program Mathematica. You aren't taking good advantage of its features, other than its quasi-ability to pretend to be C or Java. $\endgroup$NDSolve
function and can be fixed by the optionMethod
? It is just to make sure that we understood you correctly. $\endgroup$