Euler's Method of Solving Ordinary Differential Equations

Nathan Collier, Autar Kaw
University of South Florida
United States of America
kaw@eng.usf.edu

Introduction

This worksheet demonstrates the use of Mathematica to illustrate Euler's method of solving ordinary differential equations. Euler's method of solving ordinary differential equations uses the derivative and value of the function at the boundary condition to project the location and value of the next point on the function.

Section 1: Input

The following is the data used in solving ordinary differential equations.

Differential equation of the form dy/dx=f(x,y).The exact ODE is shown below in the exact soultion section.Note thatMathematicaatemps to solve the ODE using its function.A solution may or may not be possible using this function.It should be noted that in this case,Euler's method is still valid,but there will be no exact solution for comparison.

f[x_,y_]:=y*x^2-1.2*y;

Boundary condition for x

x0=0.0;

Boundary condition for y

y0=1.0;

Value of x at which y is desired

xf=2.0;

This file will find the solution at x = xf by taking 5 steps. What follows is the exact solution and each individual step.

Section 2: Exact Solution

soln=DSolve[{f[x,y[x]]==y'[x],y[x0]==y0},y,x]

plot1=Plot[Evaluate[y[x] /. soln],{x,x0,xf},PlotStyle->{{Thickness[0.012],RGBColor[1,0,0]}}];

Section 3: Approximate Solution

Step 1

This worksheet shows how Euler's method is used over 5 steps to approximate the value of the solution function y(x) at x = xf. First we find the length of each segment.

h=(xf-x0)/5.0

The function f(x,y) is used to find the derivative at the initial condition. This derivative then is used to predict the value of the function at x[1] = x[0] + h. The first values used are the boundary conditions.

X[0]=x0;
Y[0]=y0;

The next x value is

X[1]=X[0]+h

The derivative is evaluated at the previous point (boundary condition) and the new y is found using Euler's method.

f[X[0],Y[0]]
Y[1]=Y[0]+f[X[0],Y[0]]*h

plot2=ListPlot[{{X[0],Y[0]},{X[1],Y[1]}},
PlotJoined->True,
PlotStyle->{Thickness[0.012],RGBColor[0,1,0]},
DisplayFunction->Identity]

Show[plot1,plot2]

Step 2

Theprocedure is repeated using the next x value (x[2] = x[1] + h) and the y value approximation in the previous step.

The next x value is

X[2]=X[1]+h

The derivative is evaluated at the previous point (boundary condition) and the new y is found using Euler's method.

f[X[1],Y[1]]
Y[2]=Y[1]+f[X[1],Y[1]]*h

plot3=ListPlot[{{X[0],Y[0]},{X[1],Y[1]},{X[2],Y[2]}},
PlotJoined->True,
PlotStyle->{Thickness[0.012],RGBColor[0,1,0]},
DisplayFunction->Identity]

Show[plot1,plot3]

Step 3

Theprocedure is repeated using the next x value (x[3] = x[2] + h) and the y value approximation in the previous step.

The next x value is

X[3]=X[2]+h

The derivative is evaluated at the previous point (boundary condition) and the new y is found using Euler's method.

f[X[2],Y[2]]
Y[3]=Y[2]+f[X[2],Y[2]]*h

plot4=ListPlot[{{X[0],Y[0]},{X[1],Y[1]},{X[2],Y[2]},{X[3],Y[3]}},
PlotJoined->True,
PlotStyle->{Thickness[0.012],RGBColor[0,1,0]},
DisplayFunction->Identity]

Show[plot1,plot4]

Step 4

Theprocedure is repeated using the next x value (x[4] = x[3] + h) and the y value approximation in the previous step.

The next x value is

X[4]=X[3]+h

The derivative is evaluated at the previous point (boundary condition) and the new y is found using Euler's method.

f[X[3],Y[3]]
Y[4]=Y[3]+f[X[3],Y[3]]*h

plot5=ListPlot[{{X[0],Y[0]},{X[1],Y[1]},{X[2],Y[2]},{X[3],Y[3]},{X[4],Y[4]}},
PlotJoined->True,
PlotStyle->{Thickness[0.012],RGBColor[0,1,0]},
DisplayFunction->Identity]

Show[plot1,plot5]

Step 4

Theprocedure is repeated using the next x value (x[5] = x[4] + h) and the y value approximation in the previous step.

The next x value is

X[5]=X[4]+h

The derivative is evaluated at the previous point (boundary condition) and the new y is found using Euler's method.

f[X[4],Y[4]]
Y[5]=Y[4]+f[X[4],Y[4]]*h

plot6=ListPlot[{{X[0],Y[0]},{X[1],Y[1]},{X[2],Y[2]},{X[3],Y[3]},{X[4],Y[4]},{X[5],Y[5]}},
PlotJoined->True,
PlotStyle->{Thickness[0.012],RGBColor[0,1,0]},
DisplayFunction->Identity]

Show[plot1,plot6]

References

Chapra & Canale, Numerical Methods for Engineering: With Programming Applications. 3rd ed. WCB/McGraw-Hill. 1998.

Conclusions

While Euler's Method is valid for approximating the solutions of ordinary differential equations, the use of the slope at one point to project the value at the next point is not very accurate. Note the values obtained as well as the true and relative true error at our desired point x = xf.

Exact Value

yf=(y/.First[soln])[xf]

Approximate Value

Y[5]

True Error

Et=Y[5]-yf

Absolute Relative True Error Percentage

et=Abs[Et/yf]*100

That is a lot of error! Here, our approximation would be better if we made our segments smaller and more in number. other methods use the same principle as Euler's Method: use some approximation of slope to find the value at a point a fixed distance away. The difference in methods is the function used to approximate the slope.