Professional Documents
Culture Documents
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.
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]);
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)