So this is my code for Euler's method to approximate ode's. f_ being the ode, t0_ the start of the interval, tf_ the end, y0_ initial condition, h_ the step size, and y_ is a given exact solution.
euler[f_, t0_, y0_, tf_, h_, y_] := Module[{},
t[0] = t0;
w[0] = y0;
n = Rationalize[(tf - t0)/h];
Do[
w[i] = w[i - 1] + h*f[t[i - 1], w[i - 1]];
t[i] = t[i - 1] + h,
{i, 1, n}];
Print["Euler's Method Results:"];
TableForm[
Table[{i, t[i], w[i], y[t[i]], Abs[w[i] - y[t[i]]]}, {i, 0, n}],
TableHeadings -> {None, {"i", "ti",
"Euler's \nMethod \nwi","Exact \nSolution\ny(ti)",
"Absolute \nError \n|wi-y(ti)|\n"}},
TableAlignments -> Center, TableSpacing -> {2, 5}
]
]
The code works fine so I'm not worried about that, but I can't figure out how to plot the Euler's method solution. I expect it to be something like
Plot[w[i], {i, 0, n}, PlotLabel -> "Exact solution",AxesLabel -> {"t", "y"}]
but I can't seem to make it work.
euler[f_, t0_, y0_, tf_, h_] := Module[{n, t, w}, t[0] = t0; w[0] = y0; n = Rationalize[(tf - t0)/h]; Do[w[i] = w[i - 1] + h f[t[i - 1], w[i - 1]]; t[i] = t[i - 1] + h, {i, 1, n}]; ListLinePlot[Table[t[i], w[i], {i, 0, n}]]]
. $\endgroup$