Computational Lab (ME-106) TO Matlab
Computational Lab (ME-106) TO Matlab
Computational Lab (ME-106) TO Matlab
(ME-106)
INTRODUCTION
TO
MATLAB
By
K.Kiran Kumar
Assistant professor
Mechanical Engineering Department
B.S.Abdur Rahman University
Email:- kiranmek3@gmail.com
Ph.no:- 9047475841
WHAT IS MATLAB?
Input Output
System
MATLAB is too broad tool used in industry and Research for real
time interfacing of sensors and machine vision etc. and
programming the real time systems for active control of the
system behavior.
For our course purpose in this Laboratory we will have brief
overview of basics and learn what can be done with MATLAB at
beginner level.
WHY DO WE
NEED TO
PERFORM
ANALYSIS IN
MATLAB ???
AREAS WHERE MECHANICAL
ENGINEERS USE MATLAB
Solving kinematics, kinetics and complete
dynamic systems control of Automotive
suspension , Thermal systems etc.,
AERO PLANE SUSPENSION OF LANDING GEAR
M
M
Mg Fs Fc
Yo
d 2 yo
Mg Fc Fs
Ys
M 2
dt
Yin
BASICS
Displacement
distance D
35 Metres
Time
Speed Velocity
65 m/s V
Time
0-60 m/s
in 8.6 Acceleration
second A
Time
UNDAMPED FREE VIBRATION
Displacement
Displacement
d = D sinnt
D
Time
T Frequency
1
T
m
Period, Tn in [sec]
k 1
Frequency, fn= T in [Hz = 1/sec]
n
k
n= 2 fn =
m
CONTINUED…
Natural frequency of a simple single degree of
freedom undamped system is given by the
equation
ωN = square root of (stiffness / mass)
xs
ms
ks cs
xu
mu
kt
u
15
M
Mg
y
ys
Suspension system
yin
SUSPENSION SYSTEM IN FOUR
WHEELER
CONTINUED…
CONTINUED…
CONTINUED…
CONVENTIONAL PASSIVE SUSPENSION
zs
sprung mass
(body) Ms
zu
unsprung mass
(wheel, axle) Mu
zr
tyre stiffness Kt
MATLAB MODEL FOR
BUS SUSPENSION
Designing an automatic suspension system for a bus
MATLAB SCREEN
VARIABLES
No need for types. i.e.,
int a;
double b;
float c;
Accuracy and comfort is very high with matlab codes.
>>x=5;
>>x1=2;
ARRAY, MATRIX
LONG ARRAY, MATRIX
GENERATING VECTORS FROM FUNCTIONS
MATRIX INDEX
OPERATORS (ARITHMETIC)
MATRICES OPERATIONS
THE “DOT OPERATOR”
By default and whenever possible MATLAB
will perform true matrix operations (+ - *). The
operands in every arithmetic expression are
considered to be matrices.
If, on the other hand, the user wants the scalar
version of an operation a “dot” must be put in
front of the operator, e.g., .*. Matrices can still
be the operands but the mathematical
calculations will be performed element-by-
element.
A comparison of matrix multiplication and
scalar multiplication is shown on the next slide.
OPERATORS (ELEMENT BY ELEMENT)
DOT OPERATOR EXAMPLE
>> A = [1 5 6; 11 9 8; 2 34 78]
A =
1 5 6
11 9 8
2 34 78
>> B = [16 4 23; 8 123 86; 67 259 5]
B =
16 4 23
8 123 86
67 259 5
DOT OPERATOR EXAMPLE (CONT.)
>> C = A * B % “normal” matrix multiply
C =
458 2173 483
784 3223 1067
5530 24392 3360
ans =
1 0 0
0 1 0
»
RANDOM NUMBERS GENERATION
COMPLEX NUMBERS HANDLING
FUNCTIONS
MATRIXES AND VECTORS
x = [1,2,3] , vector-row,
y=[1;2;3], vector-column,
A = [1,3,5;5,6,7;8,9,10], matrix,
x‟, transpose.
42
MATRIXES AND VECTORS
size(A), matrix size,
det(A), determinant,
43
INTRODUCTION TO M-FILES PROGRAMMING
Type-1 Type-2
programming Programming
Program:- Program:-
clc;
clc; clear all;
clear all; p=input('enter the value of p:');
t=input('enter the value of t:');
p=10,000;
r=input('enter the value of r:');
t=2; I=(p*t*r)/100
r=11; Solution:-
Input:
I=(p*t*r)/100; enter the value of p:10000
Solution:- enter the value of t:2
enter the value of r:11
I = 2200
Output:
I = 2200
GRAPHICS AND DATA DISPLAY
Cartesian coordinates
>> bar(x,y) % vertical bar graph
>> barh(x,y) % horizontal bar graph
>> stem(x,y) % stem plot
>> area(x,y) % color fill from horizontal axis to line
>> hist(y,N) % histogram with N bins (default N = 10)
Polar coordinates
>> pie(y)
>> rose(theta,N) % angle histogram, N bins (default 10)
GRAPHICS AND DATA DISPLAY
Pie
>> pie3(vector)
One dimensional data, but 3-D pie perspective
GRAPHICS AND DATA DISPLAY
>> waterfall(x,y,Z)
• like mesh but without column connection lines
• used for column-oriented data
BASIC TASK: PLOT THE FUNCTION
SIN(X) BETWEEN 0≤X≤4Π
PLOT THE FUNCTION BETWEEN 0≤X≤4Π
PLOT THE FUNCTION e-X/3SIN(X)
BETWEEN 0≤X≤4Π
DISPLAY FACILITIES
CONTD..
LINE SPECIFIERS IN THE plot() COMMAND
plot(x,y,‘line specifiers’)
» plot(x,y)
2000
1500
1000
500
0
0 5 10 15 20 25 30 35 40 45 50
Plots
» plot(x,y,'*-')
» xlabel('Values of x')
» ylabel('y') 2500
2000
1500
y
1000
500
0
0 5 10 15 20 25 30 35 40 45 50
Values of x
MULTIPLE GRAPHS
t=0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2); 1
plot(t,y1,t,y2);
0.8
0.6
grid on 0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
MULTIPLE PLOTS
t=0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
grid on
subplot(2,2,2)
plot(t,y2);
grid on
subplot(i,j,k)
• i is the number of rows of subplots in the plot
• j is the number of columns of subplots in the plot
• k is the position of the plot
INTERESTING FEATURE OF GENERATING
SINE CURVE
x = 0:0.05:6; 1
y = sin(pi*x);
0.8
0.6
plot(x,y,':',x,Y,'-') 0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6
CONTINUED..
x = 0:0.05:6; 1
0.8
y = sin(pi*x); 0.6
plot(x,y,„.',x,Y,'-')
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6
OPERATORS (RELATIONAL, LOGICAL)
POLYNOMIALS
MATLAB FUNCTIONS FOR POLYNOMIALS
Contd..
Representing Polynomials:
x4 - 12x3 + 25x + 116
» P = [1 -12 0 25 116];
» roots(P)
ans =
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i
» r = ans;
» PP = poly(r)
PP =
1.0000 -12.0000 -0.0000 25.0000 116.0000
Polynomial Multiplication
a = x3 + 2x2 + 3x + 4
b = 4x2 + 9x + 16
» a = [1 2 3 4];
» b = [4 9 16];
» c = conv(a,b)
c=
4 17 46 75 84 64
Evaluation of a
Polynomial
a = x3 + 2x2 + 3x + 4
» polyval(a, 2)
ans =
26
Symbolic Math
» syms x
» int('x^3')
ans =
1/4*x^4
» eval(int('x^3',0,2))
ans =
»
Solving Nonlinear Equations
nle.m
Function of the program:-
function f = nle(x)
f(1) = x(1) - 4*x(1)*x(1) - x(1)*x(2);
f(2) = 2*x(2) - x(2)*x(2) + 3*x(1)*x(2);
Program:-
x0 = [1 1]';
x = fsolve('nle', x0)
Solution:-
x=
0.2500
0.0000
TO FIND EIGEN VALUES AND EIGEN
VECTORS OF MATRICES
CONTINUED…
SOLVING LINEAR EQUATIONS
(TRY MANUALLY)
quad:
It integrates the specified function over specified limits, based on
adaptive Simpson's rule.
The general call syntax for both quad and quadl is as follows:
Syntax:-
integral = quad(„function‟, a, b)
term = sqrt(x);
Jump to here if FALSE
y = sum(term)
EXAMPLE PROGRAM TO EXPLAIN IF LOOP
% Program to find whether roots are imaginary or not%
clc;
clear all;
a=input('enter value of a:');
b=input('enter value of b:');
c=input('enter value of c:');
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
end
Solution:-
Input:
enter value of a:1
enter value of b:2
enter value of c:3
Output:
Warning: discriminant is negative, roots are imaginary
EXAMPLE OF IF ELSE STATEMENT
Program:-
A = 2;
B = 3;
if A > B
'A is bigger'
elseif A < B
'B is bigger'
elseif A == B
'A equals B'
else
error('Something odd is happening')
end
Solution:-
ans =
B is bigger
IF STATEMENT EXAMPLE
Here are some examples based on the familiar quadratic formula.
1. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
end
2. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
else
disp('Roots are real, but may be repeated')
end
3. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
elseif discr == 0
disp('Discriminant is zero, roots are repeated')
else
disp('Roots are real')
end
EXAMPLE OF FOR LOOP
Problem: Draw graphs of sin(nπ x) on the interval −1 ≤ x ≤ 1 for
n = 1,2,....,8.We could do this by giving 8 separate plot
commands but it is much easier to use a loop.
Program:-
x=-1:0.05:1;
for n=1:8
subplot(4,2,n);
plot(x, sin(n*pi*x));
end
EXAMPLE OF FOR & WHILE LOOP
1.) % example of for loop%
Program:-
for ii=1:5
x=ii*ii
End
Solution:
1 4 9 16 25
Program:-
n=input(„enter the value of n: ‟);
switch(rem(n,3))
case 0
m = 'no remainder'
case 1
m = „the remainder is one'
case 2
m = „the remainder is two'
otherwise
error('not possible')
end
Solution:-
enter the value of n: 8
m =the remainder is two
SPRING/MASS/DAMPER SYSTEM EXAMPLE
88
PROBLEM SOLVED BY USING
MATLAB AND SIMULINK
INTRODUCTION
PROBLEM
PROBLEM
FUNCTION OF THE PROGRAM
function f=programone (t,z)
m=3;
c=8;
k=100;
dzdt=[z(2); -(c/m)*z(2)-(k/m)*z(1)];
MAIN BODY OF THE PROGRAM
% For a single degree of freedom system in free vibration
clc;
clear all;
%Enter initial conditions
z0=[5;15];
%Enter time span for solution
tspan=[0 10];
%Call solver
[t,z]=ode45(„programone',tspan,x0);
%Set up plot
plot(t,z(:,1));
RESULT
single degree of freedom spring mass damper system behaviour
6
displacement
5
3
displacement in mm
-1
-2
-3
0 5 10 15
time in seconds
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLE
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLE
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLE
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLEPROBLEM
(OSCILLATOR)
MODELLING PENDULUM WITH DAMPING
USING STATE SPACE TRANSFORM
CONTINUED…
TEST
TO IDENTIFY THE LEVEL OF UNDERSTANDING
PROBLEM:- BY CONSIDERING NONLINEAR DAMPING
FORMATTING PLOTS
(Used for comparing and Interpreting
Theoretical and Experimental Results)
1. Formatting commands.
In this method commands, that make changes or additions to the
plot, are entered after the plot() command. This can be done in
the Command Window, or as part of a program in a script file.
title(‘string’)
Adds the string as a title at the top of the plot.
xlabel(‘string’)
Adds the string as a label to the x-axis.
ylabel(‘string’)
Adds the string as a label to the y-axis.
legend(‘string1’,’string2’,’string3’)
Creates a legend using the strings to label various curves (when
several curves are in one plot). The location of the legend is
specified by the mouse.
text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to the
plot axes.
gtext(‘string’)
Places the string (text) on the plot. When the command executes
the figure window pops and the text location is clicked with the
mouse.
EXAMPLE PROGRAM
clc; 1200
Light Intensity as a Function of Distance
x=[10:0.1:22]; 1000
y=95000./x.^2; 800
INTENSITY (lux)
xd=[10:2:22]; Comparison between theory and experiment.
plot(x,y,'-','LineWidth',1.0) 400
hold on
plot(xd,yd,'ro--','linewidth',1.0,'markersize',10)200
hold off
0
8 10 12 14 16 18 20 22 24
xlabel('DISTANCE (cm)') DISTANCE (cm)
ylabel('INTENSITY (lux)')
title('\fontname{Arial}Light Intensity as a Function of Distance','FontSize',14)
axis([8 24 0 1200])
text(14,700,'Comparison between theory and
experiment.','EdgeColor','r','LineWidth',2)
legend('Theory','Experiment',0)
OPEN WEB RESOURCES
Mathworks Information
Mathworks: http://www.mathworks.com
Mathworks Central: http://www.mathworks.com/matlabcentral
http://www.mathworks.com/applications/controldesign/
http://www.mathworks.com/academia/student_center/tutorials/launchpad.
html
Matlab Demonstrations
http://www.mathworks.com/cmspro/online/4843/req.html?13616
http://www.mathworks.com/cmspro/online/7589/req.html?16880
Select Help-Demos in Matlab
Matlab/Simulink student Select “Help” in Matlab on extensive help about
Matlab, Simulink and toolboxes
http://www.mathworks.com/academia/student_center/homework/
http://www.mathworks.com/academia/student_center
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.html
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/
http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf
http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/using_ml.pdf
http://www.mathworks.com/access/helpdesk/help/toolbox/control/control.shtml
http://www.mathworks.com/academia/student_version/
http://www.mathworks.com/academia/student_version/companion.html
http://travis.eng.man.ac.uk/barry/control2/lab/SIMULINK.htm
http://matlab.kimhua.co.kr/digest/dec98/nonlinear.html
OPEN WEB RESOURCES
http://www.scsolutions.com/feedback.html
http://www.math.mtu.edu/~msgocken/intro/intro.html
http://www-personal.engin.umich.edu/~tilbury/tutorials/matlab_tutorial.html
http://www.eng.fsu.edu/~cockburn/matlab/matlab_help.html
http://www.engin.umich.edu/group/ctm/working/mac/simulink_basics/
http://www.messiah.edu/acdept/depthome/engineer/Resources/tutorial/matlab/simu.html
http://rclsgi.eng.ohio-state.edu/courses/me780/handouts/simulink.pdf
http://www.mae.ncsu.edu/homepages/buckner/simulink2.pdf
http://www.tutorgig.com/showurls.jsp?group=896&index=0
http://www.rpi.edu/dept/chem-eng/WWW/faculty/bequette/lou/simtut/simtut_html.html
http://www.math.siu.edu/matlab/tutorials.html
OPEN WEB RESOURCES
http://wolfman.eos.uoguelph.ca/~jzelek/matlab/ctms/
http://www.engin.umich.edu/group/ctm/
http://www.me.cmu.edu/matlab/html/
http://www.math.utah.edu/lab/ms/matlab/matlab.html
http://www.indiana.edu/~statmath/math/matlab/
http://spicerack.sr.unh.edu/~mathadm/tutorial/software/matlab/
http://www.math.ufl.edu/help/matlab-tutorial/matlab-tutorial.html
http://www.cs.ubc.ca/spider/cavers/MatlabGuide/guide.html
http://www.class.umd.edu/enme/403/0101/t1.html
http://www.math.mtu.edu/~msgocken/intro/intro.html
http://www.engin.umich.edu/group/ctm
THANK YOU…
Any Questions ?