2.10.4 Euler's method for systems of DEs

Finally, we show how the files in the examples/calculus subdirectory of the main SAGE_HOME directory can be used. (And please feel free to contribute your own by emailing them to the Sage Forum or to William Stein.)

In the next example, we will illustrate Euler's method for $ 2nd$ order ODE's. We first recall the basic idea. The goal is to find an approximate solution to the problem

$\displaystyle y'=f(x,y),   y(a)=c,$ (2.1)

where $ f(x,y)$ is some given function. We shall try to approximate the value of the solution at $ x=b$ , where $ b>a$ is given.

Recall from the definition of the derivative that

$\displaystyle y'(x)\cong \frac{y(x+h)-y(x)}{h},
$

$ h>0$ is a given and small. This and the DE together give $ f(x,y(x))\cong \frac{y(x+h)-y(x)}{h}$ . Now solve for $ y(x+h)$ :

$\displaystyle y(x+h)\cong y(x)+h\cdot f(x,y(x)).
$

If we call $ h\cdot f(x,y(x))$ the ``correction term'' (for lack of anything better), call $ y(x)$ the ``old value of $ y$ '', and call $ y(x+h)$ the ``new value of $ y$ '', then this approximation can be re-expressed

Tabular idea: Let $ n>0$ be an integer, which we call the step size. This is related to the increment by

$\displaystyle h=\frac{b-a}{n}.
$

This can be expressed simplest using a table.

$ x$ $ y$ $ hf(x,y)$
$ a$ $ c$ $ hf(a,c)$
$ a+h$ $ c+hf(a,c)$ ...
$ a+2h$ ...  
...    
$ b$ ??? xxx

The goal is to fill out all the blanks of the table but the xxx entry and find the ??? entry, which is the Euler's method approximation for $ y(b)$ .

The idea for systems of ODEs is similar.

Example: Numerically approximate $ z(t)$ at $ t=1$ using $ 4$ steps of Euler's method, where $ z''+tz'+z=0$ , $ z(0)=1$ , $ z'(0)=0$ .

One must reduce the 2nd order ODE down to a system of two first order DEs (using $ x=z$ , $ y=z'$ ) and apply Euler's method:

sage: t,x,y = PolynomialRing(RealField(10),3,"txy").gens()
sage: f = y; g = -x - y * t
sage: eulers_method_2x2(f,g, 0, 1, 0, 1/4, 1)
  t                x            h*f(t,x,y)                y       h*g(t,x,y)
  0                1               0.00000                0         -0.25000
1/4           1.0000             -0.062500         -0.25000         -0.23438
1/2          0.93750              -0.11719         -0.46875         -0.17578
3/4          0.82031              -0.15381         -0.61523        -0.089722
  1          0.66602              -0.16650         -0.66602          0.00000
Therefore, $ z(1)\approx 0.75$ .

Other files useful for illustrating basic aspects of differential equations os.environ['SAGE_ROOT'] + '/examples/calculus/field_plot2d.sage'. It has docstrings with many examples of the syntax.

See About this document... for information on suggesting changes.