Introduction to MATLAB

58:111 Numerical Calculations
MATLAB EXAMPLES
Initial-value problems
Department of Mechanical and Industrial Engineering
Some useful functions
ode23
An explicit one-step Runge-Kutta low order(2nd-3rd)solver
ode23s
An implicit one-step modified Rosenbrock solver of order two
ode23t
An implicit one-step trapezoidal rule using a free interpolate
ode23tb
An implicit trapezoidal rule followed by a backward differentiation rule
of order two
ode45
An explicit one-step Runge Kutta medium-order (4th-5th) solver
ode113
A multistep Adams-Bashforth-Moulton PECE solver of varying order (1st
–13th)
ode15s
An implicit, multistep numerical differentiation solver of varying order
(1st-5th)
IVP
The initial value problems for 1st order differential equation are
defined by the following standard form:
 x  f  t , x 

 x  a  is given
Example:
 x  x  1

 x(0)  0
The exact solution is:
x  et  1 x(1)  1.718281828
In the following slides, we will try to solve this problem by Euler and Runge-Kutta
methods in MATLAB. The ODE function was defined as M-file (ex1_odefun.m):
% IVP function for EX1
function xdot=ex1_odefun(t,x)
xdot=x+1;
IVP –Euler method
Use Euler method to solve the IVP example, step size sets to 0.01.
Write the M-file as follows (ivp-euler.ex1.m):
% IVP solution - Euler method
% example 1: x'=x+1, x(0)=0, solve x(1)
% the range of t [a,b]
a=0.0;
b=1.0;
% the initial value of x
x=0.0;
%the number of steps
n=100;
%set the matrix
x0=zeros(n,1);
Run this M-file:
>> ivp_euler_ex1
ans =
1.7048
% set the step size
h=(b-a)/100;
%set the initial value of t
t=a;
for k=1:n
% Record the values of x in each step
x=x+h*ex1_odefun(t,x);
x0(k)=x;
t=t+h;
end
%the final result of x
x0(100)
IVP –Runge-Kutta method
To use Runge-Kutta method, here we only try ode23 and ode45 (step size =0.1):
>> tspan=linspace(0,1,11);
>> x0=[0;0];
>> [t,x]=ode23(@ex1_odefun,tspan,x0);
>> x(11)
ans =
1.7182
>> [t,x]=ode45(@ex1_odefun,tspan,x0);
>> x(11)
ans =
1.7183
The results show the higher order solver ode45 got more accurate result than ode23 and Euler
method.
IVP - Plot
To show the accuracy of different methods, we need plot the solution together, the following Mfile is for plotting the results of exact, Euler and 4 th Runge-Kutta methods (ivp-plot_ex1.m):
% IVP solution - plot
% Record the values of x in each step
% example 1 x'=x+1
x1(k)=x;
% the range of t [a,b]
x=x+h*ex1_odefun(t,x);
a=0.0;
t=t+h;
b=1.0;
end
% the initial value of x
%plot the results
x=0.0;
t=linspace(0,1,11);
%the number of steps
%Runge-Kutta method
n=10;
rx0=[0;0];
%set the matrix
[t,x2]=ode45(@ex1_odefun,t,rx0);
x1=zeros(n,1);
%exact solution
%the step size
x0=exp(t)-1;
h=(b-a)/10;
plot(t,x0,t,x1,'.',t,x2,'o')
%set the initial value of t
xlabel('t')
t=a;
ylabel('x')
%Euler method - Iterations
for k=1:n+1
IVP - Plot
The following figures show the results by different step size h. The solid line is the
exact solution, the solid dots represent the result of Euler method, the circles represents
the results of 4th order Runge-Kutta method.
h = 0.1
h = 0.01
IVP-Summary
Compare the results of different methods (all keep 4 digits after the radix
point) in the following table (x(1)):
Method
Solution
Exact
1.7183
Euler (h = 0.1)
1.5937
4th Runge-Kutta (h = 0.1)
1.7183
Euler (h = 0.01)
1.7084
4th Runge-Kutta (h = 0.01)
1.7183