Post Tagged mathem

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:

k_1 = f(x_n,y_n)
k_2 = f(x_n + h, y_n + h \ast k_1)
k = \frac{1}{2} (k_1 + k_2)
y_{n+1} = y_n +hk
x_{n+1} = x_n + h

consider the differential equation:
\dot{y} = y^2 : y(0)=1 \: and \: h =.2
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: