You are on page 1of 30

1

This is the script that used for the animation of the four bar linkage.
In reality, the difference from the scripts that ye have been using is the lines at the end where
to create the axis,
axis([-40 180 -80 80]);
so that the figure doesnt resize each time, and then I plot the lines;
Note, the each line is given a name or handle, so that it can be referred to later to delete it.
h_1_line=line([0 cos(theta2_rad)*a],[0 sin(theta2_rad)*a]);
h_2_line=line([cos(theta2_rad)*a
cos(theta2_rad)*a+cos(theta_3)*b],[sin(theta2_rad)*a
sin(theta2_rad)*a+sin(theta_3)*b]);
h_3_line=line([d d+cos(theta_4)*c],[0 sin(theta_4)*c]);
Pause so that It can be seen before the next cycle starts
pause(0.001);
and then delete the lines so they can be re-plotted in the new position
delete(h_1_line,h_2_line,h_3_line);

%Script file: four_bar_linkage.m
%
%Purpose
% This program animates the motion of a four bar linkage.
% This can be in either open or closed form
%
%
%
%Record of revisions:
% Date Programmer Description of Change
% ==== ========== =====================
% 25/12/2012 Gerard Nagle Original code
%
%Define Variables:


d=input('what is the value of L1 in mm? ');
a=input('what is the value of L2 in mm? ');
b=input('what is the value of L3 in mm? ');
c=input('what is the value of L4 in mm? ');
%theta_2=input('what is the value of theta2 in degrees? ');
dec=input('Is the linkage open or crossed. please put in O or C? ','s' );

for theta_2=0:0.1:40*pi;

K1=d/a;
K2=d/c;
K3=(a^2-b^2+c^2+d^2)/(2*a*c);
K4=d/b;
K5=(c^2-d^2-a^2-b^2)/(2*a*b);
theta2_rad=theta_2;
A=cos(theta2_rad)-K1-K2*cos(theta2_rad)+K3;
B=-2*sin(theta2_rad);
C=K1-(K2+1)*cos(theta2_rad)+K3;
2

D=cos(theta2_rad)-K1+K4*cos(theta2_rad)+K5;
E=-2*sin(theta2_rad);
F=K1+(K4-1)*cos(theta2_rad)+K5;
if dec == 'O';
theta_4=2*atan((-B-sqrt(B^2-(4*A*C)))/(2*A));
theta_3=2*atan((-E-sqrt(E^2-(4*D*F)))/(2*D));
elseif dec == 'C';
theta_4=2*atan((-B+sqrt(B^2-4*A*C))/(2*A));
theta_3=2*atan((-E+sqrt(E^2-4*D*F))/(2*D));
end;
%theta_4=theta_4*180/pi
%theta_3=theta_3*180/pi

axis([-40 180 -80 80]);
h_1_line=line([0 cos(theta2_rad)*a],[0 sin(theta2_rad)*a]);
h_2_line=line([cos(theta2_rad)*a
cos(theta2_rad)*a+cos(theta_3)*b],[sin(theta2_rad)*a
sin(theta2_rad)*a+sin(theta_3)*b]);
h_3_line=line([d d+cos(theta_4)*c],[0 sin(theta_4)*c]);
pause(0.001);
delete(h_1_line,h_2_line,h_3_line);
end
h_1_line=line([0 cos(theta2_rad)*a],[0 sin(theta2_rad)*a]);
h_2_line=line([cos(theta2_rad)*a
cos(theta2_rad)*a+cos(theta_3)*b],[sin(theta2_rad)*a
sin(theta2_rad)*a+sin(theta_3)*b]);
h_3_line=line([d d+cos(theta_4)*c],[0 sin(theta_4)*c]);

As from the note, this is a script to sum the terms from the problem posed.
%Script file: sum_n_terms.m
%
%Purpose
% This program calculates the sum of the n terms of the equation given in
% the text
%
%
%
%Record of revisions:
% Date Programmer Description of Change
% ==== ========== =====================
% 25/12/20212 Gerard Nagle Original code
%
%Define Variables:
% n -Number of terms
% S -The sum output

n = input('Enter the number of terms ');
S=0;
for k=1:n
S=S+(-1)^k*k/(2^k);
end
fprintf('The sum of the series is: %f',S)



3

As from the note, this is a script to sum calculate sin from the Taylor expansion from the
problem posed.
%Function file: Tsin.m
%
%Purpose
% This program calculates the sin(x) with input arguments of x in degrees
% and n, the number of terms in the Taylor series
%
%
%
%Record of revisions:
% Date Programmer Description of Change
% ==== ========== =====================
% 25/12/20212 Gerard Nagle Original code
%
%Define Variables:
% x -Angle in degrees
% n -Number of terms
% y -The output

function y=Tsin(x,n)
xr = x*pi/180;
y = 0;
for k = 0:n-1
y = y + (-1)^k*xr^(2*k+1)/factorial(2*k+1);
end

Working onwards from the notes, we can create a generic algorithm for Eulers Method.
Step 1.
Set a counter 1 k =
Step 2.
Set a time step h
Step 3. Set an initial time x
Step 4. Set an initial value y
Step 5. Calculate
0
K y =
( )
1
, K hF x y =
Step 6. Calculate y at x h + by
( ) ( )
0 1
y x h K K + = +
Step 7.
Increase the counter from k to 1 k +
Step 8.
Set
( ) y y x h = + and x x h = +
Step 9. Repeat Steps 5 to 8
Step 10. Continue until number of steps required are competed

This is seen in code on the next page



4

%Function file: Ger_euler.m
%
%Purpose
% This program uses Eulers method to calculate the solution of an
% equation
%
%
%
%Record of revisions:
% Date Programmer Description of Change
% ==== ========== =====================
% 25/12/20212 Gerard Nagle Original code
%
%Define Variables:
% x -Angle in degrees
% n -Number of terms
% y -The output

k=2;
h=0.2;
x(1)=0;
y(1)=1;
n=6;
while k <= n
K0=y(k-1);
K1=h*(x(k-1)-y(k-1));
y(k)=K0+K1;
x(k)=x(k-1)+h;
k=k+1;
end

These differential equations can also be solved in Matlab by the use of functions and
Ordinary Differential Solvers (ODEs). The example above was for
y y x ' + =
Or
dy
y x
dx
+ =
The exact solution to this first order differential equation is
( ) 1 2
x
Y x x e

= +
So, this is first tackled in Mathlab by creating a function
function dydt=OEDprob1(x,y)
dydt=x-y;

Then this function is called from the command window by the command
[t,y] = solver(odefun,tspan,y0,options)
The basic input arguments are
5


Argument Meaning
odefun function handle that evaluates the system of ODEs. The function has
the form
dydt = odefun(t,y)
where t is a scalar, and dydt and y are column vectors.
tspan Vector specifying the interval of integration. The solver imposes the
initial conditions at tspan(1), and integrates from tspan(1) to
tspan(end).
y0 Vector of initial conditions for the problem
See also Initial Value Problems.
options Structure of optional parameters that change the default integration
properties.
Integrator Options tells you how to create the structure and describes
the properties you can specify.

The output arguments contain the solution approximated at discrete points:
Argument Type
t Column vector of time points
y Solution array. Each row in y corresponds to the solution
at a time returned in the corresponding row of t.

where solver is one of the ODE solver functions listed below.
Solver Solves These Kinds of Problems Method
ode45
Nonstiff differential equations Runge-Kutta
ode23
Nonstiff differential equations Runge-Kutta
ode113
Nonstiff differential equations Adams
ode15s
Stiff differential equations and DAEs NDFs (BDFs)
ode23s
Stiff differential equations Rosenbrock
ode23t
Moderately stiff differential equations and
DAEs
Trapezoidal rule
ode23tb
Stiff differential equations TR-BDF2
ode15i
Fully implicit differential equations BDFs

Then we can call it from the Mathlab command window
[x,y]=ode23(@OEDprob1,[0 1], 1);
Then plot it
6

plot(x,y)
and we get

We can use this command to pick points and see what their values are
XData=get(get(gca,'children'),'XData');
YData=get(get(gca,'children'),'YData');
y_0p4 = interp1(XData,YData,0.4,'spline')

Which returns the result y_0p4 = 0.7406
We can compare the results as follows;
x Euler numerical Matlab Exact
0.0 1.000 1.000 1.000
0.2 0.800 0.8374 0.837
0.4 0.680 0.7406 0.741
0.6 0.624 0.6976 0.698
0.8 0.619 0.6986 0.699
1.0 0.655 0.7357 0.736

Second Order Runge-Kutta
The basis use of this method is to use information that is available, but has been neglected by
the Euler formula. Looking at the equation
( )
'
, y F x y =
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1
x
y
7

The Euler formula approximates
1 k
y
+
by evaluating
( ) , F x y , which is at a single point.
However,
( ) , F x y is known at every point in the
( ) , x y plane and it is this that is utilised by
the Runge-Kutta formulas.

Looking at a difference approximation of the equation above, we have that
( ) ( )
1
, h,
i i
i i i i
y y
F x y F x y h
h
o | o
+

= + + +
Where , , , o | o are yet to be determined. There is a solution where 1 o = and 0 | = which
bring us back to Eulers formula. Expanding the above, we get
( ) ( )
1
, h,
i i i i i i
y y hF x y hF x y h o | o
+
= + + + +
Using a Taylor expansion and the following notation
x
F
F
x
c
=
c
,
y
F
F
y
c
=
c
,
2
2 xx
F
F
x
c
=
c
,
xy
F
F
x y
c
=
c c
,
2
2 yy
F
F
y
c
=
c

We have then
( ) ( ) ( ) ( )
( ) ( ) ( )
( )
2 2 2 2 2
3
h, , h , ,
1
, 2 , ,
2
i i i i x i i y i i
xx i i xy i i yy i i
F x y h F x y F x y hF x y
h F x y h F x y h F x y
O h
o o
o o
+ + = + +
( + + +

+

The above into the previous equation gives us
( ) ( ) ( ) ( )
( ) ( ) ( )
( )
2
1
3
2 2
4
1
, 2 , 2 ,
2
3 , 6 , 3 ,
6
i i i i x i i y i i
xx i i xy i i yy i i
y y h F x y h F x y F x y
h
F x y F x y F x y
O h
o | | |o
| |o |o
|
+
( = + + + +

( + + +

+

The above can be seen of the form
( )
3
2 4
1 1 2 3
1
2 6
i i
h
y y hF h F F O h
+
= + + + +
Where the coefficients
1 2 3
, and F F F are the coefficients from the previous equation. The
above equation is also suggestive of a Taylor expansion for a function of one variable. So,
8

looking at the original equation
( )
'
, y F x y = , we can suppose that the exact solution of it is
( ) Y x and so we can take the Taylor expansion of it. This leads to
3
2
1
1
...
2 6
i i i i i n
h
Y Y hY h Y Y R
+
' '' '''
= + + + + +
From
( ) , Y F x Y
'

Also, by using the chain rule and implicit differentiation, we have that
x y
Y F F F
''
+
( )
2
2
2
xx xy yy x y y
Y F FF F F F F F F ''' + + + +
Which can be substituted back in to the previous equation to give
( ) ( ) ( ) ( )
( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )
( )
2
1
3
2 2
4
1
, , , ,
2
, 2 , , , , , , , ,
6
i i i x i i y i i i i
xx i i xy i i yy i x i y i i y i
Y Y hF x Y h F x Y F x Y F x Y
h
F x Y F x Y F x Y F x Y F x Y F x Y F x Y F x Y F x Y
O h
+
( = + + +

(
( + + + + + (

(

+

So the equation of
1 i
y
+
is an expansion of the numerical solution, whilst
1 i
Y
+
is an expansion
of the exact solution. So, now our issue is to choose , , , o | o so that if
i i
y Y = , then
1 i
y
+

agrees in some way with
1 i
Y
+
.
There is the choice of 1 o = and 0 | = which allows
1 i
y
+
and
1 i
Y
+
to agree up to, but not
including the
2
h term. This is once again Eulers formula.
If there is an need to expand to this to higher terms, such as
2
h , then only the corresponding
coefficients of both h and
2
h equal. This leads to
1 o | + =
2 2
x y x y
F F F FF | |o + = +
This can be expanded to the following three equations which gives
1 o | + =
2 1 | =
9

2 F |o =
Now, there is an infinite number of solutions to the three equation with four variables above.
However, we can pick one of these that is convenient to us. This is
1
2
o | = =
1 =
( ) , y
i i
F x o =
Which with substitution back in, we get
( ) ( ) ( )
1 1
1 1
, , , y
2 2
i i i i i i i i
y y hF x y hF x y hF x
+ +
= + + +
And this is called the second order Runge-Kutta formula, as the Taylor expansion of both the
numerical and exact solutions are identical through to the terms of order
2
h under the
assumption that
i i
y Y = . This equation is better than the original Euler equation.
Of course there are what are called truncation errors, which is where the exact Taylor
expansion doesnt agree with the numerical solution.
The calculation of the last equation can be done with a three step algorithm, which is as
follows;
Firstly, let
( )
0
, y
i i
K hF x =
Then
( )
1 0 1 0
1 1
,
2 2
i i i i
y y K hF x y K
+ +
= + + +
And then let
( )
1 1 0
,
i i
K hF x y K
+
= +
So that it is got
( )
1 0 1
1
2
i i
y y K K
+
= + +
So these can be combined to show the method. For each 1, 2, 3,..., 1 i n = ,
1 i
y
+
can be
determined by;
10

( )
0
, y
i i
K hF x =
( )
1 1 0
,
i i
K hF x y K
+
= +
( )
1 0 1
1
2
i i
y y K K
+
= + +
This can be shown by an example which uses the initial value problem
( ) , y 0 1 y y x ' + = =
The exact solution to this problem is 1 2
x
Y x e

= + . Working out the three options, the


exact, Euler and the Runge-Kutta, the following table can be constructed
Exact Euler
Runge-Kutta
2
nd
Order
0
0.0 x = 1.000 1.000 1.000
1
0.1 x = 0.910 0.900 0.910
2
0.2 x = 0.837 0.820 0.838
3
0.3 x = 0.782 0.758 0.782
4
0.4 x = 0.741 0.712 0.742
5
0.5 x = 0.713 0.681 0.714
6
0.6 x = 0.698 0.663 0.699
7
0.7 x = 0.693 0.657 0.694
8
0.8 x = 0.699 0.661 0.700
9
0.9 x = 0.713 0.675 0.714
10
1.0 x = 0.736 0.697 0.737

The result above can be extended to Runge-Kutta formulas of orders of up to 10
th
. Of these
the most popular is the 4
th
order

The formula is given by
( )
1 0 1 2 3
1
2 2
6
i i
y y K K K K
+
= + + + +
Where
( )
0
, y
i i
K hF x =
1 0
1 1
h,
2 2
i i
K hF x y K
| |
= + +
|
\ .

11

2 1
1 1
h,
2 2
i i
K hF x y K
| |
= + +
|
\ .

( )
3 1 2
,
i i
K hF x y K
+
= +
This has the benefits of simplicity, relatively high accuracy and an ease of use that makes it
the usual first choice for numerical analysis.
Again, looking back at the equation y y x ' + = where
( ) y 0 1 = , we find that
Exact
Runge-Kutta
4
nd
Order
0
0.0 x = 1.000 1.0000
1
0.1 x = 0.910 0.90967
2
0.2 x = 0.837 0.83746
3
0.3 x = 0.782 0.78164
4
0.4 x = 0.741 0.74064
5
0.5 x = 0.713 0.71306
6
0.6 x = 0.698 0.69762
7
0.7 x = 0.693 0.69317
8
0.8 x = 0.699 0.69866
9
0.9 x = 0.713 0.71314
10
1.0 x = 0.736 0.73576

Which agrees with the exact solution to five decimal places. As before, the numerical and
exact agree to through the
4
h terms and the error is in the order
5
h . In the calculation above,
we have that 0.1 h = so that the error at each calculation step is ( )
5
5
0.1 0.00001 h = = . If the
steps are made even smaller, say 0.01 h = ,we have that the error becomes
( )
5
5
0.01 0.0000000001 h = =
Runge-Kutta: A System of First Order Equations
Again, the above can be extended to a system of first order equations. The initial focus will
be on a system of two first order equations, but the result can be quite readily extended to any
number of first order equations; So, taking these two equations;
( ) ( ) , , , y 0 y F x y v o ' = =
( ) ( ) , , , 0 v G x y v v | ' = =
12

As there are two equations, then there must be a set of Ks for each, hence two sets of K.
Rather than calling both K, one shall be called K and the other M for ease of identity.
So, we then have
( )
0
, y , v
i i i
K hF x =
1 0 0
1 1 1
h, , v
2 2 2
i i i
K hF x y K M
| |
= + + +
|
\ .

2 1 1
1 1 1
h, , v
2 2 2
i i i
K hF x y K M
| |
= + + +
|
\ .

( )
3 1 2 2
, , v
i i i
K hF x y K M
+
= + +
And
( )
0
, y , v
i i i
M hG x =
1 0 0
1 1 1
h, , v
2 2 2
i i i
M hG x y K M
| |
= + + +
|
\ .

2 1 1
1 1 1
h, , v
2 2 2
i i i
M hG x y K M
| |
= + + +
|
\ .

( )
3 1 2 2
, , v
i i i
M hG x y K M
+
= + +
The calculation of both
1 i
y
+
and
1 i
v
+
is got by
( )
1 0 1 2 3
1
2 2
6
i i
y y K K K K
+
= + + + +
( )
1 0 1 2 3
1
2 2
6
i i
v v M M M M
+
= + + + +
A generic pseudo code for the previous can be written as
Step 1.
Set a counter 1 k =
Step 2. Set a time step h
Step 3. Set an initial time x
Step 4. Set an initial value y
Step 5. Calculate
( )
0
, y , v
i i i
K hF x =
( )
0
, y , v
i i i
M hG x =
1 0 0
1 1 1
h, , v
2 2 2
i i i
K hF x y K M
| |
= + + +
|
\ .

13

1 0 0
1 1 1
h, , v
2 2 2
i i i
M hG x y K M
| |
= + + +
|
\ .

2 1 1
1 1 1
h, , v
2 2 2
i i i
K hF x y K M
| |
= + + +
|
\ .

2 1 1
1 1 1
h, , v
2 2 2
i i i
M hG x y K M
| |
= + + +
|
\ .

( )
3 1 2 2
, , v
i i i
K hF x y K M
+
= + +
( )
3 1 2 2
, , v
i i i
M hG x y K M
+
= + +

Step 6. Calculate y at x h + by and v at x h + by
( ) ( )
0 1 2 3
1
2 2
6
y x h y K K K K + = + + + +
( ) ( )
0 1 2 3
1
2 2
6
v x h v M M M M + = + + + +
Step 7. Increase the counter from k to 1 k +
Step 8.
Set
( ) y y x h = + ,
( ) v v x h = + and x x h = +
Step 9. Repeat Steps 5 to 8
Step 10. Continue until number of steps required are competed
Step 11 Stop the calculation

Example
Use the above to find the numerical solution on 0 1 x s s with 0.05 h = of the equations
( )
2 2
1 , y 0 0 y v y v ' = + =
( )
2 2
1 , 0 1 v y y v v ' = =
The exact solution is
( ) sin y x = and
( ) cos v x =
Runge-Kutta: Second Order Differential Equation
Yet again, the formulas can be extended to second order differential equations. So, lets look
at the following initial value problem;
( ) , , y f x y y '' ' =
( ) ( ) 0 , 0 y y o | ' = =
This can be converted to the following equivalent system;
( ) , 0 y v y o ' = =
( ) ( ) , , , 0 v f x y v v | ' = =
14

And this is now a system of first order differential equations. The previous equations can be
applied, but a simplification can also be made to reduce the requirements. In the equation
y v ' = , then
( ) , , F x y v v = is independent of x and y. Therefore we can write that
0
v
i
K h =
1 0
1
v
2
i
K h M
| |
= +
|
\ .

2 1
1
v
2
i
K h M
| |
= +
|
\ .

( )
3 2
v
i
K h M = +
Which shows that only the Ms are needed to be calculated. These are still given by
( )
0
, y , v
i i i
M hf x =
1 0
1 1 1
h, v , v
2 2 2
i i i i
M hf x y h M
| |
= + + +
|
\ .

2 0 1
1 1 1 1
h, v , v
2 2 4 2
i i i i
M hf x y h hM M
| |
= + + + +
|
\ .

3 1 1 2
1
, v , v
2
i i i i
M hf x y h hM M
+
| |
= + + +
|
\ .

The calculation of both
1 i
y
+
and
1 i
v
+
is from the equations
( )
1 0 1 2
1
v
6
i i i
y y h h M M M
+
= + + + +
( )
1 0 1 2 3
1
2 2
6
i i
v v M M M M
+
= + + + +
This can be seen by application to an example.
Example
In this case the second order differential equation
2
3 5 y xy y x '' ' + + = +
( ) ( ) 0 1, 0 0 y y' = =
It is required to approximate y and y' on 0 1 x s s with 0.1 h =
15


The second order equation can be converted to an equivalent system as follows
, y v ' =
( ) 0 1 y =
2
3 5 xv y, v x ' = +
( ) 0 0 v =
Hence it is seen that
( )
2
, , 3 5 xv y f x y v x = + .
Since
0
1 y = and
0
0 v = it is seen that at 0 i = and that
1
0.1 x = that the Ms become
( ) ( ) ( )
0 0 0 0
, y , v 0.1 0,1, 0 0.2000 M hf x f = = =
( ) ( )
1 0
1 1 1
h, v , v 0.1 0.05,1, 0.1 0.2008
2 2 2
i i i i
M hf x y h M f
| |
= + + + = =
|
\ .

( ) ( )
2 0 1
1 1 1 1
h, v , v 0.1 0.05,1.005, 0.1004 0.2002
2 2 4 2
i i i i
M hf x y h hM M f
| |
= + + + + = =
|
\ .
( ) ( )
3 1 1 2
1
, v , v 0.1 0.1,1.01, 0.2002 0.2020
2
i i i i
M hf x y h hM M f
+
| |
= + + + = =
|
\ .

And then it is calculated that
( ) ( )( )
1 0 0 0 1 2
1
0.1 v 0.1 1.0100
6
y y M M M = + + + + =
( )
1 0 0 1 2 3
1
2 2 0.2007
6
v v M M M M = + + + + =
The counter can be indexed through to give 1 i = and that
1
0.2 x = which gives
( )
0 1 1 1
, y , v 0.2020 M hf x = =
( ) ( )
1 1 1 1 1 0
0.1 0.05, 0.05v , v 0.5 0.2047 M f x y M = + + + =
( ) ( )
2 1 1 1 0 1 1
0.1 0.05, 0.05v 0.025 , v 0.5 0.2042 M f x y M M = + + + + =
( ) ( )
3 2 1 1 1 1 2
0.1 , 0.1v 0.025 , v 0.2079 M f x y M M = + + + =
And then it is calculated that
2
1.0403 y =
2
0.4053 v =
This is continued on and the results are
16


y
y'
0
0.0 x = 1.0000 0.0000
1
0.1 x = 1.0100 0.2007
2
0.2 x = 1.0403 0.4053
3
0.3 x = 1.0913 0.6176
4
0.4 x = 1.1642 0.8410
5
0.5 x = 1.2600 1.0783
6
0.6 x = 1.3804 1.3318
7
0.7 x = 1.5270 1.6028
8
0.8 x = 1.7015 1.8921
9
0.9 x = 1.9060 2.1996
10
1.0 x = 2.1420 2.5246

And as before, this can be written in psuedocode as follows
Step 1. Set a counter 1 k =
Step 2.
Set a time step h
Step 3. Set an initial time x
Step 4. Set an initial value y and v
Step 5. Calculate
( )
0
, y, v M hf x =
1 0
1 1 1
h, ,
2 2 2
M hfx x y hv v M
| |
= + + +
|
\ .

2 0 1
1 1 1 1
h, , v
2 2 4 2
i
M hf x y hv hM M
| |
= + + + +
|
\ .

3 1 2
1
, , v
2
M hf x h y hv hM M
| |
= + + + +
|
\ .


Step 6. Calculate y at x h + by and v at x h + by
( ) ( )
0 1 2
1
6
y x h y hv h M M M + = + + + +
( ) ( )
0 1 2 3
1
2 2
6
v x h v M M M M + = + + + +
Step 7.
Increase the counter from k to 1 k +
Step 8.
Set
( ) y y x h = + ,
( ) v v x h = + and x x h = +
Step 9. Repeat Steps 5 to 8
Step 10. Continue until number of steps required are competed
Step 11 Stop the calculation

All of the above can be taken in to the solution of a pendulum. If the pendulum in the figure
below is considered;
17

x
y
u
O
l
Mass m

It consists of a mass m that is l from the origin. Applying Newtons dynamic equation to it,
we have
F ma =
But arc length is related to angle u which leads to
( )
2
2
d l
a l
dx
u
u = =
The forces that acts on are two fold;
1. The component due to gravity acting on the mass m
1
sin F mg u =
2. A damping force
2
F cu =

Which leads to
sin F mg c u u =
Which reduces to the equation
sin 0 ml c mg u u u + + =
Divide by ml and the above becomes
sin 0
c g
ml l
u u u + + =
Which is subject to initial conditions. These are
18

( ) ( ) 0 , 0 0 u o u = =
Specific values can be put into the two equations above to get the following
( ) 0.3 sin 0 u u u + + =
( ) ( )
1
0 , 0 0
4
u t u = =
A simulation is carried out over the time period of 15 seconds with 0.01 h = . Note that t x =
and y u = . The following results are found
Time Value
0.00 0.78540
3.28 -0.47647
6.49 0.29335
9.68 -0.18156
12.86 0.11259


Others
( )
3
2 8 y xy y '' ' = +
( ) ( ) 0 0 0 y y' = =
2
exact: y x =

Most mathematical models of physical systems in dynamics are higher order differential
equations (single degree of freedom, SDOF) or a set of higher-order differential equations
(multiple degrees of freedom systems, MDOF).
SDOF mass spring damper system
The classic example of a single degree of freedom system is the mass spring system with a
viscous damper. This is typically seen in the diagram below.
M
K
C
( ) F t

19


Now by using Newtonian mechanics, a second order differential equation for the motion of
the system can be created
( ) ( )
( ) ( )
2
2
d x t dx t
M C Kx t F t
dt dt
+ + =
Where
( ) x t is the displacement from the equilibrium position, t is the time variable, M is
the mass, C is the coefficient of viscous damping, K is the spring coefficient and
( ) F t is an
externally applied force that is a function of time.
In the first case, the free vibration of the system shall be considered. In this case, the external
applied force
( ) F t is zero. This creates a homogeneous differential equation. It is convenient
to write the homogeneous differential equation with non-dimensional parameters, which is
obtained by dividing across by the mass M and using the un damped natural frequency
n
K
M
e = . Therefore the equation becomes
( ) ( )
( )
2
2
2
2 0
n n
d x t dx t
x t
dt dt
e e + + =
Where the viscous damping factor is given by
2
n
C
M

e
=
To get a solution to this initial value problem, there is a need to have two initial conditions,
which are;
( )
0
0 x x =
( )
0
0 dx
v
dt
=
This equation is solvable by analytical methods and there are four identifiable scenarios.
These are;
1. 0 = which is the undamped case
2. 0 1 > > which is the underdamped case
3. 1 = which is the critically damped case
4. 1 > which is the overdamped case
Scenario 1
20

In this case, we have the system is undamped with free vibration. The setup is as seen below;
M
K

The equation for this is
( )
( )
2
2
2
0
n
d x t
x t
dt
e + =
Or in dot notation
2
0
n
x x e + =
As we anticipate an oscillatory motion, we need to look for a solution which gives x as a
period function of time. A choice taken is
cos sin
n n
x A t B t e e = +
Or using the trigonometric identity ( )
1 2 3
cos sin sin a b a b a b | + = +
Where
2 2
3 1 2
a a a = +

1 1
2
tan
a
a
|

| |
=
|
\ .
,
Which leads to
( ) sin
n
x C t e = +
Substitution of the above into the original equation verifies that each expression is a valid
solution of the equation of motion. To find either A , B , C or is got from the initial
displacement
0
x or the initial velocity
0
x
Taking equation cos sin
n n
x A t B t e e = + , if
0
x and
0
x are evaluated 0 t = then we find that
0 0 n
x A x Be = =
Which leads to
0
0
cos sin
n n
n
x
x x t t e e
e
= +
21

For the equation
( ) sin
n
x C t e = + , the constants can be found in the same manner.
0 0
sin cos
n
x C x C e = =
Which when solved for both C and produces
2
2 0
0
n
x
C x
e
| |
= +
|
\ .

1 0
0
tan
n
x
x
e


| |
=
|
\ .

Which substitution of the these back into the previous equations
2
2 1 0 0
0
0
sin tan
n
n
n
x x
x x t
x
e
e
e

| | | | | |
= + +
| | |
|
\ . \ . \ .

This can be extended to the use of a damper in the system. In dot notation we have that
2
2 0
n n
x x x e e + + =
As before, we assume a solution and then see if it is correct. Therefore, we assume the
solution is
t
x Ae

=
Which if substitute back into the equation we then have
2 2
2 0
n n
e e + + =
Which is then solved by finding the roots of this equation
( )
2
1
1
n
e = +
( )
2
2
1
n
e =
By superposition, the general solution is
1 2
1 2
t t
x Ae A e

= +
( ) ( )
2 2
1 1
1 2
n n
t t
x Ae A e
e e +
= +
22

Looking at this solution, we can see that for the scenario of the system being over damped,
then the roots are distinct and real negative numbers. As a consequence, there is no
oscillation and hence no period associated with the motion.
If the system is critically damped, then the roots are equal and real negative numbers
1 2 n
e = =
And the solution to the differential equation for this special case is given by
( )
1 2
n
t
x A At e
e
= +

The other scenario is underdamped. If this is the situation, then
2
1 is negative and also
using the properties of exponentials, we have also
( ) a b a b
e e e
+
= , we can manipulate the
previous equation for x to the following
( )
2 2
1 1
1 2
n n n
i t i t t
x Ae A e e
e e e
= +
As the term
2
1
n
e appears, it is useful to write it as follows
2
1
d n
e e =
Therefore, the equation becomes
( )
1 2
d d n
i t i t t
x Ae A e e
e e e
= +
Now, using the Euler formula that cos sin
x
e x i x

= , the previous equation becomes


( ) ( )
1 2
cos sin cos sin
n
t
d d d d
x A t i t A t i t e
e
e e e e

= + + (


( ) ( )
1 2 1 2
cos sin
n
t
d d
x A A t i A A t e
e
e e

= + + (


Which leads to
| |
3 4
cos sin
n
t
d d
x A t A t e
e
e e

= +
Where
3 1 2
A A A = + and
( )
4 1 2
A i A A = . Also, as seen previously, we can take
3 4
cos sin
d d
A t A t e e + and reduce it via a trigonometric function to that of a single function
with a phase angle
( ) sin
n
t
d
x C t e
e
e

= + (


23

Or in a standard form
( ) sin
n
t
d
x Ce t
e
e

= +
Which in the equation above is the representation of an exponentially decreasing harmonic
function.
An mathematical aside.
The basis for all that weve done above is from when the roots are complex of the equation
2
2
0
d y dy
a b cy
dx dx
+ + = . If we take that
r j o | =
Then
1
r j o | = + and
2
r j o | = . The solution is then of the form;
( ) ( )
( )
j x j x
x j x x j x
x j x j x
y Ce De
Ce e De e
e Ce De
o | o |
o | o |
o | |
+

= +
= +
= +

Taking the following
cos sin
jx
e x j x = +
cos sin
jx
e x j x

=
Which extends to the above as
cos sin
j x
e x j x
|
| | = +
cos sin
j x
e x j x
|
| |

=
Which back into our equation, we have then
( ) ( ) cos sin cos sin
x
y e C x j x D x j x
o
| | | | = + + (


( ) ( ) cos sin
x
y e C D x j C D x
o
| | = + + (


| | cos sin
x
y e A x B x
o
| | = +
Where A C D = + and
( ) B j C D = . So, from all this, we have the following
If the roots are given by r j o | = , then the solution can be written as
24

| | cos sin
x
y e A x B x
o
| | = +
Contrast this to the previous example. To get the values of A and B, we need to know the
initial conditions of the system.
So for this system, we then have that

| |
3 4
cos sin
n
t
d d
x A t A t e
e
e e

= +
At time, 0 t = we have that
0
x x = .
Aside; At time, 0 t = then the terms are as follows;

( )
( )
0
cos 0 1
sin 0 0
1 e

=
=
=

So, we have then,

( ) ( )
( )
( ) ( ) ( )
0
0 3 4
0 3 4
0 3
cos 0 sin 0
1 0 1
n
x A A e
x A A
x A
e
= + (

= + (

=

Also, we have for velocity

0 0
v x =
So we need to get the differential of
0
x . Therefore;
( ) ( )
3 4
cos sin
n n
t t
d d
x A t e A t e
e e
e e

( ( = +



( ) ( ) ( ) ( ) ( ) ( )
( ) ( ) ( ) ( ) ( ) ( )
0 3
4
sin cos
cos sin
n n
n n
t t
d d d n
t t
d d d n
x A t e t e
A t e t e
e e
e e
e e e e
e e e e


(
= +

(
+ +


( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )
0 3 4
0 1 1 1 1 1 0 1
n d n
x A A e e e ( ( = + + +



| | | |
0 3 4 n d
x A A e e = +
And from before we have that
0 3
x A = , so we can solve for
4
A .

| | | |
0 0 4 n d
x x A e e = +
25


0 0
4
n
d
x x
A
e
e
+
=
So the equation
| |
3 4
cos sin
n
t
d d
x A t A t e
e
e e

= + becomes

0 0
0
cos sin
n
t n
d d
d
x x
x x t t e
e
e
e e
e

( | | +
= +
( |
\ .

And using a trigonometric identity, we then have that

2
2 0 0
0
n
d
x x
C x
e
e
| | +
= +
|
\ .


1 0
0 0
tan
d
n
x
x x
e

| |
=
|
+
\ .

So, looking at a system SDOF system, where 3
n
rad
s
e = , 0.2 = , the initial displacement
0
15 x mm = and the initial velocity is
0 0 2
0
mm
v x
s
= = , we can solve it with Euler Integration
method.
Euler Integration method.
Starting with the initial equation, we have

( ) ( )
( )
2
2
0
d x t dx t
M C Kx t
dt dt
+ + =
Divide by M to get

( ) ( )
( )
2
2
2
2 0
n n
d x t dx t
x t
dt dt
e e + + =
But,

( ) dx t
v
dt
=

( ) ( )
2
2
dv t d x t
a
dt dt
= =
So the equation can be rewritten as
26


( )
( ) ( )
2
2 0
n n
dv t
v t x t
dt
e e + + =
Which leads to

( )
( ) ( )
2
2
n n
dv t
v t x t
dt
e e =
Which in Euler Integration is

( )
( )
( )
2
1
1
2
n n n n n n n
n n n n
dv t
v v t v v x t
dt
dx t
x x t x v t
dt
e e
+
+
= + A = + A
= + A = + A

So in iteration form, we have that;
Iteration 1

( )( )( ) ( ) ( )
( )
( )
( ) ( )( )
2
1
1
0 2 0.2 3 0 3 15 0.01 1.35
15 0 0.01 15
n
n
mm
v
s
x mm
+
+
= + =
= + =

Iteration 2

( )( )( ) ( ) ( )
( )
( )
( ) ( )( )
2
1
1
1.35 2 0.2 3 1.35 3 15 0.01 2.68
15 1.35 0.01 14.987
n
n
mm
v
s
x mm
+
+
= + =
= + =

Iteration 3

( )( )( ) ( ) ( )
( )
( )
( ) ( )( )
2
1
1
2.68 2 0.2 3 2.68 3 14.987 0.01 3.99
14.987 2.68 0.01 14.9602
n
n
mm
v
s
x mm
+
+
= + =
= + =

And so on in each iteration to the required end time

Numerical Solution
To solve the problem numerically then a simplification to the second order differential
equation is called for. It is necessary to create a set of two first order differential equations.
So, taking the previous equation

( ) ( )
( )
2
2
2
2 0
n n
d x t dx t
x t
dt dt
e e + + =
27

Can be rewritten as follows

( )
( )
( )
( ) ( )
2
2
n n
dx t
v t
dt
dv t
v t x t
dt
e e
=
=

Which we can again use the Runge Kutta to solve each equation, which is given by

( )
( )
( )
1 1 2 3 4
1
2 1
3 2
4 3
1
2 2
6
,
1 1
,
2 2
1 1
,
2 2
,
n n
n n
n n
n n
n n
v v k k k k
k hf x v
k hf x h v k
k hf x h v k
k hf x h v k
+
= + + + +
=
| |
= + +
|
\ .
| |
= + +
|
\ .
= + +

Where the function is given as

( ) ( ) ( )
2
, 2
n n
f x v v t x t e e =
To solve for displacement we again use the Runge Kutta.

( )
( )
( )
1 1 2 3 4
1
2 1
3 2
4 3
1
2 2
6
1
2
1
2
n n
n
n
n
n
x x k k k k
k hg v
k hg v k
k hg v k
k hg v k
+
= + + + +
=
| |
= +
|
\ .
| |
= +
|
\ .
= +

Where the function is given as

( ) ( ) g v v t =
This can be shown in an exercise.
Consider a SDOF system with an un damped natural frequency of 2 Hz and a damping ratio
of 0.1 =
The initial conditions are an initial displacement of zero and an initial velocity of 0.1m/s
The following MATLAB code demonstrates a solution to the problem.

% SDOF undamped vibration numerical solution
clc
clear
28

tic
omega_natural = 4; % (rad/s) Natural Frequency of SDOF
zeta = 0.4; % Damping Ratio

xo = 0.0; % (m) initial displacement
vo = 50; % (m/s) Initial velocity

h = 0.0001; % (s) Time Step
t_stop = 5; % Finish Time
n_steps = t_stop/h; % Calculation of the number of steps
required

% Preallocate the displacemnt and velocity matrix for speed of solution

x=zeros(n_steps+1,1);
x_d=zeros(n_steps+1,1);

% two arrays are needed for the storage of variables : array x for
displacement,
% array x_d for velocity
x(1) = xo;
x_d(1) = vo;

for n = 1:n_steps
% perform a Runge Kutta integration on this velocity
k1 = h*accel_viscous(x(n), x_d(n), zeta, omega_natural);
k2 = h*accel_viscous(x(n)+0.5*h, x_d(n)+0.5*k1, zeta, omega_natural);
k3 = h*accel_viscous(x(n)+0.5*h, x_d(n)+0.5*k2, zeta, omega_natural);
k4 = h*accel_viscous(x(n)+h, x_d(n)+k3, zeta, omega_natural);

x_d(n+1) = x_d(n) + (k1+2*k2+2*k3+k4)/6;

% perform a Runge Kutta integration on this displacement
L1 = h*(x_d(n));
L2 = h*(x_d(n)+0.5*L1);
L3 = h*(x_d(n)+0.5*L2);
L4 = h*(x_d(n)+L3);

x(n+1) = x(n) + (L1+2*L2+2*L3+L4)/6;
end
% Now plot the variables
time = 0:h:(length(x)-1)*h;
figure(1)
subplot(2,1,1), plot(time, x);
xlabel('\bftime (s)');
ylabel('\bfdisplacement, x(t) (m)');
title('\bfDisplacement versus time');
grid;
subplot(2,1,2),plot(time, x_d);
xlabel('\bftime (s)');
ylabel('\bfvelocity, v(t) (m/s)');
title('\bfVelocity versus time');
grid;
toc



29

function acc = accel_viscous(disp, vel, zeta, omega_natural)
%accel_viscous Calculate the function within the Runge Kutta
% Function accel_viscous calculates the function within the numerical
% solution of the SDOF % free vibration problem
%
% Define variables
% disp - Displacement
% vel - Velocity
% zeta - viscous damping factor
% omega_natural - un damped natural frequency

% Calculate the function
acc = (-2*zeta*omega_natural*vel)-(((omega_natural)^2)*disp);

end



% script file: second_order_eqn_.m
%
%Define Variables
% fun_handle -Handle to function that defines derivative
% tspan -time duration to solve equation
% yo -initial condition for the equations
% t -Array of solution time
% y -Array of solution values

%Get a handle to the function that defines the derivative
fun_handle = @second_order_fn;

%solve the equation over a period of 0 to 5 seconds
tspan = [0 4];

%set the initial conditions
y0(1)=0; %Displacement
y0(2)=0.1; %Velocity

%call the differential equation solver
[t,y] = ode45(fun_handle,tspan,y0);

%plot the results
figure(1);
plot(t,y(:,1),'b-','Linewidth',2);
hold on;
plot(t,y(:,2),'k-.','Linewidth',2);
hold off;
grid on;
title('\bfSolution of Differential Equation');
xlabel('\bfTime (s)');
ylabel('\bf\itx');
legend('y1=x','y2=dx/dt');



30

function [ yprime ] = second_order_fn( t,y )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here

yprime = zeros(2,1);
yprime(1) = y(2);
yprime(2) = -157.9*y(1) - 2.5132*y(2);

end

You might also like