EM 311 Mathematical Modelling

advertisement
CP502 – Advanced Fluid Mechanics
Example 1
Let us consider the equation given by (2).
Matlab – Numerical Solutions of Ordinary Differential
Equations (Initial Value Problems)
Step 1
Numerical Simulation using Matlab numerical routines
There are number of Matlab routines to simulate a
dynamical system describe by the set of simultaneous first
order differential equations
To use the routine ode45 we need to create a Matlab file
(m-file) that would compute derivative dy/dt for any given
y and t. We want to compute the solution from t=0 to
t=10.
dy
 f ( y, u, t )
dt
Thus we create the following Matlab function file.
with y(t0)=yo
(1)
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
vector of n functions.
First Order Differential Equations
function dy=ex1(t,y)
a=-2;b=1;
%computing derivatives
and t
dy=a*y+b;
However, we can begin the study by considering a simple
first order differential equation of the form
and save this file in ex1.m. You may notice that we have
selected that a=-2 and b=1.
dy
 ay  b  f ( y ) with y(t0)=y0
dt
(2)
where we need to find the solution from t=t0 to t=tf.
Constants a and b will depend on the system, and the
initial value y0 we can select.
The analytical solution of this equation is a continuous
function y(t) defined in the domain
[t0, tf].
The numerical solution of this equation is given by a set
of numerical values
yn= y(t0 + nT) for n=0,1,2, ……..,p and T is a step size
decided by us.
T
given
y
%
Step 2
The simplest format of ode45 is given by
[t,y] = ode45(odefun,tspan,y0)
where odefun is the function that provides the derivatives,
tspan a vector indicating the initial time, time step and
final time, and yo is the initial value.
The solution to the differential equation can be computed
by using ode45, by typing
>>[t,y]=ode45(@ex1,[0:0.1:10],5);
Note that the 0.1 is the step size we have selected, and the initial
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.
The solution can be visualized by
>>plot(t,y)
y(t)
yn
t0 t1 t2
for
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.
tp
- You can also investigate the behaviour of the solution of
the differential equation by computing the solution for
different initial values.
Matlab functions to solve the equation
- By giving different values for a and b we get different
differential equations and hence different solutions. Try
different values for a and b. When changing one keep the
other constant (also keep the initial value constant). Try
both positive and negative values for a and b.
To obtain the solution of the above equation we can use
the Matlab functions ode23 or ode45. To get familiar with
how to use them we shall do a few examples.
- Above we have specified the step size T. Matlab ode45
has a special feature where it can decide the step size at the
end of each iteration. Give the command
Fig. 1 Analytical solution y(t) and numerical
solution yn.
>>[t,y]=ode45(@ex1,[0 10],5); and Matlab will use
variable step sizes. Plot the solution and compare with solution
for constant step size.
system by creating a function and passing the function
name to the ode45.
Exercise 1
The single tank system dynamics
be described by
dh

dt
k
A
qi
Create the function with the file name sys1.m
h  1A q i
h
qi is the flow in, A the tank x-sectional
area h is the water level. Take k=1.1 and
A=10 and qi= 2 and find the solution for
different initial water levels.
Investigate the behaviour of the tank for different k and A.
You can also change the in flow qi. You may have to
change the final time tf.
Use Matlab help to learn more about ode45 and the
other ODE solvers available in Matlab.
function dx=sys1(t,x)
m=1;b=5;k=3;
%to make dh a column vector
dx = zeros(2,1);
%computing derivatives
dx(1)=x(2);
dx(2)= -(k/m)*x(1)-(b/m)*x(2);
Step 2:
The simplest format of ode45 is given by
[t,x] = ode45(odefun,tspan,x0)
where odefun is the function that provides the derivatives,
tspan a vector indicating the initial time and final time,
and yo is the initial value of vector x.
Second Order Differential Equations
For the above system, to use ode45, type
(a) System without an input (u=0) To begin let us
consider a system without an input u. The system is
described by the equation
dy
 f ( y , t ) with y(0)=yo
dt
Please note that if the initial condition yo=0 then the
solution y(t) is zero for all t. In the following two examples
you can change the initial conditions and observe how the
system behaves and reach the equilibrium point.
Example 2.
A spring, mass and damper system without an external
force acting on it is described by the equation
mx  bx  kx  0 with the initial values of x’ and x
specified. This second order equation can be expressed as
a system of first order coupled differential equations as
follows (taking x1=x)
dx1
 x2
dt
dx 2
b
k
  x 2  x1
dt
m
m
with the initial values of x1 and x2 specified. Note that x1
is the displacement and x2 is the velocity. Initially we can
displace the mass and let it free to move, or give an initial
velocity too.
To simulate this system (taking m=1, b=.5 and k=3) we
use the Matlab function ode45.
Step 1:
At each time step ode45 needs to compute the derivatives
as needed in Runge-Kutta methods, and this is given to the
>>[t,x]=ode45(@sys1,[0:0.1:10],[5 0]);
simulates from t=0 to t=10 with initial values of x1=5
and x2=0. Step size is 0.1.
>>plot(t,x)
You can also try to plot x1 against x2.
>> plot(x(:,1),x(:,2))
This is called the phase plane plot. It clearly shows how
the system comes to the equilibrium from different initial
points. Use the hold command
>> hold
and draw the phase plane curves for different initial values.
Note the response of the system. Change the values of m,b
and k, and try to obtain different responses of the system.
Also observe the variation in the time step, where the step
size is not constant.
Example 3
The previous system is a linear system. A well known nonlinear system is the Van der Pols oscillator described by
the differential equation
d 2x
dx
 ( x 2  1)  x  0
2
dt
dt
This can be written as two differential equations as follows
(taking x1=x)
dx1
 x2
dt
dx 2
 ( x12  1) x 2  x1
dt
Write a function to pass the derivatives to the ode45 and
simulate the system for different initial values of x1 and x2.
Draw a phase plane curves for this system. What is the
difference of this oscillator compared to a simple
pendulum.
(b) System with an input ( u (t )  0 )
A system with a driving force or input function can be
described by
dy
 f ( y, u, t )
dt
with y(0)=yo
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
modification.
Example 4
System is same as in example 1, but driven by an input.
The respective equation is given by
mx   bx   kx  f (t ) where f(t) is the input function
or the driving force. The respective system of equation is
given by
dx1
 x2
dt
dx 2
b
k
 f (t )  x 2  x1
dt
m
m
Let us give a sinusoidal input to the system
f(t)= A sin(wt)
Step 1
Create a Matlab function for the input function as follows
Function u=finp(t)
A=1
w=2
u=A*sin(w*t)
and store in finp.m
Step 2:
Create a function to provide the derivatives to the ode45 in
every iteration as follows (similar to in example 1 but with
a change to accommodate the input function)
function dx=sys2(t,x)
m=1;b=5;k=3;
%evaluate the external function %value
for a given value of t
u=feval(‘finp’,t)
%to make dh a column vector
dx = zeros(2,1);
%computing derivatives
dx(1)=x(2);
dx(2)= u -(k/m)*x(1)-(b/m)*x(2);
Step 3:
As before run the ode45 as follows
>>[t,x]=ode45(@sys2,[0 10],[5 0]);
Step 4:
Since the input generated is not available it is generated
by the following commands.
>>for i=1:length(t),u(i)=feval(‘finp’,t(i)); end
>> plot(t,u,t,x)
Investigate the behaviour of the system for different input
functions such as, impulse, step, random inputs etc., by
creating different input function files or having them on
the same file and select the appropriate one.
At the end of this lab you should know
1. To create a system of differential equations for a given
higher order non-linear differential equation.
2. To write the Matlab functions to provide the derivatives
to the ode45
3. To provide different input functions to ode45
4. simulate a system using ode45
5. plot the phase plane curves of the system and recognize
the behaviour of the system..
Exercise 2
Simulate the second order interactive tank system
described by
dh1
 Fi  a12 (h1  h2 )
dt
dh
A2 2  a12 (h1  h2 )  a 2 h2
dt
A1
Indicate clearly how you would handle the situation
during the simulation if h1<h2. Study the behaviour of
this system for different types of inputs Fi. Can you
select A1, A2, a12 and a2 such the system would exhibit
damped oscillations for a step input?
Typical set of values to begin with A1=1, A2=0.5, ,
a2=0.1, a12=0.3.
Download