Updated MATLab Manual Content
Updated MATLab Manual Content
CHAPTER 1
INTRODUCTION
• Numerical Calculations
• Integration
• Transforms
• Curve Fitting
1
Basic Manual on “Mechanical Engineers and MATLAB”
2
Basic Manual on “Mechanical Engineers and MATLAB”
OPERATOR PURPOSE
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
./ Array right-division operator.
Colon; generates regularly spaced elements and
: represents an entire row or column.
() Parentheses; encloses function arguments and array
indices; overrides precedence.
[] Brackets; enclosures array elements.
. Decimal point.
… Ellipsis; line-continuation operator
, Comma; separates statements and elements in a row
; Semicolon; separates columns and suppresses
display.
% Percent sign; designates a comment and specifies
formatting.
_ Quote sign and transpose operator.
._ Non-conjugated transpose operator
= Assignment operator
3
Basic Manual on “Mechanical Engineers and MATLAB”
Note: Semicolon (;) indicates end of statement. However, it suppresses and hides the
MATLAB output for an expression.
4
Basic Manual on “Mechanical Engineers and MATLAB”
CHAPTER 2
GETTING USED TO MATLAB BASICS
>> x=6;
>> y=x+9
And press ENTER, to display the result
Result returned is y =15
Tip 1: If you feel the gap between 2 individual lines are too large, the type
>> format compact
The default value of “pi” is 3.1416, you check by typing following commands
5
Basic Manual on “Mechanical Engineers and MATLAB”
Tips 2: The who command displays all the variable names you have used. MATLAB
will execute the above statement and return the following result.
>> who
Your variables are:
m r z
>> whos z
Name Size Bytes Class
z 1x1 8 double
ans =
64
ans =
1.0000
6
Basic Manual on “Mechanical Engineers and MATLAB”
ans =
-1
ans =
Inf
{warning: division by zero}
Tips 3: Use clc command to clear the commands in command window but not the
data in the variables
Tips 4: Use clear command to erase the memory or data in workspace section
>> y=sqrt(36)
y =
6
ans =
5
7
Basic Manual on “Mechanical Engineers and MATLAB”
atan2(y, x) Evaluates the arctangent or inverse tangent of y/x, The function returns an
angle in radians between −π & π depending upon the signs of x & y
Example:
>> r = [9 84 20 10 33]
r =
9 84 20 10 33
>> m = [5,8,5,2,1]
m =
5 8 5 2 1
8
Basic Manual on “Mechanical Engineers and MATLAB”
Column vectors are created by enclosing the set of elements in square brackets,
using semicolon (;) to delimit the elements.
Example:
>> c = [1; 2; 4; 5; 6]
c =
1
2
4
5
6
Colon (:) is one of the most useful operators in MATLAB. It is used to create vectors,
subscript arrays, and specify for iterations.
Example:
>> 10:16
ans =
10 11 12 13 14 15 16
ans =
10 12 14 16 18 20
We can also create equally spaced vectors given the starting & ending numbers. It
creates regularly spaced vector
>> x = linspace(-3,3,6) % from -3 to 3, generate 6 numbers with equal intervals
x =
-3.0000 -1.8000 -0.6000 0.6000 1.8000 3.0000
9
Basic Manual on “Mechanical Engineers and MATLAB”
Magic Square: magic (n) returns an n by n matrix constructed from the integers 1
through n^2 with equal sums of rows, columns and diagonal values. Note: The order
n must be a scalar greater than or equal to 3.
>> f=magic(3)
f =
8 1 6
3 5 7
4 9 2
ans =
15 15 15
ans =
15
15
15
10
Basic Manual on “Mechanical Engineers and MATLAB”
a =
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
11
Basic Manual on “Mechanical Engineers and MATLAB”
Example:
>> A=[1 2 3; 3 2 1; 1 2 3]; B=[4 5 6; 6 5 4; 4 5 6];
>> X=A+B
X =
5 7 9
9 7 5
5 7 9
>> Y=A-B
Y =
-3 -3 -3
-3 -3 -3
-3 -3 -3
>> Z=A*B
Z =
28 30 32
28 30 32
28 30 32
With a dot product we multiply the corresponding elements of two equally sized
matrices using the .* operator.
Example:
>> E = [2 1;4 3]
F = [3 4;5 1]
E =
2 1
12
Basic Manual on “Mechanical Engineers and MATLAB”
4 3
F =
3 4
5 1
>> T= [ 10 8 9; 7 4 5; 6 1 2];
>> V=det(T) % generates value for determinant of T matrix
V=
5.0000
>> c=inv(x)
c =
-0.0625 0.1875
0.3750 -0.1250
v =
-0.0625
0.3750
13
Basic Manual on “Mechanical Engineers and MATLAB”
v =
0.3750 -0.1250
14
Basic Manual on “Mechanical Engineers and MATLAB”
hand side of the linear equations and X is the column vector representing the solution
as shown in the below program
>> z=[5 9; 9 5];
>> r=[6;1];
>> m=inv(z)*r
m =
-0.3750
0.8750
15
Basic Manual on “Mechanical Engineers and MATLAB”
16
Basic Manual on “Mechanical Engineers and MATLAB”
Solution:
>>Year=[1988:1:1994];
>> Sales=[8 12 20 22 18 24 27];
>> plot(Year,Sales), xlabel('Year'),
ylabel('Sales(Millions)'),title('Sales v/s Years')
%you may use the aforementioned xlabel, ylabel or title in programming or you may individually
choose from options of figure windows individually
17
Basic Manual on “Mechanical Engineers and MATLAB”
Just double tap that red encircled icon and you shall be greeted with numerous tools
to operate either your graphs, charts, etc.
18
Basic Manual on “Mechanical Engineers and MATLAB”
19
Basic Manual on “Mechanical Engineers and MATLAB”
Actual MATLAB
name name Style Symbol
White w Plus +
Black k Solid -
Green g Dash-dot -.
Cyan c Dotted :
Blue b Dashed --
Red r Circle o
Magenta m Point .
Yellow y Star *
20
Basic Manual on “Mechanical Engineers and MATLAB”
5. Plot a function y=3x3-26x+10, its first derivative & second derivative in same
plot
y′=9x2-26
y″=18x for -2≤ x ≤4
Solution:
>> x=[-2:0.01:4];
>> y=3*x.^3-26*x+10;
>> yd=9*x.^2-26;
>> ydd=18*x;
>> plot(x,y,'k');
>> hold on;
>> plot(x,yd,'-b');
>> hold on;
>> plot(x,ydd,'g');
>> hold off;
21
Basic Manual on “Mechanical Engineers and MATLAB”
22
Basic Manual on “Mechanical Engineers and MATLAB”
23
Basic Manual on “Mechanical Engineers and MATLAB”
24
Basic Manual on “Mechanical Engineers and MATLAB”
Note:
.* implies array multiplication operator
.^ implies array exponential operator
25
Basic Manual on “Mechanical Engineers and MATLAB”
Solution:
>> [x,y]=meshgrid(-7.5:0.2:7.5,-7.5:0.2:7.5);
>> k=sqrt(x.^2+y.^2)+eps;
>> z=sin(k)./k;
26
Basic Manual on “Mechanical Engineers and MATLAB”
27
Basic Manual on “Mechanical Engineers and MATLAB”
Solutions:
>> [x,y]=peaks(30);
>> z=[(x.^2)-(y.^2)];
28
Basic Manual on “Mechanical Engineers and MATLAB”
12. Find the temperature at various times of the day in-between above timings.
Time of the 1 2 3 4 5 6 7 8 9
Day
Temperature 25.4 25.7 26 26.8 27 27.7 28 28.5 5.7
in Celsius
Solution:
>> d=1:9;
>> t=[25.4,25.7,26,26.8,27,27.7,28,28.5,5.7];
>> di=[1.5,2.5,4.75,5,6.75];
ans =
29
Basic Manual on “Mechanical Engineers and MATLAB”
13. Interpolate a Karizma motorcycle speed when reaching & exiting out from the
REVA main gate.
Time (sec) 0 10 20 30 40 50 60 70 80 90
Speed 33 25 10 0 0 18 25 38 45 60
(km/hr)
Solution:
>> t=0:10:90;
>> ti=[5:10:85];
>> si=spline(t,s,ti);
ans =
30
Basic Manual on “Mechanical Engineers and MATLAB”
49.8
14. Using a loop function, script a MATLAB code to print the sum of natural
numbers from 3 to 13.
Solution:
>> n=0;
>> i=3;
n=n+i;
i=i+1;
end
>> n
n =
88
31
Basic Manual on “Mechanical Engineers and MATLAB”
CHAPTER 3
1. Plot the variation of natural frequency and the time period with static deflection
of an undamped system using MATLAB for deflection range of 0 to 0.5 m.
Solution:
𝟏 𝐠 𝟏 𝛅𝐬𝐭
𝒇𝒏 = 𝟐 √𝛅𝐬𝐭 𝑻 = 𝒇𝒏 𝑻 = 𝟐𝛑√ 𝐠
>> g=9.81;
>> for i=1:101
d(i)=(0.5)*(i-1)/100;
f(i)=(g/d(i))^0.5;
T(i)=2*pi/f(i);
end
>> plot(d,f);
>> hold on; plot(d,T); hold off
32
Basic Manual on “Mechanical Engineers and MATLAB”
2. Solve for seven cycles, the response of an unforced system given by the equation
m x” + c x’+ kx = 0
ξ = 0.2 m = 2 kg k = 150 N/m x (0) = 0.02 m x’ (0) = 0
Solution:
MATLAB uses Runge-Kutta method to solve the differential equations, which are only
in the form of first order equations only.
But the above equations are second order differential equations.
Let m x” + c x’+ kx = 0 …………………….……………………….(1)
−c k
Eqn. 1 shall be formulated as v=( m ) 𝑣 − (m)𝑥
x’’+2ωnξ x+ ωn2 =0
c
m
= 2 ξ ωn and ωn2 = mk
By using the values of ‘k’ and ‘m’, calculate the different values of ‘c’ corresponding
to each value of ξ. Once the values of ‘c’ are known, equations (4) and (5) can be
solved using MATLAB.
Now, we require to solve for 5 cycles. So first we need to find the damped period of
system and later the time interval.
Natural frequency ωn = (k / m)1/2 = 8.6 rad/sec
For ξ = 0.2
Damped natural frequency ωd = ωn (1- ξ2)1/2 = 8.42rad/sec
Damped time period Td = 2π/ ωn = 0.73 sec
We need for 7 times the time cycles i.e, 0.63*5 = 5.14 sec
>>edit unfocrced1
% the moment you type the aforementioned line, a new editor window would pop up.
% It is to be note that we have to create a new function as to represent the MATLAB for the
retrieved equations separately by defining them in “.m” files
33
Basic Manual on “Mechanical Engineers and MATLAB”
function yp = unforced1(t,y)
yp = [y(2) ;(-(3.44*y(2))-(75*y(1)))];
end
%we are assigning yp to have 2 different values
%Initially open a M-file from “New Script” and input the 2 aforementioned lines.
%Main point to ponder is, begin the script with “function” and save that file in “unforced1.m”
only. As the MATLAB can accesses the code when you enter this argument.
%Now enter the below lines in command window section
>> tspan=[0 4]; % tspan represents time interval
>> [t,y]=ode45('unforced1',tspan,y0);
34
Basic Manual on “Mechanical Engineers and MATLAB”
3. Using MATLAB, generate a table showing velocity against height from which a
body is dropped vertically. Consider the height from 0 m to 50 m with an
increment step of 5 m.
Solution:
format short g
g=9.81;
iteration=1;
for height=0:5:50;
velocity=sqrt(2*g*height);
table(iteration,1)=height;
table(iteration,2)=velocity;
iteration=iteration+1;
end
table
table =
0 0
5 9.9045
10 14.007
15 17.155
20 19.809
25 22.147
30 24.261
35 26.205
40 28.014
45 29.714
50 31.321
35
Basic Manual on “Mechanical Engineers and MATLAB”
4. Write a MATLAB program to generate set of elongation, strain and stress developed in
a circular bar of length 2 m and diameter 30 mm subjected a load of 4000 N to 40 kN in
a step of 4000 N. Assume Modulus of Elasticity as 210 GPa. Display the results under
the following title: Force in N, Elongation in mm, Strain, Stress in N/mm2.
Solution:
>> format short g
>> l=2000; % Length of the bar in mm
>> d=30; % Diameter of the bar in mm
>> E=2.1e5; % Modulus of Elasticity in N/mm^2
>> A=(pi/4)*d^2; % Area of Cross-section of the bar in mm^2
>> i=1;
>> for P=4000:4000:40e3; % Load applied from 3000 to 30,000 N increment of
3000N
delta=((P*l)/(A*E));
strain=delta/l;
stress=P/A;
Res(i,1)=P;
Res(i,2)=delta;
Res(i,3)=strain;
Res(i,4)=stress;
i=i+1;
end
>> disp('Load(N) Elongation(mm) Strain Stress(N/mm^2)
');Res
36
Basic Manual on “Mechanical Engineers and MATLAB”
Note: Res stands for result in short form and it is a user defined term. Res(ii,1) = P;
% It create a Matrix in the name of Res and the value of P is stored in the iith row
and 1st column. Since, ii is 1 in the 1st iteration; the value of P will be stored in 1st
row – 1st column. In the 2nd iteration ii will be incremented to 2 and so on.
37
Basic Manual on “Mechanical Engineers and MATLAB”
format short g
l=0.6e3;
d=60;
E=210e3;
A=(pi*d^4)/64;
I = (pi*d^4)/64;
z = (pi*d^3)/32;
% Programming part starts from this section onwards
ii=1;
for P=200:200:4e3;
M=(P*l);
max_def=(P*(l^3))/(3*E*I);
stress=M/z;
% Storing the calculated values in array forms begins from this section
Res(ii,1)=P;
Res(ii,2)=M;
38
Basic Manual on “Mechanical Engineers and MATLAB”
Res(ii,3)=max_def;
Res(ii,4)=stress;
ii=ii+1;
end
disp('Load (N) Bending Moment (M) Max Deflection (mm)
Stress(N/mm^2)'); Res
39
Basic Manual on “Mechanical Engineers and MATLAB”
>> i=1;
>> for v=0:15:150; %increment for every 15 m/s
KE=0.5*m*(v^2); % K.E. of a body formula
table(i,1)=KE;
table(i,2)=v;
i=i+1;
end
>> table
table =
0 0
1376.1 15
5504.6 30
12385 45
22018 60
34404 75
49541 90
67431 105
88073 120
1.1147e+05 135
1.3761e+05 150
40
Basic Manual on “Mechanical Engineers and MATLAB”
7. The below figure shows the location of C.G. of 35,000 N weight pickup truck
under unloaded condition. At the cargo section additional weights are added with
load of WL at a distance of x cm behind the rear axle. Generate the MATLAB
program and plot WL as a function of x, where x value ranges from 0 to 50cm.
The front wheel midpoint lies 120cm from C.G. point and the rear wheels
midpoint lies 200cm from C.G point.
Solution:
Equilibrium equations:
∑Fy= 0 ➔ N + N – 35000 – WL = 0
2N = 35000 - WL
35,000+WL
𝑁= 2
41
Basic Manual on “Mechanical Engineers and MATLAB”
>> wl=1400000./(160+x);
>> plot(x,wl);xlabel('x in cm'); ylabel('additional cargo
weight in N'); title('addition of cargo weight (N) v/s
dispalcement in cargo position (cm)'); grid on
Note: This graph represents, how the semitruck would react for the after-cargo input
without toppling of vehicle under maneuvering conditions.
42
Basic Manual on “Mechanical Engineers and MATLAB”
Solution:
43
Basic Manual on “Mechanical Engineers and MATLAB”
p_1 =
110
>> p_2 = (p_x+p_y)/2 - circle_center
p_2 =
-60
% The “text” function, induced the MATLAB to insert the text message inside the enclosed
brackets with single quotes.
44
Basic Manual on “Mechanical Engineers and MATLAB”
45
Basic Manual on “Mechanical Engineers and MATLAB”
216.6190
46
Basic Manual on “Mechanical Engineers and MATLAB”
47
Basic Manual on “Mechanical Engineers and MATLAB”
𝑅𝐴 + 𝑅𝐵 = 30 × 3 + 20
ΣMA = 0 = 30 × 3 × (3/2) − 6 × 𝑅𝐵 + 20 × 8 = 0
Portion AD:
𝑥^2
F = 60.8 – 30x M = 608 x – 30 2
Portion AD:
F = RA – 30*3 M = RA*x – 30*(x-1.5)
Portion BC:
F = RA – 30*3 + RB M = RA* x – 30*3*(x-1.5) + RB*(x-6)
48
Basic Manual on “Mechanical Engineers and MATLAB”
>> subplot(2,1,1)
>> plot(x,F)
>> title('SFD')
>> subplot(2,1,2)
>> plot(x,M)
>> title('BMD')
49
Basic Manual on “Mechanical Engineers and MATLAB”
50
Basic Manual on “Mechanical Engineers and MATLAB”
Solution:
Simple D.E. of motion for simple pendulum without damping system is given by
−𝑔
θ’’= θ ( )sinθ
𝐿
sinθ = θ
MATLAB is versatile with Runge-Kutta method, thereby we reduce the above 2nd
O.D.E to 2 first order differential equation.
Now, y(1)=y(2)
51
Basic Manual on “Mechanical Engineers and MATLAB”
−𝑔
Now 2 first order D.E.:: y(1)=y(2) and y(2)=( )sin(y(1))
𝐿
−𝑔 −9.81
= = 6.54 %150cm to 1.5m
𝐿 1.5
>>edit linear_1
>> y0=[1.39;0];
>> [t,y]=ode45('linear_1',tspan,y0);
>> plot(t,y(:,1))
>> grid on
>> xlabel('Time')
>> ylabel('Theta')
>> hold on; %this “hold on”, causes the MATLAB to plot the next
graphs details to the previous one. And must be released back to hold off
>>edit nonlinear
function yp=nonlinear(t,y)
yp=[y(2);((-6.54) *sin(y(1)))];
end
>> [t,y]=ode45('nonlinear',tspan,y0);
>> plot(t,y(:,1))
52
Basic Manual on “Mechanical Engineers and MATLAB”
12. A can of Coke is taken out from the refrigerator at 5°C, and is placed on a table
in a room at 25°C. Sketch T(t).
Related questions:
53
Basic Manual on “Mechanical Engineers and MATLAB”
14. If the temperature of our body tissue is 37°C, T_room is 25°C, and the thickness
of the tissue is 0.002m, find T_skin. Relevant data:
L = 0.002m,
h = 20 W/𝑚2 -K,
k = 0.61 W/m-K
54
Basic Manual on “Mechanical Engineers and MATLAB”
Answer:
k_skin*(37 – T_skin)/L = h (T_skin – T_room)
MATLAB code:
>>clc; clear
k=0.61; h=20; L=0.002;
Bi = h*L/k % = 0.0656
Troom = 25;
Tskin = (37 + Bi*Troom)/(1+Bi)
15. Consider a 2-D concrete column. Initially, the air and the column are at 20C.
Suddenly, T_west is raised to 1000C. Relevant data include:
k = 0.8; ρ = 2100; 𝑐𝑣 =1100;
Lx=Ly=0.3m; Δt = 100s; one node;
Find T1 at t = 100s. Use the implicit method.
55
Basic Manual on “Mechanical Engineers and MATLAB”
MATLAB code:
>>clc; clear
rho=2100; k=0.8; cv=1100; aLf=k/(rho*cv);
Lx=0.3; dx=Lx/2; dt=100; dxs=dx*dx;
r=aLf*dt/dxs; %dimensionless aspect ratio parameter
Tp=20;
T=(Tp+ r*(1000+3*20))/(1+4*r)
Transient Lumped-Capacitance Heat Conduction
Consider a small copper plate placed in the outer space.
m cv (T – Tp) = εσ𝑇𝑝 4 (1) ( explicit )
m cv (T – Tp) = εσ𝑇 4 (2) ( implicit )
56
Basic Manual on “Mechanical Engineers and MATLAB”
16. Determine the internal energy for superheated water vapor at 90°C and 0.20 bar
MATLAB code:
u1=interp1([80 120],[2487.3 2544.7],90);
u2=interp1([80 120],[2483.7 2542.4],90);
u=interp1([.06 .35],[u1 u2],.2)
Tips 6:You can make the best use of ‘help’ tool to the fullest, by understudying the
basic till solved examples of all functions and arguments.
>>help ode45
% Get down to the bottom of the message, you find the references
% This is linked to the default documentations of MathWorks, where there are solved
examples from beginning to pro levels.
57
Basic Manual on “Mechanical Engineers and MATLAB”
FUN TASK
s = surface(L);
s.EdgeColor = 'none';
view(3) %Turning on 3D view insertion
%Setting up the camera view for logo view in iconic style, just like zooming in and out
ax.CameraPosition = [-145.5 -229.7 283.6];
ax.CameraTarget = [77.4 60.2 63.9];
ax.CameraUpVector = [0 0 1];
ax.CameraViewAngle = 36.7;
58
Basic Manual on “Mechanical Engineers and MATLAB”
l2 = light;
l2.Position = [.5 -1 .4];
l2.Color = [0.8 0.8 0];
%Touch up session
s.FaceLighting = 'gouraud';
s.AmbientStrength = 0.3;
s.DiffuseStrength = 0.6;
s.BackFaceLighting = 'lit';
s.SpecularStrength = 1;
s.SpecularColorReflectance = 1;
s.SpecularExponent = 7;
%Turing off the axis display, to show only the logo, with transparency background .
axis off
f.Color = 'black';
59
Basic Manual on “Mechanical Engineers and MATLAB”
ACKNOWDEGEMENT:
60