1 Euler's Method With Python
1 Euler's Method With Python
1
Using this tangent line approximation, we approximate that
yj+1 := xf (xj , yj ) + yj .
and create an account. Then create a new project call it whatever you want, such as
Eulers Method. Click on the created project and create a new Sage worksheet by selecting
the appropriate item out of the drop-down menu. You should now be presented with a page
which looks like this one.
2
Now in the big blank part of the page, were going to enter our first bit of code. The
code is
Listing 1: Importing External Libraries
import numpy as np
from m a t p l o t l i b import p y p l o t as p l t
This bit of code includes some fancy packages and things that other people have written for
us. In particular, numpy is a huge library of routines for numerical calculation in python,
and matplotlib has plotting packages for making pretty graphs. Under this bit of code, we
will find our numerical approximation to an initial value problem. For the purposes of this
demonstration, lets suppose that our initial value problem is
y 0 = y + sin(x), y(0) = 1.
Lets use Eulers method to approximate the value of the function in the interval [0, 10] with
101 points. Then x0 = 0, y0 = 1, xf = 10, n = 101, and x = (xf x0 )/(n 1) = 0.1. The
following code tells the computer this information.
Listing 2: Defining Basic Data
x0 = 0
y0 = 1
x f = 10
n = 101
d e l t a x = ( xfx0 ) / ( n1)
We next want to create a vector of length 101 whose entries are our x-values. To do this, we
enter the following code
Listing 3: Defining x-values
x = np . l i n s p a c e ( x0 , xf , n )
Now the computer has a vector called x, given by x = [0.0, 0.1, 0.2, . . . , 10.0]. The next thing
we want to do is tell the computer how to generate the y-values. The first thing we do is
create an array to store the y-values in.
Listing 4: Initializing Array for y-values
y = np . z e r o s ( [ n ] )
This creates an array y with n entries that are all 0.0. The next thing we want to do is fix
this so that the entries are the estimates given by the (forward) Euler method. To do this,
we use a for loop, which is a way of telling the computer to repeat an instruction a certain
number of times:
Listing 5: For Loop for Eulers Method
y [ 0 ] = y0
f o r i i n range ( 1 , n ) :
y [ i ] = d e l t a x (y [ i 1] + np . s i n ( x [ i ] ) ) + y [ i 1]
3
The first line sets the first entry of the array y to the value y0. The loop then sets the ith
entry of the array to the (i 1)th entry plus x times our estimate of the derivative from
the differential equation. The array y now has the estimates of the solution from Eulers
method.
We can also use a for loop to print out the data that weve generated in a pretty way
Listing 6: Printing the Data
f o r i i n range ( n ) :
print (x [ i ] , y [ i ])
Finally, to make a pretty graph of our data, we can use the plot function from matplotlib.
Listing 7: Plotting the Solution
plt . plot (x , y , o )
p l t . x l a b e l ( Value o f x )
p l t . y l a b e l ( Value o f y )
p l t . t i t l e ( Approximate S o l u t i o n with Forward Euler s Method )
p l t . show ( )
In summary, our code is:
Listing 8: Summary of Code
import numpy as np
from m a t p l o t l i b import p y p l o t as p l t
x0 = 0
y0 = 1
x f = 10
n = 101
d e l t a x = ( xfx0 ) / ( n1)
x = np . l i n s p a c e ( x0 , xf , n )
y = np . z e r o s ( [ n ] )
y [ 0 ] = y0
f o r i i n range ( 1 , n ) :
y [ i ] = d e l t a x (y [ i 1] + np . s i n ( x [ i ] ) ) + y [ i 1]
f o r i i n range ( n ) :
print (x [ i ] , y [ i ])
plt . plot (x , y , o )
p l t . x l a b e l ( Value o f x )
p l t . y l a b e l ( Value o f y )
p l t . t i t l e ( Approximate S o l u t i o n with Forward Euler s Method )
p l t . show ( )
4
To run the code, we hit the green arrow that says Run at the top of the screen.
The result should be a bunch of data which is spit out plus a pretty graph a the end