The goal is to find an approximate solution to the problem
eulers_method.sage file in the examples subdirectory.
The basic idea can also be explained ``algebraically''. Recall from the definition of the derivative in calculus 1 that
Tabular idea:
Let
be an integer, which we call the
step size. This is related to the increment
by
| ??? | 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
.
Geometric idea:
The basic idea can be easily expressed in geometric terms.
As in Euler's method, we know the solution must go through the
point
and we know its slope there is
. If we went out one step using the
tangent line approximation to the solution curve,
the approximate slope to the tangent line at
would be
.
The idea is that instead of using
as the slope of the line to get our first
approximation, use
.
The ``improved'' tangent-line approximation at
is:
Tabular idea:
The integer step size
is related to the increment
by
The improved Euler method can be expressed simplest using a table.
|
|
||
|
|
||
|
|
||
| ??? | xxx |
The goal is to fill out all the blanks of the
table but the xxx entry and find the ???
entry, which is the improved Euler's method
approximation for
.
The idea for systems of ODEs is similar. This is implemented below as well.
improved_eulers_method
implements Improved Euler's method for
finding numerically the solution of the 1st order
ODE Here is how the syntax of the program works:
sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU')
sage: x,y = PolynomialRing(RR,2).gens()
sage: improved_eulers_method(5*x+y-5,0,1,1/2,1)
x y (h/2)*(f(x,y)+f(x+h,y+h*f(x,y))
0 1 -1.87
1/2 -0.875 -1.37
1 -2.25 -0.687
sage: x,y=PolynomialRing(QQ,2).gens()
sage: improved_eulers_method(5*x+y-5,0,1,1/2,1)
x y (h/2)*(f(x,y)+f(x+h,y+h*f(x,y))
0 1 -15/8
1/2 -7/8 -95/64
1 -151/64 -435/512
eulers_method
implements Euler's method for
finding numerically the solution of the 1st order
ODE Here is how the syntax of the program works:
sage: x,y=PolynomialRing(QQ,2).gens()
sage: eulers_method(5*x+y-5,0,1,1/2,1)
x y h*f(x,y)
0 1 -2
1/2 -1 -7/4
1 -11/4 -11/8
sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU')
sage: x,y=PolynomialRing(RR,2).gens()
sage: eulers_method(5*x+y-5,0,1,1/2,1)
x y h*f(x,y)
0 1 -2.00
1/2 -1.00 -1.75
1 -2.75 -1.37
eulers_method_2x2
implements Euler's method for
finding numerically the solution of the 1st order
system of two ODEs
Here is how the syntax of the program works:
To approximate
using 3 steps, where
sage: t, x, y = PolynomialRing(QQ,3).gens()
sage: f = x+y+t; g = x-y
sage: eulers_method_2x2(f,g, 0, 0, 0, 1/3, 1)
t x h*f(t,x,y) y h*g(t,x,y)
0 0 0 0 0
1/3 0 1/9 0 0
2/3 1/9 7/27 0 1/27
1 10/27 38/81 1/27 1/9
sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU')
sage: t,x,y=PolynomialRing(RR,3).gens()
sage: f = x+y+t; g = x-y
sage: eulers_method_2x2(f,g, 0, 0, 0, 1/3, 1)
t x h*f(t,x,y) y h*g(t,x,y)
0 0 0.000 0 0.000
1/3 0.000 0.125 0.000 0.000
2/3 0.125 0.282 0.000 0.0430
1 0.407 0.563 0.0430 0.141
To numerically approximate y(1), where
sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU')
sage: t, y1, y2=PolynomialRing(RR,3).gens()
sage: f = y2; g = (y1-y2)/(1+t^2)
sage: eulers_method_2x2(f,g, 0, 1, -1, 1/4, 1)
t x h*f(t,x,y) y h*g(t,x,y)
0 1 -0.250 -1 0.500
1/4 0.750 -0.125 -0.500 0.282
1/2 0.625 -0.0546 -0.218 0.188
3/4 0.625 -0.00781 -0.0312 0.110
1 0.625 0.0196 0.0782 0.0704
To numerically approximate y(1), where
sage: t,x,y=PolynomialRing(RR,3).gens()
sage: f = y2; g = -y1-y2*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.000 0 -0.250
1/4 1.00 -0.0625 -0.250 -0.234
1/2 0.938 -0.117 -0.468 -0.171
3/4 0.875 -0.156 -0.625 -0.101
1 0.750 -0.171 -0.687 -0.0156