Improved Euler Method in Mathematica.
Tuesday, 09 September 2008
So let us take a look at the case where we need to obtain a numerical approximation to a ODE. Just go to wikipedia to find some theory of when and why this works…..i will show you the algorithm implemented in Mathematica. The general solution looks like this:





consider the differential equation:

Mathematica Code:
Clear[x, y, x0, y0, h, n, k, k1, k2, tbl] f[x_, y_] = y^2; {x, y, h} = {0, 1, .2}; tbl = {{x, y}}; n = 6; Do[ k1 = f[x, y]; k2 = f[x + h, y + h*k1]; k = (k1 + k2)/2; y = y + h*k; x = x + h; AppendTo[tbl, {x, y}] , {i, 1, n}]
So, your Mathematica notebook should look something like this.

While you are at it, you might as well plot the data: