SlideShare a Scribd company logo
1 of 12
Download to read offline
Applications of MATLAB:
Ordinary Differential Equations (ODE)
David Houcque
Robert R. McCormick School of Engineering and
Applied Science - Northwestern University
2145 Sheridan Road
Evanston, IL 60208-3102
Abstract
Textbooks on differential equations often give the impression that most differential
equations can be solved in closed form, but experience does not bear this out. It
remains true that solutions of the vast majority of first order initial value problems
cannot be found by analytical means. Therefore, it is important to be able to approach
the problem in other ways. Today there are numerous methods that produce numerical
approximations to solutions of differential equations. Here, we introduce the oldest and
simplest such method, originated by Euler about 1768. It is called the tangent line
method or the Euler method. It uses a fixed step size h and generates the approximate
solution.
The purpose of this paper is to show the details of implementing a few steps of
Euler’s method, as well as how to use built-in functions available in MATLAB (2005)
[1]. In the first part, we use Euler methods to introduce the basic ideas associated with
initial value problems (IVP). In the second part, we use the Runge-Kutta method pre-
sented together with the built-in MATLAB solver ODE45. The implementations that
we develop in this paper are designed to build intuition and are the first step from
textbook formula on ODE to production software.
Key words: Euler’s methods, Euler forward, Euler modified, Euler backward, MAT-
LAB, Ordinary differential equation, ODE, ode45.
1 Introduction
The dynamic behavior of systems is an important subject. A mechanical system involves displace-
ments, velocities, and accelerations. An electric or electronic system involves voltages, currents,
and time derivatives of these quantities. An equation that involves one or more derivatives of the
1
unknown function is called an ordinary differential equation, abbreviated as ODE. The order of the
equation is determined by the order of the highest derivative. For example, if the first derivative
is the only derivative, the equation is called a first-order ODE. In the same way, if the highest
derivative is second order, the equation is called a second-order ODE.
The problems of solving an ODE are classified into initial-value problems (IVP) and boundary-
value problems (BVP), depending on how the conditions at the endpoints of the domain are spec-
ified. All the conditions of an initial-value problem are specified at the initial point. On the other
hand, the problem becomes a boundary-value problem if the conditions are needed for both initial
and final points. The ODE in the time domain are initial-value problems, so all the conditions are
specified at the initial time, such as t = 0 or x = 0. For notations, we use t or x as an independent
variable. Some literatures use t as time for independent variable.
It is important to note that our focus here is on the practical use of numerical methods in
order to solve some typical problems, not to present any consistent theoretical background. There
are many excellent and exhaustive texts on these subjects that may be consulted. For example, we
would recommend Edwards and Penny (2000) [2], Boyce and DiPrima (2001) [3], Coombes et al.
(2000) [4], Van Loan (1997) [5], Nakamura (2002) [6], Moler (2004) [7], and Gilat (2004) [8].
2 Numerical methods
Numerical methods are commonly used for solving mathematical problems that are formulated in
science and engineering where it is difficult or even impossible to obtain exact solutions. Only a
limited number of differential equations can be solved analytically. Numerical methods, on the
other hand, can give an approximate solution to (almost) any equation. An ordinary differential
equation (ODE) is an equation that contains an independent variable, a dependent variable, and
derivatives of the dependent variable. Literal implementation of this procedure results in Euler’s
method, which is, however, not recommended for any practical use. There are other methods
more sophisticated than Euler’s. Among them, there are three major types of practical numerical
methods for solving initial value problems for ODEs: (i) Runge-Kutta methods, (ii) Burlirsch-Stoer
method, and (iii) predictor-corrector methods. We will present these three approaches on another
occasion. Now, we are interested to talk about Euler’s methods.
2.1 EULER methods
The Euler methods are simple methods of solving first-order ODE, particularly suitable for quick
programming because of their great simplicity, although their accuracy is not high. Euler methods
include three versions, namely,
• forward Euler method
• modified Euler method
• backward Euler method
2.1.1 Forward Euler method
The forward Euler method for y′ = f(y, x) is derived by rewriting the forward difference approxi-
mation,
(yn+1 − yn)/h ≈ y′
n (1)
to
yn+1 = yn + hf(yn, xn) (2)
where y′
n = f(yn, xn) is used. In order to advance time steps, Eq. 2 is recursively applied as
y1 = y0 + hy′
0
y1 = y0 + hf(y0, x0)
y2 = y1 + hf(y1, x1) (3)
y3 = y2 + hf(y2, x2)
.
.
.
yn = yn−1 + hf(yn−1, xn−1)
2.1.2 Modified Euler method
First, the modified Euler method is more accurate than the forward Euler method. Second, it is
more stable. It is derived by applying the trapezoidal rule to the solution of y′ = f(y, x)
yn+1 = yn +
h
2
[f(yn+1, xn+1) + f(yn, xn)] (4)
2.1.3 Backward Euler Method
The backward Euler method is based on the backward difference approximation and written as
yn+1 = yn + hf(yn+1, xn+1) (5)
The accuracy of this method is quite the same as that of the forward Euler method.
2.2 Steps for MATLAB implementation
The purpose of using an example is to show you the details of implementing the typical steps of
Euler’s method, so that it will be clear exactly what computations are being executed. For some
reasons, MATLAB does not include Euler functions. Therefore, if you really need one, you have to
code by yourselves. However, MATLAB has very sophisticated ones using Runge-Kutta algorithms.
We will show how to use one of them in the next section.
2.2.1 Basic steps
The typical steps of Euler’s method are given below.
Step 1. define f(x, y)
Step 2. input initial values x0 and y0
Step 3. input step sizes h and number of steps n
Step 4. calculate x and y:
for i=1:n
x=x+h
y=y+hf(x,y)
end
Step 5. output x and y
Step 6. end
2.2.2 Example
As an application, consider the following initial value problem
dy
dx
=
x
y
, y(0) = 1 (6)
which was chosen because we know the analytical solution and we can use it for check. Its exact or
analytical solution is found to be
y(x) =
p
x2 + 1 (7)
Therefore, we will be able to compare the approximate solutions and the exact solution.
Here we wish to approximate y(0.3) using the Euler’s methods with step sizes h = 0.1 and
h = 0.05. We find by hand-calculation,
x0 = 0, x1 = 0.1, x2 = 0.2, x3 = 0.3
y0 = 1
y1 = y0 + hf(x0, y0) = y0 + hx0/y0 = 1
y2 = y1 + hx1/y1 = 1.01
y3 = y2 + hx2/y2 = 1.0298.
Since y(0.3) =
p
(0.3)2 + 1 = 1.044030, we find that
Error =
|y(0.3) − y3|
y(0.3)
× 100 = 1.36%
Similarly, for the step size h = 0.05, we find that the error is
Error =
|y(0.3) − y6|
y(0.3)
× 100 = 0.67%
2.2.3 MATLAB codes
• Step 1: Create user-defined function files: euler forward.m, euler modified.m,
and euler backward.m.
function [x,y]=euler_forward(f,xinit,yinit,xfinal,n)
% Euler approximation for ODE initial value problem
% Euler forward method
% File prepared by David Houcque - Northwestern U. 5/11/2005
% Calculation of h from xinit, xfinal, and n
h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)];
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x(i),y(i));
end
end
function [x,y]=euler_modified(f,xinit,yinit,xfinal,n)
% Euler approximation for ODE initial value problem
% Euler modified method
% File prepared by David Houcque - Northwestern U. - 5/11/2005
% Calculation of h from xinit, xfinal, and n
h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)];
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
ynew=y(i)+h*f(x(i),y(i));
y(i+1)=y(i)+(h/2)*(f(x(i),y(i))+f(x(i+1),ynew));
end
end
function [x,y]=euler_backward(f,xinit,yinit,xfinal,n)
% Euler approximation for ODE initial value problem
% Euler backward method
% File prepared by David Houcque - Northwestern U. - 5/11/2005
% Calculation of h from xinit, xfinal, and n
h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x=[xinit zeros(1,n)];
y=[yinit zeros(1,n)];
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
ynew=y(i)+h*(f(x(i),y(i)));
y(i+1)=y(i)+h*f(x(i+1),ynew);
end
end
• Step 2: Solve the problem by putting data and called functions into a script file called
main1.m:
% Script file: main1.m
% The RHS of the differential equation is defined as
% a handle function
% File prepared by David Houcque - Northwestern U. - 5/11/2005
f=@(x,y) x./y;
% Calculate exact solution
g=@(x) sqrt(x.^2+1);
xe=[0:0.01:0.3];
ye=g(xe);
% Call functions
[x1,y1]=euler_forward(f,0,1,0.3,6);
[x2,y2]=euler_modified(f,0,1,0.3,6);
[x3,y3]=euler_backward(f,0,1,0.3,6);
% Plot
plot(xe,ye,’k-’,x1,y1,’k-.’,x2,y2,’k:’,x3,y3,’k--’)
xlabel(’x’)
ylabel(’y’)
legend(’Analytical’,’Forward’,’Modified’,’Backward’)
axis([0 0.3 1 1.07])
% Estimate errors
error1=[’Forward error: ’ num2str(-100*(ye(end)-y1(end))/ye(end)) ’%’];
error2=[’Modified error: ’ num2str(-100*(ye(end)-y2(end))/ye(end)) ’%’];
error3=[’Backward error: ’ num2str(-100*(ye(end)-y3(end))/ye(end)) ’%’];
error={error1;error2;error3};
text(0.04,1.06,error)
• Step 3: Compare the results.
The calculated results are displayed in the graphical form below. Reasonably good results
are obtained even for a moderately large step size and the approximation can be improved
by decreasing the step size. According to the results (Figure 1) and Table 1, forward and
backward approaches give identically the same results (less than 1% of error), while modified
method give very good result when compared with the exact solution.
h Forward Modified Backward
0.05 0.67% 0.04% 0.67%
Table 1: Comparison of exact solution with Euler methods
2.3 Using built-in function
MATLAB has several different functions (built-ins) for the numerical solution of ordinary differ-
ential equations (ODE). In this section, however, we will present one of them. We will also give
an example how to use it, instead of writing our own MATLAB codes as we did in the first part.
The basic steps, previously defined, are still typically the same. These solvers can be used with
the following syntax:
[x,y] = solver(@odefun,tspan,y0)
0 0.05 0.1 0.15 0.2 0.25 0.3
1
1.01
1.02
1.03
1.04
1.05
1.06
1.07
x
y
Forward error: −0.6751%
Modified error: 0.0003983%
Backward error: 0.67244%
Analytical
Forward
Modified
Backward
Figure 1: Comparison of exact solution with Euler methods
solver is the solver you are using, such as name, ode45 or ode23. odefun is the function that
defines the derivatives, so odefun defines y′ as a function of the independent parameter (typically
x or t) as well as y and other parameters. tspan a vector that specifies the interval of the solution
(e.g., [t0,tf]). y0 is the initial value of y. [x,y] is the output, which is the solution of the ODE.
2.3.1 Runge-Kutta methods
There are many variants of the Runge-Kutta method, but the most widely used one is the following.
Given:
y′
= f(x, y) (8)
y(xn) = yn
we compute in turn
k1 = hf(xn, yn)
k2 = hf(xn +
h
2
, yn +
k1
2
)
k3 = hf(xn +
h
2
, yn +
k2
2
) (9)
k4 = hf(xn + h, yn + k3)
yn+1 = yn +
1
6
(k1 + 2k2 + 2k3 + k4)
2.3.2 Using ode45
ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair [9]. That means
the numerical solver ode45 combines a fourth order method and a fifth order method, both of which
are similar to the classical fourth order Runge-Kutta (RK) method discussed above. The modified
RK varies the step size, choosing the step size at each step in an attempt to achieve the desired
accuracy. Therefore, the solver ode45 is suitable for a wide variety of initial value problems in
practical applications. In general, ode45 is the best function to apply as a “first try” for most
problems. Table 2 lists ODE solvers, which are MATLAB built-in functions. A short description
of each solver is included.
Note - It is important to note that in MATLAB 7.0 (R14), latest version, it is preferred to have
odefun in the form of a function handle. For example, it is recommended to use the following
syntax,
ode45(@xdot,tspan,y0)
rather than
Solver Accuracy Description
ode45 Medium This should be the first solver you try
ode23 Low Less accurate than ode45
ode113 Low to high For computationally intensive problems
ode15s Low to medium Use if ode45 failed
Table 2: Some MATLAB ODE solvers
ode45(’xdot’,tspan,y0)
Note the use of @xdot and ’xdot’. Use function handles to pass any function that defines quantities
the MATLAB solver will compute, in particular for simple functions.
On the other hand, it is also important to remember that complicated differential equations
should be written an M-file instead of using inline command or function handle.
Following is an example of an ordinary differential equation using a MATLAB ODE solver.
First, let’s create a script file, called main2.m, as follows:
% Script file: main2.m
% The RHS of the differential equation is defined as
% a function handle.
% File prepared by David Houcque - Northwestern U. - 5/11/2005
f=@(x,y) x./y;
% Calculate exact solution
g=@(x) sqrt(x.^2+1);
xe=[0:0.01:0.3];
ye=g(xe);
% Call function
[x4,y4]=ode45(f,[0,0.3],1);
% Plot
plot(xe,ye,’k-’,x4,y4,’k:’)
xlabel(’x’)
ylabel(’y’)
legend(’Analytical’,’ode45’)
axis([0 0.3 1 1.05])
Here, we use the same data as defined in the first part for Euler’s methods. The initial conditions
and the time steps are the same as before.
The integration proceeds by steps, taken to the values specified in tspan. Note that the step
size (the distance between consecutive elements of tspan) does not have to be uniform.
A plot comparing the computed y versus x is shown in Figure 2. According to the plot, the
0 0.05 0.1 0.15 0.2 0.25 0.3
1
1.005
1.01
1.015
1.02
1.025
1.03
1.035
1.04
1.045
1.05
x
y
Analytical
ode45
Figure 2: Comparison of exact solution with ode45 solution
calculated results using the built-in function (ode45) give a very good result when compared with
the analytical solution.
Additional on all of these solvers can be found in the online help. For addition information
on numerical methods, we refer to Shampine (1994) [10] and Forsythe et al. (1977) [11].
3 Conclusion
The above plots show the results obtained from different algorithms. Consequently, we can see
better that Runge-Kutta algorithm is even more accurate at large step size (h = 0.1) than Euler
algorithms at small step size (h = 0.05). One can easily adapt these MATLAB codes as needed for
a different type of problem. In using numerical procedure, such as Euler’s method, one must always
keep in mind the question of whether the results are accurate enough to be useful. In the preceding
examples, the accuracy of the numerical results could be ascertained directly by a comparison with
the solution obtained analytically. Of course, usually the analytical solution is not always available
to compare.
Acknowledgement- The author is sincerely grateful to Associate Dean Steve Carr
for his support. Thanks also go to Professor MacIver for his support and discussions.
He is grateful to EA3 students (Honors Section) for their bright ideas and suggestions.
References
[1] The MathWorks Inc. MATLAB: SP2 R14. The MathWorks Inc., 2005.
[2] C. H. Edwards and D. E. Penny. Differential Equations and Boundary Value Problems: Com-
puting and Modeling. Prentice Hall, 2000.
[3] W. E. Boyce and R. C. DiPrima. Elementary Differential Equations and Boundary Value
Problems. John Wiley and Sons, 2001.
[4] K. R. Coombes, B. R. Hunt, R. L. Lipsman, J. E. Osborn, and G. J. Stuck. Differential
Equations with MATLAB. John Wiley and Sons, 2000.
[5] C. F. Van Loan. Introduction to Scientific Computing. Prentice Hall, 1997.
[6] S. Nakamura. Numerical Analysis with MATLAB. Prentice Hall, 2002.
[7] C. B. Moler. Numerical Computing with MATLAB. Siam, 2004.
[8] A. Gilat. MATLAB: An introduction with Applications. John Wiley and Sons, 2004.
[9] J. R. Dormand and P. J. Prince. A family of embedded Runge-Kutta formulae. J. Comp.
Appl. Math, 6:19–26, 1980.
[10] L. F. Shampine. Numerical Solution of Ordinary Equations. Chapman and Hall, 1994.
[11] G. Forsythe, M. Malcolm, and C. Moler. Computer Methods for Mathematical Computations.
Prentice-Hall, 1977.

More Related Content

Similar to Applications Of MATLAB Ordinary Differential Equations (ODE

numericalmethods.pdf
numericalmethods.pdfnumericalmethods.pdf
numericalmethods.pdfShailChettri
 
Applied numerical methods lec12
Applied numerical methods lec12Applied numerical methods lec12
Applied numerical methods lec12Yasser Ahmed
 
Comparative Analysis of Different Numerical Methods of Solving First Order Di...
Comparative Analysis of Different Numerical Methods of Solving First Order Di...Comparative Analysis of Different Numerical Methods of Solving First Order Di...
Comparative Analysis of Different Numerical Methods of Solving First Order Di...ijtsrd
 
Euler and improved euler method
Euler and improved euler methodEuler and improved euler method
Euler and improved euler methodSohaib Butt
 
Convergence of Homotopy Perturbation
Convergence of Homotopy PerturbationConvergence of Homotopy Perturbation
Convergence of Homotopy Perturbationnadia naseem
 
Homotopy Perturbation Method
Homotopy Perturbation MethodHomotopy Perturbation Method
Homotopy Perturbation MethodSOUMYADAS835019
 
Numerical Solutions of Burgers' Equation Project Report
Numerical Solutions of Burgers' Equation Project ReportNumerical Solutions of Burgers' Equation Project Report
Numerical Solutions of Burgers' Equation Project ReportShikhar Agarwal
 
Elzaki transform homotopy perturbation method for
Elzaki transform homotopy perturbation method forElzaki transform homotopy perturbation method for
Elzaki transform homotopy perturbation method foreSAT Publishing House
 
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docxCALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docxRAHUL126667
 
The_variational_iteration_method_for_solving_linea.pdf
The_variational_iteration_method_for_solving_linea.pdfThe_variational_iteration_method_for_solving_linea.pdf
The_variational_iteration_method_for_solving_linea.pdfP Ramana
 
On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...
On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...
On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...BRNSS Publication Hub
 

Similar to Applications Of MATLAB Ordinary Differential Equations (ODE (20)

numericalmethods.pdf
numericalmethods.pdfnumericalmethods.pdf
numericalmethods.pdf
 
Applied numerical methods lec12
Applied numerical methods lec12Applied numerical methods lec12
Applied numerical methods lec12
 
ODE.pdf
ODE.pdfODE.pdf
ODE.pdf
 
Comparative Analysis of Different Numerical Methods of Solving First Order Di...
Comparative Analysis of Different Numerical Methods of Solving First Order Di...Comparative Analysis of Different Numerical Methods of Solving First Order Di...
Comparative Analysis of Different Numerical Methods of Solving First Order Di...
 
MSME_ ch 5.pptx
MSME_ ch 5.pptxMSME_ ch 5.pptx
MSME_ ch 5.pptx
 
Matlab lab.pdf
Matlab lab.pdfMatlab lab.pdf
Matlab lab.pdf
 
Euler and improved euler method
Euler and improved euler methodEuler and improved euler method
Euler and improved euler method
 
Euler Method
Euler MethodEuler Method
Euler Method
 
Presentation5
Presentation5Presentation5
Presentation5
 
Convergence of Homotopy Perturbation
Convergence of Homotopy PerturbationConvergence of Homotopy Perturbation
Convergence of Homotopy Perturbation
 
Euler's Method.pdf
Euler's Method.pdfEuler's Method.pdf
Euler's Method.pdf
 
Euler's Method.pdf
Euler's Method.pdfEuler's Method.pdf
Euler's Method.pdf
 
Euler Method Details
Euler Method Details Euler Method Details
Euler Method Details
 
Homotopy Perturbation Method
Homotopy Perturbation MethodHomotopy Perturbation Method
Homotopy Perturbation Method
 
Ch02 7
Ch02 7Ch02 7
Ch02 7
 
Numerical Solutions of Burgers' Equation Project Report
Numerical Solutions of Burgers' Equation Project ReportNumerical Solutions of Burgers' Equation Project Report
Numerical Solutions of Burgers' Equation Project Report
 
Elzaki transform homotopy perturbation method for
Elzaki transform homotopy perturbation method forElzaki transform homotopy perturbation method for
Elzaki transform homotopy perturbation method for
 
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docxCALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
CALIFORNIA STATE UNIVERSITY, NORTHRIDGEMECHANICAL ENGINEERIN.docx
 
The_variational_iteration_method_for_solving_linea.pdf
The_variational_iteration_method_for_solving_linea.pdfThe_variational_iteration_method_for_solving_linea.pdf
The_variational_iteration_method_for_solving_linea.pdf
 
On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...
On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...
On the Numerical Fixed Point Iterative Methods of Solution for the Boundary V...
 

More from Justin Knight

My Summer Narrative Writing For The Beginning O
My Summer Narrative Writing For The Beginning OMy Summer Narrative Writing For The Beginning O
My Summer Narrative Writing For The Beginning OJustin Knight
 
Writing Paper With Drawing Space. Online assignment writing service.
Writing Paper With Drawing Space. Online assignment writing service.Writing Paper With Drawing Space. Online assignment writing service.
Writing Paper With Drawing Space. Online assignment writing service.Justin Knight
 
College Admission Essay Template - College Applica
College Admission Essay Template - College ApplicaCollege Admission Essay Template - College Applica
College Admission Essay Template - College ApplicaJustin Knight
 
How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...
How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...
How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...Justin Knight
 
College Application Essay Sample College Applicatio
College Application Essay Sample  College ApplicatioCollege Application Essay Sample  College Applicatio
College Application Essay Sample College ApplicatioJustin Knight
 
Top Narrative Essay Examples Mla Most Popul
Top Narrative Essay Examples Mla Most PopulTop Narrative Essay Examples Mla Most Popul
Top Narrative Essay Examples Mla Most PopulJustin Knight
 
Calamo - How To Buy Best Essay Wr. Online assignment writing service.
Calamo - How To Buy Best Essay Wr. Online assignment writing service.Calamo - How To Buy Best Essay Wr. Online assignment writing service.
Calamo - How To Buy Best Essay Wr. Online assignment writing service.Justin Knight
 
Career Research Paper Sample. Online assignment writing service.
Career Research Paper Sample. Online assignment writing service.Career Research Paper Sample. Online assignment writing service.
Career Research Paper Sample. Online assignment writing service.Justin Knight
 
Writing The Ad Analysis Essay - YouTube. Online assignment writing service.
Writing The Ad Analysis Essay - YouTube. Online assignment writing service.Writing The Ad Analysis Essay - YouTube. Online assignment writing service.
Writing The Ad Analysis Essay - YouTube. Online assignment writing service.Justin Knight
 
ABAP FAQ S On Reports Scripts BDC Dialogs ABAP Reporting SAP TERMINOLOGY
ABAP FAQ S On Reports   Scripts   BDC   Dialogs ABAP Reporting SAP TERMINOLOGYABAP FAQ S On Reports   Scripts   BDC   Dialogs ABAP Reporting SAP TERMINOLOGY
ABAP FAQ S On Reports Scripts BDC Dialogs ABAP Reporting SAP TERMINOLOGYJustin Knight
 
AI 03 Solving Problems By Searching
AI 03 Solving Problems By SearchingAI 03 Solving Problems By Searching
AI 03 Solving Problems By SearchingJustin Knight
 
ASSESSMENT AND EVALUATION IN EDUCATION
ASSESSMENT AND EVALUATION IN EDUCATIONASSESSMENT AND EVALUATION IN EDUCATION
ASSESSMENT AND EVALUATION IN EDUCATIONJustin Knight
 
Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...
Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...
Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...Justin Knight
 
A Framework For Information Security Risk Management Communication
A Framework For Information Security Risk Management CommunicationA Framework For Information Security Risk Management Communication
A Framework For Information Security Risk Management CommunicationJustin Knight
 
An Object Approach For Web Presentations
An Object Approach For Web PresentationsAn Object Approach For Web Presentations
An Object Approach For Web PresentationsJustin Knight
 
American Ways - An Introducation To American Culture.PDF
American Ways - An Introducation To American Culture.PDFAmerican Ways - An Introducation To American Culture.PDF
American Ways - An Introducation To American Culture.PDFJustin Knight
 
A Compilation Of Case Digests For Transportation Law
A Compilation Of Case Digests For Transportation LawA Compilation Of Case Digests For Transportation Law
A Compilation Of Case Digests For Transportation LawJustin Knight
 
A New Childhood Social Studies Curriculum For A New Generation Of Citizenship
A New Childhood Social Studies Curriculum For A New Generation Of CitizenshipA New Childhood Social Studies Curriculum For A New Generation Of Citizenship
A New Childhood Social Studies Curriculum For A New Generation Of CitizenshipJustin Knight
 
A Supporting Hand In Dealing With Interpersonal Conflicts The Role Of Intera...
A Supporting Hand In Dealing With Interpersonal Conflicts  The Role Of Intera...A Supporting Hand In Dealing With Interpersonal Conflicts  The Role Of Intera...
A Supporting Hand In Dealing With Interpersonal Conflicts The Role Of Intera...Justin Knight
 
An Analytical Framework For Miles And Snow Typology And Dynamic Capabilities
An Analytical Framework For Miles And Snow Typology And Dynamic CapabilitiesAn Analytical Framework For Miles And Snow Typology And Dynamic Capabilities
An Analytical Framework For Miles And Snow Typology And Dynamic CapabilitiesJustin Knight
 

More from Justin Knight (20)

My Summer Narrative Writing For The Beginning O
My Summer Narrative Writing For The Beginning OMy Summer Narrative Writing For The Beginning O
My Summer Narrative Writing For The Beginning O
 
Writing Paper With Drawing Space. Online assignment writing service.
Writing Paper With Drawing Space. Online assignment writing service.Writing Paper With Drawing Space. Online assignment writing service.
Writing Paper With Drawing Space. Online assignment writing service.
 
College Admission Essay Template - College Applica
College Admission Essay Template - College ApplicaCollege Admission Essay Template - College Applica
College Admission Essay Template - College Applica
 
How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...
How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...
How To Open An Essay. How To Start An Essay (With Pictures. Online assignment...
 
College Application Essay Sample College Applicatio
College Application Essay Sample  College ApplicatioCollege Application Essay Sample  College Applicatio
College Application Essay Sample College Applicatio
 
Top Narrative Essay Examples Mla Most Popul
Top Narrative Essay Examples Mla Most PopulTop Narrative Essay Examples Mla Most Popul
Top Narrative Essay Examples Mla Most Popul
 
Calamo - How To Buy Best Essay Wr. Online assignment writing service.
Calamo - How To Buy Best Essay Wr. Online assignment writing service.Calamo - How To Buy Best Essay Wr. Online assignment writing service.
Calamo - How To Buy Best Essay Wr. Online assignment writing service.
 
Career Research Paper Sample. Online assignment writing service.
Career Research Paper Sample. Online assignment writing service.Career Research Paper Sample. Online assignment writing service.
Career Research Paper Sample. Online assignment writing service.
 
Writing The Ad Analysis Essay - YouTube. Online assignment writing service.
Writing The Ad Analysis Essay - YouTube. Online assignment writing service.Writing The Ad Analysis Essay - YouTube. Online assignment writing service.
Writing The Ad Analysis Essay - YouTube. Online assignment writing service.
 
ABAP FAQ S On Reports Scripts BDC Dialogs ABAP Reporting SAP TERMINOLOGY
ABAP FAQ S On Reports   Scripts   BDC   Dialogs ABAP Reporting SAP TERMINOLOGYABAP FAQ S On Reports   Scripts   BDC   Dialogs ABAP Reporting SAP TERMINOLOGY
ABAP FAQ S On Reports Scripts BDC Dialogs ABAP Reporting SAP TERMINOLOGY
 
AI 03 Solving Problems By Searching
AI 03 Solving Problems By SearchingAI 03 Solving Problems By Searching
AI 03 Solving Problems By Searching
 
ASSESSMENT AND EVALUATION IN EDUCATION
ASSESSMENT AND EVALUATION IN EDUCATIONASSESSMENT AND EVALUATION IN EDUCATION
ASSESSMENT AND EVALUATION IN EDUCATION
 
Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...
Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...
Application Of Lean Six Sigma To Improve Service In Healthcare Facilities Man...
 
A Framework For Information Security Risk Management Communication
A Framework For Information Security Risk Management CommunicationA Framework For Information Security Risk Management Communication
A Framework For Information Security Risk Management Communication
 
An Object Approach For Web Presentations
An Object Approach For Web PresentationsAn Object Approach For Web Presentations
An Object Approach For Web Presentations
 
American Ways - An Introducation To American Culture.PDF
American Ways - An Introducation To American Culture.PDFAmerican Ways - An Introducation To American Culture.PDF
American Ways - An Introducation To American Culture.PDF
 
A Compilation Of Case Digests For Transportation Law
A Compilation Of Case Digests For Transportation LawA Compilation Of Case Digests For Transportation Law
A Compilation Of Case Digests For Transportation Law
 
A New Childhood Social Studies Curriculum For A New Generation Of Citizenship
A New Childhood Social Studies Curriculum For A New Generation Of CitizenshipA New Childhood Social Studies Curriculum For A New Generation Of Citizenship
A New Childhood Social Studies Curriculum For A New Generation Of Citizenship
 
A Supporting Hand In Dealing With Interpersonal Conflicts The Role Of Intera...
A Supporting Hand In Dealing With Interpersonal Conflicts  The Role Of Intera...A Supporting Hand In Dealing With Interpersonal Conflicts  The Role Of Intera...
A Supporting Hand In Dealing With Interpersonal Conflicts The Role Of Intera...
 
An Analytical Framework For Miles And Snow Typology And Dynamic Capabilities
An Analytical Framework For Miles And Snow Typology And Dynamic CapabilitiesAn Analytical Framework For Miles And Snow Typology And Dynamic Capabilities
An Analytical Framework For Miles And Snow Typology And Dynamic Capabilities
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Applications Of MATLAB Ordinary Differential Equations (ODE

  • 1. Applications of MATLAB: Ordinary Differential Equations (ODE) David Houcque Robert R. McCormick School of Engineering and Applied Science - Northwestern University 2145 Sheridan Road Evanston, IL 60208-3102 Abstract Textbooks on differential equations often give the impression that most differential equations can be solved in closed form, but experience does not bear this out. It remains true that solutions of the vast majority of first order initial value problems cannot be found by analytical means. Therefore, it is important to be able to approach the problem in other ways. Today there are numerous methods that produce numerical approximations to solutions of differential equations. Here, we introduce the oldest and simplest such method, originated by Euler about 1768. It is called the tangent line method or the Euler method. It uses a fixed step size h and generates the approximate solution. The purpose of this paper is to show the details of implementing a few steps of Euler’s method, as well as how to use built-in functions available in MATLAB (2005) [1]. In the first part, we use Euler methods to introduce the basic ideas associated with initial value problems (IVP). In the second part, we use the Runge-Kutta method pre- sented together with the built-in MATLAB solver ODE45. The implementations that we develop in this paper are designed to build intuition and are the first step from textbook formula on ODE to production software. Key words: Euler’s methods, Euler forward, Euler modified, Euler backward, MAT- LAB, Ordinary differential equation, ODE, ode45. 1 Introduction The dynamic behavior of systems is an important subject. A mechanical system involves displace- ments, velocities, and accelerations. An electric or electronic system involves voltages, currents, and time derivatives of these quantities. An equation that involves one or more derivatives of the 1
  • 2. unknown function is called an ordinary differential equation, abbreviated as ODE. The order of the equation is determined by the order of the highest derivative. For example, if the first derivative is the only derivative, the equation is called a first-order ODE. In the same way, if the highest derivative is second order, the equation is called a second-order ODE. The problems of solving an ODE are classified into initial-value problems (IVP) and boundary- value problems (BVP), depending on how the conditions at the endpoints of the domain are spec- ified. All the conditions of an initial-value problem are specified at the initial point. On the other hand, the problem becomes a boundary-value problem if the conditions are needed for both initial and final points. The ODE in the time domain are initial-value problems, so all the conditions are specified at the initial time, such as t = 0 or x = 0. For notations, we use t or x as an independent variable. Some literatures use t as time for independent variable. It is important to note that our focus here is on the practical use of numerical methods in order to solve some typical problems, not to present any consistent theoretical background. There are many excellent and exhaustive texts on these subjects that may be consulted. For example, we would recommend Edwards and Penny (2000) [2], Boyce and DiPrima (2001) [3], Coombes et al. (2000) [4], Van Loan (1997) [5], Nakamura (2002) [6], Moler (2004) [7], and Gilat (2004) [8]. 2 Numerical methods Numerical methods are commonly used for solving mathematical problems that are formulated in science and engineering where it is difficult or even impossible to obtain exact solutions. Only a limited number of differential equations can be solved analytically. Numerical methods, on the other hand, can give an approximate solution to (almost) any equation. An ordinary differential equation (ODE) is an equation that contains an independent variable, a dependent variable, and derivatives of the dependent variable. Literal implementation of this procedure results in Euler’s method, which is, however, not recommended for any practical use. There are other methods more sophisticated than Euler’s. Among them, there are three major types of practical numerical methods for solving initial value problems for ODEs: (i) Runge-Kutta methods, (ii) Burlirsch-Stoer method, and (iii) predictor-corrector methods. We will present these three approaches on another occasion. Now, we are interested to talk about Euler’s methods. 2.1 EULER methods The Euler methods are simple methods of solving first-order ODE, particularly suitable for quick programming because of their great simplicity, although their accuracy is not high. Euler methods include three versions, namely, • forward Euler method • modified Euler method • backward Euler method
  • 3. 2.1.1 Forward Euler method The forward Euler method for y′ = f(y, x) is derived by rewriting the forward difference approxi- mation, (yn+1 − yn)/h ≈ y′ n (1) to yn+1 = yn + hf(yn, xn) (2) where y′ n = f(yn, xn) is used. In order to advance time steps, Eq. 2 is recursively applied as y1 = y0 + hy′ 0 y1 = y0 + hf(y0, x0) y2 = y1 + hf(y1, x1) (3) y3 = y2 + hf(y2, x2) . . . yn = yn−1 + hf(yn−1, xn−1) 2.1.2 Modified Euler method First, the modified Euler method is more accurate than the forward Euler method. Second, it is more stable. It is derived by applying the trapezoidal rule to the solution of y′ = f(y, x) yn+1 = yn + h 2 [f(yn+1, xn+1) + f(yn, xn)] (4) 2.1.3 Backward Euler Method The backward Euler method is based on the backward difference approximation and written as yn+1 = yn + hf(yn+1, xn+1) (5) The accuracy of this method is quite the same as that of the forward Euler method. 2.2 Steps for MATLAB implementation The purpose of using an example is to show you the details of implementing the typical steps of Euler’s method, so that it will be clear exactly what computations are being executed. For some reasons, MATLAB does not include Euler functions. Therefore, if you really need one, you have to code by yourselves. However, MATLAB has very sophisticated ones using Runge-Kutta algorithms. We will show how to use one of them in the next section.
  • 4. 2.2.1 Basic steps The typical steps of Euler’s method are given below. Step 1. define f(x, y) Step 2. input initial values x0 and y0 Step 3. input step sizes h and number of steps n Step 4. calculate x and y: for i=1:n x=x+h y=y+hf(x,y) end Step 5. output x and y Step 6. end 2.2.2 Example As an application, consider the following initial value problem dy dx = x y , y(0) = 1 (6) which was chosen because we know the analytical solution and we can use it for check. Its exact or analytical solution is found to be y(x) = p x2 + 1 (7) Therefore, we will be able to compare the approximate solutions and the exact solution. Here we wish to approximate y(0.3) using the Euler’s methods with step sizes h = 0.1 and h = 0.05. We find by hand-calculation, x0 = 0, x1 = 0.1, x2 = 0.2, x3 = 0.3 y0 = 1 y1 = y0 + hf(x0, y0) = y0 + hx0/y0 = 1 y2 = y1 + hx1/y1 = 1.01 y3 = y2 + hx2/y2 = 1.0298.
  • 5. Since y(0.3) = p (0.3)2 + 1 = 1.044030, we find that Error = |y(0.3) − y3| y(0.3) × 100 = 1.36% Similarly, for the step size h = 0.05, we find that the error is Error = |y(0.3) − y6| y(0.3) × 100 = 0.67% 2.2.3 MATLAB codes • Step 1: Create user-defined function files: euler forward.m, euler modified.m, and euler backward.m. function [x,y]=euler_forward(f,xinit,yinit,xfinal,n) % Euler approximation for ODE initial value problem % Euler forward method % File prepared by David Houcque - Northwestern U. 5/11/2005 % Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; % Initialization of x and y as column vectors x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)]; % Calculation of x and y for i=1:n x(i+1)=x(i)+h; y(i+1)=y(i)+h*f(x(i),y(i)); end end function [x,y]=euler_modified(f,xinit,yinit,xfinal,n) % Euler approximation for ODE initial value problem % Euler modified method % File prepared by David Houcque - Northwestern U. - 5/11/2005 % Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; % Initialization of x and y as column vectors x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)];
  • 6. % Calculation of x and y for i=1:n x(i+1)=x(i)+h; ynew=y(i)+h*f(x(i),y(i)); y(i+1)=y(i)+(h/2)*(f(x(i),y(i))+f(x(i+1),ynew)); end end function [x,y]=euler_backward(f,xinit,yinit,xfinal,n) % Euler approximation for ODE initial value problem % Euler backward method % File prepared by David Houcque - Northwestern U. - 5/11/2005 % Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; % Initialization of x and y as column vectors x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)]; % Calculation of x and y for i=1:n x(i+1)=x(i)+h; ynew=y(i)+h*(f(x(i),y(i))); y(i+1)=y(i)+h*f(x(i+1),ynew); end end • Step 2: Solve the problem by putting data and called functions into a script file called main1.m: % Script file: main1.m % The RHS of the differential equation is defined as % a handle function % File prepared by David Houcque - Northwestern U. - 5/11/2005 f=@(x,y) x./y; % Calculate exact solution g=@(x) sqrt(x.^2+1); xe=[0:0.01:0.3]; ye=g(xe);
  • 7. % Call functions [x1,y1]=euler_forward(f,0,1,0.3,6); [x2,y2]=euler_modified(f,0,1,0.3,6); [x3,y3]=euler_backward(f,0,1,0.3,6); % Plot plot(xe,ye,’k-’,x1,y1,’k-.’,x2,y2,’k:’,x3,y3,’k--’) xlabel(’x’) ylabel(’y’) legend(’Analytical’,’Forward’,’Modified’,’Backward’) axis([0 0.3 1 1.07]) % Estimate errors error1=[’Forward error: ’ num2str(-100*(ye(end)-y1(end))/ye(end)) ’%’]; error2=[’Modified error: ’ num2str(-100*(ye(end)-y2(end))/ye(end)) ’%’]; error3=[’Backward error: ’ num2str(-100*(ye(end)-y3(end))/ye(end)) ’%’]; error={error1;error2;error3}; text(0.04,1.06,error) • Step 3: Compare the results. The calculated results are displayed in the graphical form below. Reasonably good results are obtained even for a moderately large step size and the approximation can be improved by decreasing the step size. According to the results (Figure 1) and Table 1, forward and backward approaches give identically the same results (less than 1% of error), while modified method give very good result when compared with the exact solution. h Forward Modified Backward 0.05 0.67% 0.04% 0.67% Table 1: Comparison of exact solution with Euler methods 2.3 Using built-in function MATLAB has several different functions (built-ins) for the numerical solution of ordinary differ- ential equations (ODE). In this section, however, we will present one of them. We will also give an example how to use it, instead of writing our own MATLAB codes as we did in the first part. The basic steps, previously defined, are still typically the same. These solvers can be used with the following syntax: [x,y] = solver(@odefun,tspan,y0)
  • 8. 0 0.05 0.1 0.15 0.2 0.25 0.3 1 1.01 1.02 1.03 1.04 1.05 1.06 1.07 x y Forward error: −0.6751% Modified error: 0.0003983% Backward error: 0.67244% Analytical Forward Modified Backward Figure 1: Comparison of exact solution with Euler methods
  • 9. solver is the solver you are using, such as name, ode45 or ode23. odefun is the function that defines the derivatives, so odefun defines y′ as a function of the independent parameter (typically x or t) as well as y and other parameters. tspan a vector that specifies the interval of the solution (e.g., [t0,tf]). y0 is the initial value of y. [x,y] is the output, which is the solution of the ODE. 2.3.1 Runge-Kutta methods There are many variants of the Runge-Kutta method, but the most widely used one is the following. Given: y′ = f(x, y) (8) y(xn) = yn we compute in turn k1 = hf(xn, yn) k2 = hf(xn + h 2 , yn + k1 2 ) k3 = hf(xn + h 2 , yn + k2 2 ) (9) k4 = hf(xn + h, yn + k3) yn+1 = yn + 1 6 (k1 + 2k2 + 2k3 + k4) 2.3.2 Using ode45 ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair [9]. That means the numerical solver ode45 combines a fourth order method and a fifth order method, both of which are similar to the classical fourth order Runge-Kutta (RK) method discussed above. The modified RK varies the step size, choosing the step size at each step in an attempt to achieve the desired accuracy. Therefore, the solver ode45 is suitable for a wide variety of initial value problems in practical applications. In general, ode45 is the best function to apply as a “first try” for most problems. Table 2 lists ODE solvers, which are MATLAB built-in functions. A short description of each solver is included. Note - It is important to note that in MATLAB 7.0 (R14), latest version, it is preferred to have odefun in the form of a function handle. For example, it is recommended to use the following syntax, ode45(@xdot,tspan,y0) rather than
  • 10. Solver Accuracy Description ode45 Medium This should be the first solver you try ode23 Low Less accurate than ode45 ode113 Low to high For computationally intensive problems ode15s Low to medium Use if ode45 failed Table 2: Some MATLAB ODE solvers ode45(’xdot’,tspan,y0) Note the use of @xdot and ’xdot’. Use function handles to pass any function that defines quantities the MATLAB solver will compute, in particular for simple functions. On the other hand, it is also important to remember that complicated differential equations should be written an M-file instead of using inline command or function handle. Following is an example of an ordinary differential equation using a MATLAB ODE solver. First, let’s create a script file, called main2.m, as follows: % Script file: main2.m % The RHS of the differential equation is defined as % a function handle. % File prepared by David Houcque - Northwestern U. - 5/11/2005 f=@(x,y) x./y; % Calculate exact solution g=@(x) sqrt(x.^2+1); xe=[0:0.01:0.3]; ye=g(xe); % Call function [x4,y4]=ode45(f,[0,0.3],1); % Plot plot(xe,ye,’k-’,x4,y4,’k:’) xlabel(’x’) ylabel(’y’) legend(’Analytical’,’ode45’) axis([0 0.3 1 1.05]) Here, we use the same data as defined in the first part for Euler’s methods. The initial conditions and the time steps are the same as before. The integration proceeds by steps, taken to the values specified in tspan. Note that the step size (the distance between consecutive elements of tspan) does not have to be uniform.
  • 11. A plot comparing the computed y versus x is shown in Figure 2. According to the plot, the 0 0.05 0.1 0.15 0.2 0.25 0.3 1 1.005 1.01 1.015 1.02 1.025 1.03 1.035 1.04 1.045 1.05 x y Analytical ode45 Figure 2: Comparison of exact solution with ode45 solution calculated results using the built-in function (ode45) give a very good result when compared with the analytical solution. Additional on all of these solvers can be found in the online help. For addition information on numerical methods, we refer to Shampine (1994) [10] and Forsythe et al. (1977) [11]. 3 Conclusion The above plots show the results obtained from different algorithms. Consequently, we can see better that Runge-Kutta algorithm is even more accurate at large step size (h = 0.1) than Euler algorithms at small step size (h = 0.05). One can easily adapt these MATLAB codes as needed for a different type of problem. In using numerical procedure, such as Euler’s method, one must always keep in mind the question of whether the results are accurate enough to be useful. In the preceding examples, the accuracy of the numerical results could be ascertained directly by a comparison with the solution obtained analytically. Of course, usually the analytical solution is not always available to compare.
  • 12. Acknowledgement- The author is sincerely grateful to Associate Dean Steve Carr for his support. Thanks also go to Professor MacIver for his support and discussions. He is grateful to EA3 students (Honors Section) for their bright ideas and suggestions. References [1] The MathWorks Inc. MATLAB: SP2 R14. The MathWorks Inc., 2005. [2] C. H. Edwards and D. E. Penny. Differential Equations and Boundary Value Problems: Com- puting and Modeling. Prentice Hall, 2000. [3] W. E. Boyce and R. C. DiPrima. Elementary Differential Equations and Boundary Value Problems. John Wiley and Sons, 2001. [4] K. R. Coombes, B. R. Hunt, R. L. Lipsman, J. E. Osborn, and G. J. Stuck. Differential Equations with MATLAB. John Wiley and Sons, 2000. [5] C. F. Van Loan. Introduction to Scientific Computing. Prentice Hall, 1997. [6] S. Nakamura. Numerical Analysis with MATLAB. Prentice Hall, 2002. [7] C. B. Moler. Numerical Computing with MATLAB. Siam, 2004. [8] A. Gilat. MATLAB: An introduction with Applications. John Wiley and Sons, 2004. [9] J. R. Dormand and P. J. Prince. A family of embedded Runge-Kutta formulae. J. Comp. Appl. Math, 6:19–26, 1980. [10] L. F. Shampine. Numerical Solution of Ordinary Equations. Chapman and Hall, 1994. [11] G. Forsythe, M. Malcolm, and C. Moler. Computer Methods for Mathematical Computations. Prentice-Hall, 1977.