Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

MEEN 364 Parasuram

July 27, 2001


1
HANDOUT M.4 - SOLUTIONS OF ORDINARY DIFFERENTIAL
EQUATIONS
Introduction
The majority of physical systems of interest in this course can be represented in the form
of ordinary differential equations. In order to study the response of the system modeling,
the differential equations must be solved. The general solution of a linear differential
equation is the sum of two components, the particular integral and the complementary
function. Often the particular integral is the steady-state component of the solution of the
differential equation, while the complimentary function, which is the solution of the
corresponding homogenous equation, is the transient component of the solution. Often
the steady state component of the solution has the same form as the driving or (external)
function.
Solution of a second order differential equation
The model of a majority of mechanical systems can be approximated with a second order
differential equation. So the solution of a second order differential equation is important,
as it can be used to study the response of many physical systems.
Case 1: Constant forcing function
Consider, for example a mass spring damper system subjected to a constant force F, as
shown in the following figure:
F
m
k c
The governing differential equation of motion of the mass, m is given by
, ) ( ) ( ) (
. ..
F t kx t x c t x m + + (1)
where dividing the entire equation by m, we have
MEEN 364 Parasuram
July 27, 2001
2
Note that the time dependence is assumed and hence it is omitted for future
manipulations.
.
. ..
m
F
x
m
k
x
m
c
x + + (2)
Let
.
, 2
2
n
n
m
k
m
c

So equation (2) reduces to


. 2
2
. ..
m
F
x x x
n n
+ + (3)
As explained earlier, the solution to the above differential equation has two parts, the
particular integral and the complimentary function. To find the particular integral, which
is nothing but the steady-state solution, equate all of the derivate terms to zero, i.e.,
. 0
, 0
.
..

x
x
Substituting the above relation in equation (3), we have
m
F
x
m
F
x
n
s
n
2
2

(4)
The second of equations (4) represents the steady-state response of the system.
To find the complimentary function or the transient response, we need to consider the
homogenous equation, i.e.,
0 2
2
. ..
+ + x x x
n n
. (5)
Equation (5) is the left hand side of equation (3), with the right hand side set to zero.
To solve the above equation, first assume a solution of the form
MEEN 364 Parasuram
July 27, 2001
3
t
Ae x

. (6)
Substituting the above relation in equation (5), we get
0 2
2 2
+ +
t
n
t
n
t
e A e A e A

(7)
0 ) 2 (
2 2
+ +
t
n n
e A A A

(8)
0
t
e

is a trivial solution, as equation (6) will then be identically equal to zero, i.e.,
0 x . But we need to find the non-trivial solution, so
0
t
e

.
So from equation (8), we have
0 2
2 2
+ +
n n
(9)
Solving the above quadratic equation, we find the following roots
. 1
where
1
2
4 4 2
2
2 , 1
2
2 , 1
2 2 2
2 , 1



n d
d n
n n
n n n
Therefore the transient solution of the system is given as
t t
t
d n d n
Be Ae x
) ( ) ( +
+ (10)
where A and B can be found based on the initial conditions. So the final solution of the
differential equation given by equation (1) is
t t
n
t s
d n d n
Be Ae
m
F
x
x x x
) ( ) (
2

+
+ +
+
For an unforced system, the differential equation is given by
0
. ..
+ + kx x c x m . (11)
MEEN 364 Parasuram
July 27, 2001
4
Since the forcing function is equal to zero, here the steady state response of the system
will be equal to zero, while the transient response will be the same as that of the forced
system. In other words the solution of the equation (11) is given as
t t
t t
t s
d n d n
d n d n
Be Ae x
Be Ae x
x x x
) ( ) (
) ( ) (
0


+
+
+
+ +
+
Case 2: Time varying forcing function
In this case, instead of a constant forcing function driving the system as in case 1, let us
consider a harmonic function, Fe
jwt
, which by Eulers expansion is equal to
) sin (cos t j t F + , driving the system. w is the frequency with which the driving
function is applied on the system. The governing differential equation will be
. 2
2
. ..
. ..
jwt
o
jwt
n n
jwt
e F e
m
F
x x x
Fe kx x c x m
+ +
+ +

(12)
Usually the form of the solution is similar to that of the driving function. In this case, to
obtain the steady state component, we assume a solution of the form
jwt
Ce x .
Substituting the above relation in equation (12), we have
0
2 2
0
2 2
} 2 ) {(
2
F C j
e F e C e j C e C
n n
jwt jwt
n
jwt
n
jwt
+
+ +


(13)
Diving the second equation of equation (13) by
2
n
, results in
.
where
,
} 2 ) 1 {( } 2 ) 1 {(
2 2
2
0
n
n
r
r r
k
F
r r
F
C

Once the value of C is obtained, the steady state solution of the system is
jwt
s
Ce x .
MEEN 364 Parasuram
July 27, 2001
5
The complimentary function or the transient response of this system is the same as that of
the previous case, as the homogenous equation is the same. So the entire solution to the
system subjected to a harmonic driving function is given by
.
where
,
} 2 ) 1 {(
conditions initial from obtained B and A
where
,
2
) ( ) (
n
jwt
t t
s t
r
r r
k
F
C
Ce Be Ae x
x x x
d n d n


+ +
+
+
Using MATLAB to solve the differential equations
The ODE command in MATLAB can be used to solve various differential equations.
The use of this command is explained in detail with the help of the following example.
Example 1
Consider the system below.
m
k c
This system is similar to the one in case 1, except that the forcing function is equal to
zero. Therefore the governing differential equation of motion of the system is
0
. ..
+ + kx x c x m . (14)
MEEN 364 Parasuram
July 27, 2001
6
Problem statement
Solve for the response of the unforced system defined by the differential equation given
by equation (14). Assume = 0.1; m = 1Kg; k = 100 N/m. Let the initial conditions be
chosen as
s m x
m x
/ 0 ) 0 (
02 . 0 ) 0 (
.

Solution
Equation (14) is a second order constant-coefficient differential equation. To solve this
equation we have to reduce it into two first order differential equations. This step is taken
because MATLAB uses a Runge-Kutta method to solve differential equations, which is
valid only for first order differential equations.
Let
.
.
v x (15)
From the above expression we see that
0
.
+ + kx cv v m , (16)
so equation (16) reduces to
]. ) ( ) [(
.
x
m
k
v
m
c
v

(17)
We can see that the second order differential equation (14) has been reduced to two first
order differential equations (15) and (17).
For our convenience, put
x = y (1);
); 2 (
.
y v x
Note that, the variables x and v are the states of the system, and are each equated to
another variable y(1) and y(2) respectively to be compatible with MATLAB. These
variables are not the outputs of the system.
MEEN 364 Parasuram
July 27, 2001
7
Equations (15) and (17) reduce to
) 1 (
.
y = y (2); (18)
) 2 (
.
y = [(-c/m)*y (2) (k/m)*y (1)]; (19)
To calculate the value of c, compare equation (14) with the following generalized
equation.
0 2
2
. ..
+ +
n n
x x .
Equating the coefficients of the similar terms we have
n
m
c
2 (20)
m
k
n

2
. (21)
Using the values of m and k, calculate the value of c corresponding to the value of .
Once the value of c is known, equations (18) and (19) can be solved using MATLAB.
Some of the properties of the system are defined as follows.
Natural frequency
n
= ) / ( m k = 10 rad/sec.
For = 0.1
Damped natural frequency
d
=
n

2
1
= 9.95 rad/sec.
Damped time period T
d
= 2/
d
= 0.63 sec.
Therefore for to plot five time cycles, the time interval should be 5 times the damped
time period, i.e., 3.16 sec.
MATLAB Code
In order to apply the ODE45 or any other numerical integration procedure, a separate
function file must be generated to define equations (18) and (19). Actually, the right hand
side of equations (18) and (19) are what is stored in the file. The equations are written in
the form of a vector.
The MATLAB code is given below.
function yp = unforced(t,y)
yp = [y(2);(-((c/m)*y(2))-((k/m)*y(1)))]; (22)
In the above code yp is a two dimensional vector representing the right hand side of the
equations (18) and (19), respectively. Open a new M-file and write down the above two
MEEN 364 Parasuram
July 27, 2001
8
lines. The first line of the function file must start with the word function and the file
must be saved corresponding to the function call; i.e., in this case, the file is saved as
unforced.m. The derivatives are stored in the form of a vector.
This example problem has been solved for = 0.1. We need to find the value of c/m
and k/m so that the values can be substituted in equation (22). Substituting the values of
and
n
in equations (20) and (21) the values of c/m and k/m can be found out. After
finding the values, substitute them into equation (22). In this example
. 100
1
100
, 2 10 1 . 0 2


m
k
m
c
Now we need to write a code, which calls the above function and solves the differential
equation and plots the required result. First open another M-file and type the following
code.
tspan=[0 4];
y0=[0.02;0];
[t,y]=ode45('unforced',tspan,y0);
figure(1);
plot(t,y(:,1));
grid on
xlabel(time)
ylabel(Displacement)
title(Displacement Vs Time)
The ode45 command in the main body calls the function unforced, which defines the
systems first order derivatives. The response is then plotted using the plot command.
tspan represents the time interval and y0 represents the initial conditions for y(1) and
y(2), which in turn represent the displacement x and the first derivative of x, i.e., the
velocity. In this example, the initial conditions are taken as 0.02 m for x and 0 m/sec for
the first derivative of x or the velocity of the mass m.
MATLAB uses a default step value for the vector tspan. In order to change the step size
use the following code
tspan=0:0.001:4;
This tells MATLAB that the vector tspan is from 0 to 4 with a step size of 0.001. This
example uses an arbitrary step size. If the step size is too large, the plot obtained will not
be a smooth curve. So it is always better to use relatively smaller step size. But it also
takes longer to simulate programs with smaller step size. So you have to decide which is
the best step size you can use for a given problem.
MEEN 364 Parasuram
July 27, 2001
9
In the above code y(:,1) represents the displacement x. To plot the velocity, change the
variable in the plot command line to y(:,2).
The plot is attached below
Note: Any n
th
order differential equation can be solved using MATLAB by following the
procedure outlined in example 1. In other words, the n
th
order differential equation can be
reduced to n first order differential equations and the ode command in MATLAB can
be used to solve the system of n first order differential equations.
MEEN 364 Parasuram
July 27, 2001
10
Example 2. Double Pendulum
Derive the governing differential equation of motion of a double pendulum. Solve the
derived differential equation and plot the values of
1
and
2
with respect to time.
Choose l
1
= 1m; l
2
= 1m; m
1
= m
2
= 5Kg. The initial conditions for
1
and
2
are 0.5233
and 0.5233 radians respectively.

1
l
1
, m
1

2
l
2
, m
2
Solution
When represented in a matrix form, the differential equations of motion for the above
system is


+ +

]
]
]
,

+
) 1 2
2
1
.
1 1 2 2
) 1 2
2
.
2 2 2 1 2 1
..
2
..
1
2 2 1 2 1 2
1 2 2 2 1 1 2
sin( sin
sin( sin ) (
) cos(
) cos( ) (



l m w
l m w w
l m l m
l m l m m
MATLAB Code
These coupled second order differential equations must be converted into a vector of first
order differential equation. This step is done by the following substitutions.
Substituting the above relations in the original nonlinear differential equation, we get the
following first order differential equations,
); 4 (
); 3 (
); 2 (
); 1 (
.
2
2
.
1
1
y
y
y
y


+ +

]
]
]
]
]
]
,
,
,
,

+
) sin( sin
) 4 (
) sin( sin ) (
) 2 (
) 4 (
) 3 (
) 2 (
) 1 (
0 ) cos( 0
0 1 0 0
) cos( 0 ) ( 0
0 0 0 1
1 2
2
1
.
1 1 2 2
1 2
2
.
2 2 2 1 2 1
.
.
.
.
2 2 1 2 1 2
1 2 2 2 1 1 2




l m w
y
l m w w
y
y
y
y
y
l m l m
l m l m m
MEEN 364 Parasuram
July 27, 2001
11
In this type of a problem where the coefficient matrix is a function of the states or the
variables, a separate M-file must be written which incorporates a switch/case
programming with a flag case of mass.
For example if the differential equation is of the form,
) , ( ) ( ) , (
.
y t F t y y t M
then the right hand side of the above equation has to be stored in a separate m-file called
F.m. Similarly the coefficient matrix should be stored in a separate m-file named
M.m. So, when the flag is set to the default, the function F.m is called and later when
the flag is set to mass the function M.m is called.
The code with the switch/case is given below. Note that it is a function file and should be
saved as indmot_ode.m in the current directory.
function varargout=indmot_ode(t,y,flag)
switch flag
case '' %no input flag
varargout{1}=pend(t,y);
case 'mass' %flag of mass calls mass.m
varargout{1}=mass(t,y);
otherwise
error(['unknown flag ''' flag '''.']);
end
To store the right hand side of the state variable matrix form of the model, a separate
function file must be written as shown below. Note that the name of the function is
pend, so this file must be saved as pend.m.
%the following function contains the right hand side of the
%differential equation of the form
%M(t,y)*y'=F(t,y)
%i.e. it contains F(t,y).it is also stored in a separate file named,
pend.m.
function yp= pend(t,y)
M1=5;
M2=5;
g=9.81;
l1=1;
l2=1;
w2=M2*9.81;
w1=M1*9.81;
yp=zeros(4,1);
yp(1)=y(2);
yp(2)=-(w1+w2)*sin(y(1))+M2*l2*(y(4)^2)*sin(y(3)-y(1));
yp(3)=y(4);
yp(4)=-w2*sin(y(3))-M2*l1*(y(2)^2)*sin(y(3)-y(1));
MEEN 364 Parasuram
July 27, 2001
12
Similarly, to store the coefficient matrix a separate function file is written which is stored
as mass.m.
% the following function contains the mass matrix.
%it is separately stored in a file named, mass.m
function m = mass(t,y)
M1=5;
M2=5;
g=9.81;
l1=1;
l2=1;
m1=[1 0 0 0];
m2=[0 (M1+M2)*l1 0 M2*l2*cos(y(3)-y(1))];
m3=[0 0 1 0];
m4=[0 M2*l1*cos(y(3)-y(1)) 0 M2*l2];
m=[m1;m2;m3;m4];
To plot the response, the main file should call the function indmot_ode.m, which has
the switch/case programming which in turn calls the corresponding functions depending
on the value of the flag. For the main file to recognize the coefficient matrix, the
MATLAB command ODESET is used to set the mass to M (t, y).
The MATLAB code for the main file is given below
% this is the main file, which calls the equations and solves using
ode113.
%it then plots the first variable.
tspan=[0 10]
y0=[0.5233;0;0.5233;0]
options=odeset('mass','M(t,y)')
[t,y]=ode113('indmot_ode',tspan,y0,options)
subplot(2,1,1)
plot(t,y(:,1))
grid
xlabel('Time')
ylabel('Theta1')
subplot(2,1,2)
plot(t,y(:,3))
grid
xlabel('Time')
ylabel('Theta2')
For help regarding subplot use the online help in MATLAB by typing the following
statement at the command prompt.
help subplot
subplot(m,n,p), breaks the Figure window into an m-by-n matrix of small axes and
selects the p-th axes for the current plot. The axes are counted along the top row of the
Figure window, then the second row, etc. For example,
MEEN 364 Parasuram
July 27, 2001
13
subplot(2,1,1), plot(income)
subplot(2,1,2), plot(outgo)
plots income on the top half of the window and outgo on the bottom half.
The plot has been attached.
Assignment
1) For the system shown below,

MEEN 364 Parasuram
July 27, 2001
14
l
m
m = 10 g;
l = 5 cms;
The initial conditions are (0) = 90 and 0 ) 0 (
.

a) Derive the governing differential equation.
b) Solve the above differential equation obtained analytically and plot the value of theta
with respect to time.
c) Solve the differential equation using MATLAB and again plot the value of theta with
respect to time. Compare the results obtained from (2) and (3).
2) For the system shown below
F
m
k c
Using MATLAB, plot the response of a forced system given by the equation
t f kx x c x m sin
. ..
+ +
For = 0.1.
Take m = 5 kg; k = 1000 N/m; f = 50 N; = 4
n
, x(0) = 5 cms;
.
x (0) = 0.
MEEN 364 Parasuram
July 27, 2001
15

You might also like