Sei sulla pagina 1di 3

EM203

Matlab Numerical Solutions of Ordinary Differential


Equations (Initial Value Problems) Example 1
Let us consider the equation given by (2).
Numerical Simulation using Matlab numerical routines
There are number of Matlab routines to simulate a Step 1
dynamical system describe by the set of simultaneous first
order differential equations To use the routine ode45 we need to create a Matlab file
dy (m-file) that would compute derivative dy/dt for any given
f ( y, u , t ) with y(t0)=yo (1) y and t. We want to compute the solution from t=0 to
dt t=10.
where y can be a vector of n elements if the system is of nth
order. u is the input to the system and t is the time. f is a Thus we create the following Matlab function file.
vector of n functions.
function dy=ex1(t,y)
First Order Differential Equations a=-2;b=1;
%computing derivatives for given y %
However, we can begin the study by considering a simple and t
first order differential equation of the form dy=a*y+b;
dy
ay b f ( y ) with y(t0)=y0 (2) and save this file in ex1.m. You may notice that we have
dt selected that a=-2 and b=1.
where we need to find the solution from t=t0 to t=tf.
Constants a and b will depend on the system, and the Step 2
initial value y0 we can select. The simplest format of ode45 is given by
[t,y] = ode45(odefun,tspan,y0)
The analytical solution of this equation is a continuous
function y(t) defined in the domain where odefun is the function that provides the derivatives,
[t0, tf]. tspan a vector indicating the initial time, time step and
final time, and yo is the initial value.
The numerical solution of this equation is given by a set
of numerical values The solution to the differential equation can be computed
yn= y(t0 + nT) for n=0,1,2, ..,p and T is a step size by using ode45, by typing
decided by us. >>[t,y]=ode45(@ex1,[0:0.1:10],5);

Note that the 0.1 is the step size we have selected, and the initial
Fi value of y is 5. the output variable of the ode45 is the vectors t
and y which contains the time instants ans the solution values
respectively.
y(t) The solution can be visualized by
yn >>plot(t,y)

Further Investigations
- Now you can investigate the numerical method by trying
different step sizes T and comparing the solutions you get
by plotting them. As you know the small step sizes should
give more accurate results.

t0 t1 t2 T tp - You can also investigate the behaviour of the solution of


the differential equation by computing the solution for
different initial values.
g. 1 Analytical solution y(t) and numerical
solution yn. - By giving different values for a and b we get different
differential equations and hence different solutions. Try
Matlab functions to solve the equation different values for a and b. When changing one keep the
other constant (also keep the initial value constant). Try
To obtain the solution of the above equation we can use the both positive and negative values for a and b.
Matlab functions ode23 or ode45. To get familiar with how
to use them we shall do a few examples.
1
- Above we have specified the step size T. Matlab ode45 displace the mass and let it free to move, or give an initial
has a special feature where it can decide the step size at the velocity too.
end of each iteration. Give the command
>>[t,y]=ode45(@ex1,[0 10],5); and Matlab will use To simulate this system (taking m=1, b=.5 and k=3) we
variable step sizes. Plot the solution and compare with solution use the Matlab function ode45.
for constant step size.
Exercise 1 Step 1:
The single tank system dynamics At each time step ode45 needs to compute the derivatives
be described by qi as needed in Runge-Kutta methods, and this is given to the
dh system by creating a function and passing the function
k
A h 1A qi name to the ode45.
dt
h Create the function with the file name sys1.m

function dx=sys1(t,x)
qi is the inflow, A the tank x-sectional
m=1;b=5;k=3;
area h is the water level. Take k=1.1 and %to make dx a column vector
A=10 and qi= 2 and find the solution for dx = zeros(2,1);
different initial water levels. %computing derivatives
Investigate the behaviour of the tank for different k and A. dx(1)=x(2);
You can also change the inflow qi. You may have to change dx(2)= -(k/m)*x(1)-(b/m)*x(2);
the final time tf.
Step 2:
Use Matlab help to learn more about ode45 and the The simplest format of ode45 is given by
other ODE solvers available in Matlab. [t,x] = ode45(odefun,tspan,x0)

Second Order Differential Equations where odefun is the function that provides the derivatives,
tspan a vector indicating the initial time and final time,
(a) System without an input (u=0) To begin let us and yo is the initial value of vector x.
consider a system without an input u. The system is
described by the equation For the above system, to use ode45, type
>>[t,x]=ode45(@sys1,[0:0.1:10],[5 0]);
dy simulates from t=0 to t=10 with initial values of x1=5
f ( y , t ) with y(0)=yo and x2=0. Step size is 0.1.
dt >>plot(t,x)
You can also try to plot x1 against x2.
Please note that if the initial condition yo=0 then the >> plot(x(:,1),x(:,2))
solution y(t) is zero for all t. In the following two examples This is called the phase plane plot. It clearly shows how
you can change the initial conditions and observe how the the system comes to the equilibrium from different initial
system behaves and reach the equilibrium point. points. Use the hold command
>> hold
Example 2. and draw the phase plane curves for different initial values.
A spring, mass and damper system without an external
force acting on it is described by the equation Note the response of the system. Change the values of m,b
mx bx kx 0 with the initial values of x and x and k, and try to obtain different responses of the system.
specified. This second order equation can be expressed as a Also observe the variation in the time step, where the step
system of first order coupled differential equations as size is not constant.
follows (taking x1=x)

dx1 Example 3
x2 The previous system is a linear system. A well-known non-
dt linear system is the Van der Pols oscillator described by
dx 2 b k
x 2 x1 the differential equation
dt m m d 2x dx
2
( x 2 1) x 0
with the initial values of x1 and x2 specified. Note that x1 dt dt
is the displacement and x2 is the velocity. Initially we can This can be written as two differential equations as follows
(taking x1=x)

2
dx1 function dx=sys2(t,x)
x2 m=1;b=5;k=3;
dt %evaluate the external function %value
dx 2
( x12 1) x 2 x1 for a given value of t
dt u=feval(finp,t)
%to make dh a column vector
Write a function to pass the derivatives to the ode45 and dx = zeros(2,1);
simulate the system for different initial values of x1 and x2. %computing derivatives
Draw a phase plane curves for this system. What is the dx(1)=x(2);
difference of this oscillator compared to a simple dx(2)= u -(k/m)*x(1)-(b/m)*x(2);
pendulum.
Step 3:
As before run the ode45 as follows
(b) System with an input ( u (t ) 0) >>[t,x]=ode45(@sys2,[0 10],[5 0]);

A system with a driving force or input function can be


described by Step 4:
Since the input generated is not available it is generated
dy
f ( y, u , t ) with y(0)=yo by the following commands.
dt >>for i=1:length(t),u(i)=feval(finp,t(i)); end
>> plot(t,u,t,x)
The input u(t) can be a constant or a function of t. The
Matlab function ode45 can be used as before with a slight Investigate the behaviour of the system for different input
modification. functions such as, impulse, step, random inputs etc., by
creating different input function files or having them on
Example 4 the same file and select the appropriate one.

System is same as in example 1, but driven by an input. At the end of this lab you should know
The respective equation is given by 1. To create a system of differential equations for a given
mx bx kx f (t ) where f(t) is the input function higher order non-linear differential equation.
or the driving force. The respective system of equation is 2. To write the Matlab functions to provide the derivatives
given by to the ode45
3. To provide different input functions to ode45
dx1
x2 4. simulate a system using ode45
dt 5. plot the phase plane curves of the system and recognize
dx 2 b k the behaviour of the system..
f (t ) x 2 x1
dt m m Exercise 2
Let us give a sinusoidal input to the system Simulate the second order interactive tank system
f(t)= A sin(wt) described by
Step 1 dh1
A1 Fi a12 (h1 h2 )
Create a Matlab function for the input function as follows dt
dh
Function u=finp(t) A2 2 a12 (h1 h2 ) a 2 h2
A=1 dt
w=2 Indicate clearly how you would handle the situation
u=A*sin(w*t) during the simulation if h1<h2. Study the behaviour of
this system for different types of inputs Fi. Can you
and store in finp.m select A1, A2, a12 and a2 such the system would exhibit
Step 2: damped oscillations for a step input?
Create a function to provide the derivatives to the ode45 in Typical set of values to begin with A1=1, A2=0.5, ,
every iteration as follows (similar to in example 1 but with a2=0.1, a12=0.3.
a change to accommodate the input function)