You are on page 1of 30

Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

ME-406
SIMULATION LAB
Introduction to MATLAB

The name MATLAB stands for Matrix Laboratory. MATLAB was written originally to provide easy
access to matrix software developed in 1984 and is now considered as a standard tool at most
universities and industries worldwide.
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming environment. Furthermore, MATLAB is a modern programming
language environment; it has sophisticated data structures, contains built-in editing and
debugging tools, and supports object-oriented programming. These factors make MATLAB an
excellent tool for teaching and research.
MATLAB has many advantages compared to conventional computer languages (e.g.,C,
FORTRAN) for solving technical problems. MATLAB is an interactive system whose basic data
element is an array that does not require dimensioning.
MATLAB integrates mathematical computing, visualization, and a powerful language to provide
a flexible environment for technical computing. The open architecture makes it easy to use
MATLAB and its companion products to explore data, create algorithms and create custom
tools that provide early insights and competitive advantages. Known for its highly optimized
matrix and vector calculations, MATLAB offers an intuitive language for expressing problems
and their solutions both mathematically and visually. Typical uses include:
Numeric computation and algorithm development.
Symbolic computation (with the built-in Symbolic Math functions).
Modeling, simulation and prototyping.
Data analysis and signal processing.
Engineering graphics and scientific visualization.
It has powerful built-in routines that enable a very wide variety of computations. It also has
easy to use graphics commands that make the visualization of results immediately available.
Special applications are collected in packages referred to as toolbox. There are toolboxes for
signal processing, symbolic computation, control theory, simulation, optimization, and several
other fields of applied science and engineering.

1
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

INTRODUCTION TO SIMULINK
Simulink is a graphical extension to MATLAB for modeling and simulation of dynamic systems.
In Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams
are available, such as transfer functions, summing junctions, etc., as well as virtual input and
output devices such as function generators and oscilloscopes. Simulink is integrated with
MATLAB and data can be easily transferred between the programs.
Steps for using SIMULINK are as follows
1. Starting Simulink
2. Model Files
3. Basic Elements
4. Running Simulations

2
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

ME 406
SIMULATION LAB

LIST OF EXERCISES

1) Simulation of simple pendulum.


2) Simulation of nonlinear pendulum.
3) Single degree of freedom spring mass system with free vibration.
4) Single degree of freedom spring mass system with forced vibration.
5) Single degree of freedom spring-mass-damper system with free vibration.
6) Single degree of freedom spring-mass-damper system with forced vibration.
7) Simulation of forced vibratory system with Non-linear damping.
8) Simulation of Compound pendulum.
9) Two degree of freedom spring-mass-damper system with free vibration.
10) Two degree of freedom free vibratory spring-mass system with forced vibration by
considering friction.
11) Implementation of PID controller in Tuning and control of dynamic systems.
12) Simulation of Simple pendulum using SimMechanics.
13) Simulation of Four bar mechanism using SimMechanics.
14) Simulation of Hydraulic system with Single-Acting Cylinder using SimHydraulics.
15) Simulation of Hydraulic system with Double-Acting Cylinder SimHydraulics.

3
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULATION LAB EXPERIMENTS


1) Write the program for the task given: The motion of the simple pendulum is modeled by the
following equation, solve the differential equation and find the displacement, velocity with
respect to time (from 0 to 120 seconds). Assume the value of ω=1 rad/sec. Analyze the
system by MATLAB Programming and validate the solution by using SIMULINK.

Equation can be written as the system of two differential equations for z=[z1,z2], with
assumptions as

Then the stage comes to reduce the second order differential equation interms of Ѳ to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as expone.m in the current working directory of MATLAB:

Function of the Program:-


Function dzdt=expone(t,z)
w=1;
dzdt=[z(2); -w^2*sin(z(1))];

Main body of the Program:-


clc;
clear all;
w=1;
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('expone',tspan,z0);
x=z(:,1);
v=z(:,2);
a=- w^2*sin(x);
plot(t,x,t,v,t,a);
xlabel(‘time in seconds’);
ylabel(‘x(t),v(t) & a(t)’);
legend ('displacement','velocity',’acceleration’);
title(‘Kinematic analysis of simple pendulum’);

4
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

5
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

2) Write the program for the task given:

Initial conditions are a(0)=0.1; da(0)/dt=1


The motion of the compound pendulum is modeled by the following equation, solve the
differential equation and find the displacement, velocity and acceleration with respect to time
(from 0 to 120 seconds). Analyze the system by MATLAB Programming and validate the
solution by using SIMULINK.

MATLAB PROGRAM:-
Create the function file and save it as exptwo.m in the current working directory of MATLAB:

Function of the program:-


function dzdt=exptwo(t,z);
M=1;
R=1;
g=9.81;
b=1;
dzdt=[z(2);-(b/M*R^2)*z(2)-(g/R)*sin(z(1))];

Main body of the program:-


clc;
clear all;
M=1;
R=1;
g=9.81;
b=1;
z0=[1 0];
tspan=[0 10];
[t z]=ode45('exptwo',tspan,z0);
theta=z(:,1);
thetadot=z(:,2);
thetadott=-(b/M*R^2)*thetadot-(g/R)*sin(theta);
plot(t,theta,t,thetadot,t,thetadott);
xlabel('time in seconds');
ylabel('theta, omega and alpha');
legend('theta','thetadot','thetadott');
title(‘Kinematic analysis of system');

6
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

7
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

3) Write the program for the task given: The motion of the free vibratory spring-mass system is
modeled by the following equation, solve the differential equation and find the displacement,
velocity with respect to time (from 0 to 120 seconds).Analyze the system by MATLAB
Programming and validate the solution by using SIMULINK. Assume m=1 kg, k=2 N/m.

Then the stage comes to reduce the second order differential equation in terms of x to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as expthree.m in the current working directory of MATLAB:

Function of the program:-


function dzdt = expthree(t,z);
m=1;
k=2;
dzdt =[z(2); -(k/m)*z(1)];

Main body of the program:-


clc;
clear all;
m=1;
k=2;
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('expthree',tspan,z0);
x=z(:,1);
v=z(:,2;
acc=-(k/m)*x;
plot(t,x,t,v,t,acc);
xlabel('t');
ylabel('x(t),v(t)and a(t)');
legend('displacement','velocity','acceleration');
title('position, velocity and acceleration variation with respect to time');

8
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

9
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

4) Write the program for the task given: The motion of the forced vibratory system with spring-
mass system is modeled by the following equation, solve the differential equation and find the
displacement, velocity with respect to time (from 0 to 120 seconds).Analyze the system by
MATLAB Programming and validate the solution by using SIMULINK. Assume m=1 kg, k=2
N/m, F=120N and ω=1 rad/sec.

M*(d2x/dt2)+K*x(t)=F*sin(w*t)
Then the stage comes to reduce the second order differential equation in terms of x to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as expfour.m in the current working directory of MATLAB:
Function of the program:-
function dzdt = expfour(t,z);
m=1;
k=2
w=1;
f=120;
dzdt =[z(2); -(k/m)*z(1)+(f/m)*sin(w*t)];

Main body of the program:-


clc;
clear all;
m=1;
k=2;
w=1;
f=120;
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('expfour',tspan,z0);
x=z(:,1);
v=z(:,2;
acc=-(k/m)*x+(f/m)*sin(w*t);
plot(t,x,t,v,t,acc);
xlabel('t');
ylabel('x(t),v(t)and a(t)');
legend('displacement','velocity','acceleration');
title('position, velocity and acceleration variation with respect to time');

10
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

11
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

5) Write the program for the task given: The motion of the free vibratory system with spring-mass-
damping system is modeled by the following equation, solve the differential equation and find the
displacement, velocity with respect to time (from 0 to 120 seconds).Analyze the system by MATLAB
Programming and validate the solution by using SIMULINK. Assume m=1 kg, c=0.5 Ns/m, k=2 N/m.

M*(d2x/dt2)+C*(dx/dt)+K*x(t)=0
Then the stage comes to reduce the second order differential equation in terms of x to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as expfive.m in the current working directory of MATLAB:
Function of the program:-
function dzdt = expfive(t,z);
m=1;
k=2
c=0.5;
dzdt =[z(2); -(k/m)*z(1)-(c/m)*z(2)];

Main body of the program:-


clc;
clear all;
m=1;
k=2;
w=1;
f=120;
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('expfive',tspan,z0);
x=z(:,1);
v=z(:,2;
acc=-(k/m)*x+(c/m)*v;
plot(t,x,t,v,t,acc);
xlabel('t');
ylabel('x(t),v(t)and a(t)');
legend('displacement','velocity','acceleration');
title('position, velocity and acceleration variation with respect to time');

12
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

13
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

6) Write the program for the task given: The motion of the forced vibratory system with spring-
mass-damping system is modeled by the following equation, solve the differential equation and
find the displacement, velocity with respect to time (from 0 to 120 seconds).Analyze the system
by MATLAB Programming and validate the solution by using SIMULINK. Assume m=1 kg,
c=0.5 Ns/m, k=2 N/m, ω=1 rad/sec and F=120 N.

M*(d2x/dt2)+C*(dx/dt)+K*x(t)=F*sin(ω*t)
Then the stage comes to reduce the second order differential equation in terms of x to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as expsix.m in the current working directory of MATLAB:
Function of the program:-
function dzdt = expsix(t,z);
m=1;
k=2
c=0.5;
w=1;
f=120;
dzdt =[z(2); -(k/m)*z(1)-(c/m)*z(2)];
Main body of the program:-
clc;
clear all;
m=1;
k=2;
w=1;
f=120;
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('expsix',tspan,z0);
x=z(:,1);
v=z(:,2;
acc=-(k/m)*x+(c/m)*v;
plot(t,x,t,v,t,acc);
xlabel('t');
ylabel('x(t),v(t)and a(t)');
legend('displacement','velocity','acceleration');
title('position, velocity and acceleration variation with respect to time');

14
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

15
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

7) Write the program for the task given: The motion of the forced vibratory system with
nonlinear damping system is modeled by the following equation, solve the differential equation
and find the displacement, velocity with respect to time (from 0 to 120 seconds).Analyze the
system by MATLAB Programming and validate the solution by using SIMULINK. Assume
m=10 kg, CD=2 N/m/s, F(t)=12*sin(5*t)N.

m*(d2x/dt2)+CD*(dx/dt)^2=F(t)
The above equation can be rewritten as:

Then the stage comes to reduce the second order differential equation in terms of x to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as exptest.m in the current working directory of MATLAB:
Function of the program:-
function dzdt = exptest(t,z);
m=10;
CD=0.5;
F=12*sin(5*t);
dzdt =[z(2); (F/m)*sin(5*t)-(CD/m)*z(2)^2];
Main body of the program:-
clc;
clear all;
m=10;
CD=0.5;
F=12*sin(5*t);
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('exptest',tspan,z0);
x=z(:,1);
v=z(:,2;
acc=(F/m)*sin(5*t)-(CD/m)*v^2;
plot(t,x,t,v,t,acc);
xlabel('t');
ylabel('x(t),v(t)and a(t)');
legend('displacement','velocity','acceleration');
title('position, velocity and acceleration variation with respect to time');

16
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

17
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

8) Write the program for the task given: The motion of the compound pendulum is modeled by
the following equation, solve the differential equation and find the displacement, velocity
with respect to time (from 0 to 120 seconds).Assume the value of d=0.023 m, m=0.43 kg,
L=0.495 m, J=0.009 kg-m2, c=0.00035, g=9.81. Analyze the system by MATLAB
Programming and validate the solution by using SIMULINK.

The system of above equation can be rewritten as

Equation can be written as the system of two differential equations for z=[z1,z2], with
assumptions as

Then the stage comes to reduce the second order differential equation interms of Ѳ to a first
order differential equation in terms of z which is termed as state-variable method.

MATLAB PROGRAM:-
Create the function file and save it as explearn.m in the current working directory of MATLAB:

Function of the Program:-


Function dzdt=explearn(t,z)
d=0.0023;
m=0.43;
l=0.495;
j=0.009;
c=0.00035;
g=9.81;
dzdt=[z(2); -(c/j)*z(2)-(m*g*d/j)*sin(z(1))];

Main body of the Program:-


clc;
clear all;
d=0.0023;
m=0.43;

18
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

L=0.495;
J=0.009;
c=0.00035;
g=9.81;
z0=[0,1];
tspan =[0,120];
[t,z] = ode45('explearn',tspan,z0);
x=z(:,1);
v=z(:,2);
a= -(c/j)*v-(m*g*d/j)*sin(x);
plot(t,x,t,v,t,a);
xlabel(‘time in seconds’);
ylabel(‘x(t),v(t) & a(t)’);
legend ('displacement','velocity',’acceleration’);
title(‘Kinematic analysis of compound pendulum’);
SIMULINK DESIGN:-

19
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

9) Write the program for the task given: The motion of the Two Degree of freedom free
vibratory system with spring-mass-damping system is modeled by the following
equation, solve the differential equation and find the displacement, velocity with respect
to time (from 0 to 120 seconds). Analyze the system by MATLAB Programming and
validate the solution by using SIMULINK. Assume m1=4 kg, m2=6 kg,
k1=2N/m,k2=3N/m, c1=1.5Ns/m, c2=3.0 Ns/m.

MATLAB PROGRAM:-
Create the following function file and save it as twodof.m in the current working
directory of Matlab:
Function of the Program:-
function dxdt=twodof(t,x);
c1=1.5;
c2=3.0;
k1=2;
k2=3;
m1=4;
m2=6;
dxdt=[x(2);-(c1/m1)*(x(2)-x(4))-(k1/m1)*(x(1)-x(3));x(4);-(c1/m1)*(x(4)-x(2))
(c2/m2)*x(4)-(k1/m2)*(x(3)-x(1))-(k2/m2)*x(3)];

20
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

Main Body of the Program:-


clc;
clear all;
tspan=0:1:60;
x0=[10,0,0,0]; %start with cart 1 deflected 10mm from equilibrium
c1=1.5;
c2=3.0;
k1=2;
k2=3;
m1=4;
m2=6;
[t,x]=ode45('twodof',tspan,x0);
disp1=x(:,1);
vel1=x(:,2);
disp2=x(:,3);
vel2=x(:,4);
acc1=(k1/m1)*(disp2-disp1)+(c1/m1)*(vel2-vel1);
acc2=-(k1/m2)*(disp2-disp1)-(c1/m2)*(vel2-vel1)-(k2/m2)*disp2-(c2/m2)*vel2;
plot(t,disp1,t,vel1,t,acc1);
%plot(t,disp2,t,vel2,t,acc2);
xlabel('Time');
ylabel('Cart 1 and cart2 behaviour of x1 v1 x2 v2');
legend('x1','v1','x2','v2');
title('two dof system behaviour');

NOTE:-There will be test to check the understanding level posing question on Two degree of
freedom Free and forced vibratory system on spring-mass and spring-mass-damper system.
Students should be able to obtain the solution for exercise by MATLAB Programming and
validate your Solution by Simulink Design.

21
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

NOTE:- This is TEST-1 for Understanding to build Simulink Design for the Experiment 9.

22
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

10) Write the program for the task given: The motion of the Two Degree of freedom
forced vibratory system with spring-mass system by considering friction is modeled by
the following equation, solve the differential equation and find the displacement, velocity
with respect to time (from 0 to 120 seconds). Analyze the system by MATLAB
Programming and validate the solution by using SIMULINK. Assume M1=1kg, M2=2kg,
k=1N/m, F=1; mu=0.002; g=9.81.

MATLAB PROGRAM:-
Create the following function file and save it as twodofwithfriction.m in the current
working directory of Matlab:
Function of the Program:-
function dzdt=twodofwithfriction(t,z,mu,g,f,k,m1,m2);
mu=0.02;
g=9.81;
f=1;
k=1;
m1=1;
m2=2;
dzdt=zeros(4,1);
dzdt(1)=z(2);
dzdt(2)=-(k/m1)*(z(1)-z(3))-mu*g*z(2)-(f/m1)
dzdt(3)=z(4);
dzdt(4)=-(k/m2)*(z(1)-z(3))-mu*g*z(4);

23
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

Main Body of the Program:-


clc;
clear all;
newstyle=@twodofwithfriction;
tspan=0:1:100;
z0=[10,0,0,0]; %start with cart 1 deflected 10mm from equilibrium
mu=0.02;
g=9.81;
f=1;
k=1;
m1=1;
m2=2;
[t,z]=ode45('twodofwithfriction',tspan,z0);
disp1=z(:,1);
vel1=z(:,2);
disp2=z(:,3);
vel2=z(:,4);
acc1=-(k/m1)*(disp1-disp2)-mu*g*vel1-(f/m1);
acc2=-(k/m2)*(disp1-disp2)-mu*g*vel2;
plot(t,disp1,t,vel1,t,acc1,t,disp2,t,vel2,t,acc2);
xlabel('Time');
ylabel('Cart 1 and cart2 behaviour of x1 v1 x2 v2');
legend('x1','v1','x2','v2');
title('two dof system behaviour by considering friction');

24
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

SIMULINK DESIGN:-

25
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

11) Implementation of PID controller in Tuning and control of dynamic systems

26
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

12) Simulation of Simple pendulum using SimMechanics.

27
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

13.) Simulation of Four bar mechanism using SimMechanics.

28
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

14.) Simulation of Hydraulic Circuit with Single-Acting Cylinder using SimHydraulics.

29
Mechanical Engineering Department, B.S.Abdur Rahman University, Vandalur, Chennai-600048.

15) Simulation of Hydraulic system with Double-Acting Cylinder using SimHydraulics.

30

You might also like