Documente Academic
Documente Profesional
Documente Cultură
Laboratory Manual
(Matlab Programming Practices)
Signature
(Faculty In-Charge)
INDEX
1. Consider
matrix A=[ 1 3 5 7
5 9 23 12
8 34 22 87
56 43 4 63]
Prepare a script file for the following
(a) Store 2nd row in b
(b) Store 3rd column in c
(c) Store all the elements of 2nd row from 2nd column till the end.
(d) Store all the elements of 3rd column from 3rd row till the end.
(e) Find transpose of the matrix A
(f) Find inverse of the matrix A
(g) Determine number and rows in the matrix A and display it on the screen.
Program
Function BME49_1_1()
clc;
clear all;
close all;
A=[1 3 5 7;5 9 23 12;8 34 22 87;56 43 4 63]
%(a) Store 2nd row in "b"
b=A(2,:)
%(b) Store 3rd column in "c"
c=A(:,3)
%(c) Store all the elements of 2nd row from 2nd cloumn till the end
d=A(2,(2:end))
%(d) Store all the elements of 3rd column from 3rd column from 3rd row till
the end
e=A((3:end),3)
%(e) Find transpose of the matrix A
f=A'
%(f) Find inverse of the matrix A
g=inv(A)
%(g) Determine number and rows in the matrix A and display it on the screen
length(A(1,:))
length(A(:,1))
size(A)
end
Output
A=
1 3 5 7
5 9 23 12
8 34 22 87
56 43 4 63
b=
5 9 23 12
c=
5
23
22
4
d=
9 23 12
e=
22
4
f=
1 5 8 56
3 9 34 43
5 23 22 4
7 12 87 63
g=
1.2325 -0.1842 -0.0922 0.0255
-3.0900 0.4902 0.1927 -0.0161
0.4266 -0.0198 -0.0305 -0.0015
0.9864 -0.1696 -0.0476 0.0043
ans =
ans =
ans =
4 4
Program
Function BME60_1_2()
H=[1 0 1;2 3 4;-1 6 7]
I=[7 4 2;3 5 6;-1 2 1]
% (a) H + I
disp('H+I is')
J=H+I
% (b) H*I
disp('HI is')
K=H*I
disp('H^2 is')
L=H^2
disp('Transpose of H is')
M=H'
disp('I inverse is')
N=inv(I)
disp('HT*IT is')
O=H'*I'
disp('(H^2*I^2)-(H*I) is')
P=(H^2*I^2)-(H*I)
disp('Det. of H is')
Q=det(H)
disp('Det. of I is')
R=det(I)
disp('Det. of H*I is')
S=det(H*I)
end
Output
H=
1 0 1
2 3 4
-1 6 7
I=
7 4 2
3 5 6
-1 2 1
H+I is
J=
8 4 3
5 8 10
-2 8 8
HI is
K=
6 6 3
19 31 26
4 40 41
H^2 is
L=
0 6 8
4 33 42
4 60 72
Transpose of H is
M=
1 2 -1
0 3 6
1 4 7
I inverse is
N=
HT*IT is
O=
13 7 2
24 51 12
37 65 14
(H^2*I^2)-(H*I) is
P=
Det. of H is
Q=
12
Det. of I is
R=
-63.0000
Det. of H*I is
S=
-756.0000
3. Determine the Eigen values and Eigen vectors of the matrix A and B.
Program
eig_H=eig(H)
eig_I=eig(I)
Output
eig_H =
0.3308 + 1.0253i
0.3308 - 1.0253i
10.3385 + 0.0000i
eig_I =
-1.3967
9.7884
4.6082
4. Determine the values of x1, x2 and x3 for the following set of linear algebraic equations:
x2 3x3 = 5
2x1 + 3x2 x3 = 7
4x1 + 5x2 2x3 = 10
Program
Function BME60_1_3()
clc;
clear all;
close all;
syms x1 x2 x3
eqn1 = x2 - 3*x3 == -5;
eqn2 = 2*x1 + 3*x2 - x3 == 7;
eqn3 = 4*x1 + 5*x2 - 2*x3 == 10;
sol = solve([eqn1, eqn2, eqn3],x1, x2, x3);
xSol = sol.x1
ySol = sol.x2
zSol = sol.x3
end
Output
xSol = -1
ySol = 4
zSol = 3
5. Prepare a script file to take unknown as variables and gives output of the
following expression.
Program
Function BME60_1_5()
clc;
clear all;
close all;
f=input('Enter value of f= ');
p=input('Enter value of Density= ');
h=input('Enter value of h= ');
l=input('Enter value of l= ');
d=input('Enter d=')
D=input('Enter D=')
g=9.81;
A=0.25*pi*p*(d^2)*(((2*g*(D^5)*h)/((D^5)+(4*f*l*d^4)))^1.5);
disp(A)
end
Output
Enter D=2
D=
A is 2.9472e+06
EXERCISE-2
1. Prepare a Matlab program which takes input as marks and displays grade.
Program
Function BME60_2_1()
clc;
clear all;
close all;
M=input('Enter your marks==');
if M>100
disp('Marks you entered are not valid');
elseif M>89
disp('Congrats your grade is AA');
elseif M>80
disp('Your grade is AB');
elseif M>71
disp('Your grade is BB');
elseif M>63
disp('Your grade is BC');
elseif M>55
disp('Your grade is CC');
elseif M>47
disp('Your grade is CD');
elseif M>40
disp('Your grade is DD');
else
disp('Sorry you failed in this subject');
end
Program
Function BME60_2_2()
clc;
clear all;
close all;
N=input('Enter input:');
if N==1
disp('You have entered 1');
elseif N==2
disp('You have entered 2');
elseif N==3
disp('You have entered 3');
elseif N==4
disp('You have entered 4');
elseif N==5
disp('You have entered 5');
else
disp('Invalid input');
end
Output-Enter input:2
You have entered 2
Program
Function BME60_1_3()
clc;
clear all;
close all;
O=input('Enter value:','s');
method='O';
switch O
case'a'
disp('Acceleration');
case'b'
disp('Bouyancy');
case'c'
disp('Crank');
case'd'
disp('Drill');
case'e'
disp('Efficiency');
otherwise
disp('Input is invalid please enter alphabets a to e');
end
Output- Enter
value:c
Crank
4. Prepare a Matlab program which takes shaft diameter and dynamic load
carrying capacity
as input.
-based on shaft diameter and dynamic load carrying capacity it should display the
appropriate bearing.
-it should display invalid input- enter shaft diameter between 25 and 35 mm if input
diameter is out of range.
- It should display No bearing possible if dynamic capacity is out of range.
Program
Function BME60_2_4()
clc;
clear all;
close all;
D=input('Enter Diameter of Shaft=');
C=input('Enter Dynamic Loadng Capacity=');
if D==25&&C<4.36
disp('Bearing Designation is 61805');
elseif D==25&&C<7.02
disp('Bearing Designation is 61905');
elseif D==25&&C<8.06
disp('Bearing Designation is *16005');
elseif D==25&&C<10.6
disp('Bearing Designation is 30205');
elseif D==25&&C<11.9
disp('Bearing Designation is *6005');
elseif D==25&&C<14.8
disp('Bearing Designation is *6205');
elseif D==25&&C<17.8
disp('Bearing Designation is 6205 ETN9');
elseif D==25&&C<23.4
disp('Bearing Designation is *6305');
elseif D==25&&C<26
disp('Bearing Designation is 6305 ETN9');
elseif D==25&&C<35.8
disp('Bearing Designation is 6405');
elseif D==28&&C<16.8
disp('Bearing Designation is 62/28');
elseif D==28&&C<25.1
disp('Bearing Designation is 63/28');
elseif D==30&&C<4.49
disp('Bearing Designation is 61806');
elseif D==30&&C<7.28
disp('Bearing Designation is 61906');
elseif D==30&&C<11.9
disp('Bearing Designation is *16006');
elseif D==30&&C<13.8
disp('Bearing Designation is *6006');
elseif D==30&&C<15.9
disp('Bearing Designation is 98206');
elseif D==30&&C<20.3
disp('Bearing Designation is *6206');
elseif D==30&&C<23.4
disp('Bearing Designation is 6206 ETN9');
elseif D==30&&C<29.6
disp('Bearing Designation is *6306');
elseif D==30&&C<32.5
disp('Bearing Designation is 6306 ETN9');
elseif D==30&&C<43.6
disp('Bearing Designation is 6406');
elseif D==35&&C<4.75
disp('Bearing Designation is 61807');
elseif D==35&&C<9.56
disp('Bearing Designation is 61907');
elseif D==35&&C<13
disp('Bearing Designation is *16007');
elseif D==35&&C<16.8
disp('Bearing Designation is *6007');
elseif D==35&&C<27
disp('Bearing Designation is *6207');
elseif D==35&&C<31.2
disp('Bearing Designation is 6207 ETN9');
elseif D==35&&C<35.1
disp('Bearing Designation is *6307');
elseif D==35&&C<55.3
disp('Bearing Designation is 6407');
end
Output
2. Prepare a program which terminates at a<=0.001. Consider initial value of a as 100
and it decreases by 1% in each iteration. Display values of a at each iterations
Program
Function BME60_3_2()
a=100;
i=1;
while a>0.001
A(i)=a;
a=a*0.99;
i=i+1;
end
size(A)
Output
3. In example 2 terminate program in 10 iterations.
Program
Function BME60_3_3()
a=100;
i=1;
while a>0.001 &&i<=10
A(i)=a;
a=a*0.99;
i=i+1;
end
A
size(A)
Output
4. Prepare following matrix using for statement.
[1 2 3 4.10
11 12 13 1420
.
.
91 92 93 94100]
Program
Function BME60_3_4()
For i=1:1:10
for j=1:1:10
A(i,j)=(10*(i-1))+j;
end
end
Output
5. Obtain the variation of Torque(T) with respect to diameter(D) for a shaft.
Consider10<=D<=100, Stress=150 MPa.
Program
Function BME60_3_5()
t=input('Enter value of Torque= ');
D=10:10:100;
fori=1:1:10
T(i)=(pi/16)*t*(D(i)^3);
end
T
Output
6. Obtain variation for the following expression for 0.1<d<10 and D=[10 20 30 40 50]
Program
Function BME60_3_6()
f=input('Enter value of f= ');
p=input('Enter value of Density= ');
h=input('Enter value of h= ');
l=input('Enter value of l= ');
g=9.81;
D=10:10:50;
d=0.1:0.1:10;
fori=1:1:length(D)
for j=1:1:length(d);
A(i,j)=0.25*pi*p*(d(j)^2)*(((2*g*(D(i)^5)*h)/((D(i)^5)+(4*f*l*d(
j)^4)))^1.5);
end
end
Output
Exercise 4 Plot
Q.1 Prepare a Matlab program which gives square of numbers from 1 to 20 and
display number and its square on the screen
Program
Function BME60_4_1()
clc;
clear all;
close all;
%Exercise-1
%Ex-4(1_1)
for i=1:1:20
A(i)=i;
B(i)=i^2;
end
display ([A' B'])
figure(1)
plot(A,B)
Output
400
350
300
250
200
150
100
50
0
0 2 4 6 8 10 12 14 16 18 20
Q.2 Obtain the variation of Torque (T) with respect to diameter (D) for a shaft.
Program
Function BME60_4_2()
t=input('Enter value of Torque= ');
D=10:10:100;
for i=1:1:10
T(i)=(pi/16)*t*(D(i)^3);
end
T
figure(2)
plot(T,D)
Output
Input: Torque
100
90
80
70
60
50
40
30
20
10
0 0.5 1 1.5 2 2.5 3
7
x 10
Q. 3 Obtain variation for the following expression for 0.1<d<10 and D=[10 20 30 40 50]
Program
Function BME60_4_3()
N=input('enter the value of density n=');
G=input('enter the value of gravitational constant g=');
H=input('enter the value of h=');
F=input('enter the value of f=');
L=input('enter the value of l=');
D=10:10:50;
d=.1:.1:10;
for i=1:1:length(D)
for j=1:1:length(d)
R(i,j)=((pi/4)*N*(d(j)^2))*((2*G*H*(D(i)^5))/((D(i)^5)+(4*F*L*(d(j)^4))));
end
end
R
figure(3)
plot(R,D);
hold on
plot(R,d);
Output
50
45
40
35
30
25
20
15
10
0
0 0.5 1 1.5 2 2.5 3
5
x 10
Q. 4 Obtain Surface plot and contour in different script files for (take n=2)
Program
function BME60_4_4_1()
[a,b]=meshgrid(-100:100,-100:100);
X=a.^2+b.^2;
figure(4)
subplot(1,2,1)
surf(X);
subplot(1,2,2)
contour(X,10);
end
Output
Program
Function BME60_4_4_2()
[c,d]=meshgrid(-100:100,-100:100);
Y=abs(c+d)+abs(c.*d);
figure(5)
subplot(1,2,1)
surf(Y);
subplot(1,2,2)
contour(Y);
end
Output
Q.5 Obtain surface plot and contour plot in single figure using Subplot
Program
Function BME60_4_5()
[E,F]=meshgrid(-100:100,-100:100)
Z=E.^2;
S=(E+F).^2;
T=Z+S;
figure(3)
subplot(1,2,1)
surf(T);
subplot(1,2,2)
contour(T);
disp(T)
end
Output
200
180
4
x 10
5 160
4.5
140
4
3.5
120
3
2.5
100
2
1.5
80
0.5 60
0
250 40
200
250
150 200
150 20
100
100
50
50
0 0 20 40 60 80 100 120 140 160 180 200
Function BME60_4_6()
[G]=meshgrid(-30:30)
A=100*((G+1)-G.^2)^2+(G-1)^2;
figure(4)
subplot(2,2,1:2)
surf(A)
[H]=meshgrid(-1.28:1.28)
B=H.^4+(2*H.^4);
subplot(2,2,3)
surf(B)
[I,Q]=meshgrid(-500:500);
C=-((I.*sin(abs(I.^(1/2))))+(Q.*sin(abs(Q.^(1/2)))))
subplot(2,2,4)
surf(C)
end
Output
Q.7 Obtain subplot in following form
Program
Function BME60_4_7()
[J,K]=meshgrid(-5.12:5.12,-5.12:5.12)
D=(J.^2-(10*cos(2*J.*pi)+10))+(K.^2-(10*cos(2*K.*pi)+10));
figure(5)
subplot(2,2,[1 3])
surf(D)
for m=1:2
[L]=meshgrid(-32:32)
E=-20*exp(-0.2*sqrt((1/30)*L.^2)-exp((1/30)*cos(2*L.*pi)))
end
subplot(2,2,2)
surf(E)
[M,N]=meshgrid(-100:100,-100 :100)
F=(M+0.5)^2+(N+0.5)^2;
subplot(2,2,4)
surf(F)
end
Output
EXERCISE-11
Q.1 Develop a Matlab program with following
details: Design problem: Shaft
Input parameters: Power (KW), rpm of shaft, Allowable shear stress, factor of safety,
length of shaft
Output: diameter of shaft, weight of shaft.
Program
Function BME60_11_1()
P=input('input Power(W) = ');
N=input('input RPM = ');
S=input('input Allowable Sheer Stress(Pa) = ');
F=input('input FOS(Factor of Safety) = ');
l=input('input Length of Shaft(m) = ');
R=input('input material density(kg/m3) = ');
d=(1.6747*N*F/S)^(1/3);
w=R*(3.14/4)*(d^2)*l;
display(sprintf('diameter of solid shaft is %d m',d));
display(sprintf('weight of solid shaft is %d kg',w));
end
Input
Output
Q.2 Develop a Matlab program by assuming same data as in problem 1 to find
the material saving if hollow shaft is used instead of solid shaft
Program
Function BME60_11_2()
P=input('input Power(W) = ');
N=input('input RPM = ');
S=input('input Allowable Sheer Stress(Pa) = ');
F=input('input FOS(Factor of Safety) = ');
l=input('input Length of Shaft(m) = ');
R=input('input material density(kg/m3) = ');
% Hollow Shaft
k=2; % k=do/di
di=(24*(10^4)*P*F/(pi^2)/N/S/(k*k+1)/(k-1))^(1/3);
do=k*di;
v2=pi*((do^2)-(di^2))*l/4;
w2=R*v2;
disp(sprintf('The weight of hollow shaft is w(h)=%f kg',w2));
M=w-w2;
disp(sprintf('\nMaterial Saved= %d kg',M));
end
Input
Output
3. Develop a Matlab program to design a cotter joint with following details:
Input: Material properties, load applied on cotter joint (tension and compression), factor
of safety for different parts
Output: All dimensions of cotter joint
Program
Function BME60_11_3()
Tau=input('Allowable shear stress in MPa: ');
S=input('Allowable tensile stress in MPa: ');
F=input('Load applied in KN: ');
FS=input('Factor Of Safety: ');
d=input('Rod Dia in mm: ');
t=0.31*d;
disp(sprintf('\nCotter thickness is %f mm',t));
a=0.75*d;
disp(sprintf('Value of a is %f mm',a));
c=0.75*d;
disp(sprintf('Value of c is %f mm',c));
d3=1.5*d;
disp(sprintf('spigot collar dia is %f mm',d3));
d4=2.4*d;
disp(sprintf('Socket Collar dia is %f mm',d4));
d2=F*FS/(S*t)*1000;
disp(sprintf('Spigot dia is %f mm',d2));
t1=F*FS/((pi)*d2*Tau)*1000;
disp(sprintf('Spigot Collar thickness is %f mm',t1));
end
Input
Output
Q.4 Develop a Matlab code for the following data:
Objective: Selection of single row deep groove ball bearing
Input data: Radial load, axial load, expected life in hours, diameter of shaft
Output: Bearing designation
Program
Function BME60_11_4()
FR=input('Radial load(N): ');%radial load=100%
FA=input('Axial load(N): ');%axial load=200%
D=input('Shaft diameter(mm): ');%shaft diameter=12%
L10h=input('Expected life in hours: ');%life in hours=200%
N=input('RPM of shaft: ');%RPM=1000%
L10=(60*N*L10h)/1000000;
X=0.56;
Y=1.5;
P=X*FR+Y*FA;
C=P*(L10^0.33);
switch D
case 12
if(C<1430)
disp('bearing 61801')
elseif(C>1430 && C<5070)
disp('bearing 6001')
elseif(C>5070 && C<6890)
disp('bearing 6201')
elseif(C>6890 && C<9750)
disp('bearing 6301')
else
disp('no bearing for this dia can withstand this much load')
end
case 15
if(C<1560)
disp('bearing 61802')
elseif(C>1560 && C<5590)
disp('bearing 6002')
elseif(C>5590 && C<7800)
disp('bearing 6202')
elseif(C>7800 && C<11400)
disp('bearing 6302')
else
disp('no bearing for this dia can withstand this much load')
end
case 17
if(C<1680)
disp('bearing 61803')
elseif(C>1680 && C<6050)
disp('bearing 6003')
elseif(C>6050 && C<9560)
disp('bearing 6202')
elseif(C>9560 && C<13500)
disp('bearing 6303')
elseif(C>13500 && C<22900)
disp('bearing 6403')
else
disp('no bearing for this dia can withstand this much load')
end
case 20
if(C<2700)
disp('bearing 61804')
elseif(C>2700 && C<7020)
disp('bearing 16404')
elseif(C>7020 && C<9360)
disp('bearing 6004')
elseif(C>9360 && C<12700)
disp('bearing 6204')
elseif(C>12700 && C<15900)
disp('bearing 6304')
elseif(C>15900 && C<30700)
disp('bearing 6404')
else
disp('no bearing for this dia can withstand this much load')
end
case 25
if(C<3120)
disp('bearing 61805')
elseif(C>3120 && C<7610)
disp('bearing 16005')
elseif(C>7610 && C<11200)
disp('bearing 6005')
elseif(C>11200 && C<14000)
disp('bearing 6205')
elseif(C>14000 && C<22500)
disp('bearing 6305')
elseif(C>22500 && C<35800)
disp('bearing 6405')
else
disp('no bearing for this dia can withstand this much load')
end
case 30
if(C<3120)
disp('bearing 61806')
elseif(C>3120 && C<11200)
disp('bearing 16006')
elseif(C>11200 && C<13300)
disp('bearing 6006')
elseif(C>13300 && C<19500)
disp('bearing 6206')
elseif(C>19500 && C<28100)
disp('bearing 6306')
elseif(C>28100 && C<43600)
disp('bearing 6406')
else
disp('no bearing for this dia can withstand this much load')
end
case 35
if(C<4030)
ME 401P: Computer Aided Design
disp('bearing 61807')
elseif(C>4030 && C<12400)
disp('bearing 16007')
elseif(C>12400 && C<15900)
disp('bearing 6007')
elseif(C>15900 && C<25500)
disp('bearing 6207')
elseif(C>25500 && C<33200)
disp('bearing 6307')
elseif(C>33200 && C<55300)
disp('bearing 6407')
else
disp('no bearing for this dia can withstand this much load')
end
case 40
if(C<4160)
disp('bearing 61808')
elseif(C>4160 && C<13300)
disp('bearing 16008')
elseif(C>13300 && C<16800)
disp('bearing 6008')
elseif(C>16800 && C<30700)
disp('bearing 6208')
elseif(C>30700 && C<41000)
disp('bearing 6308')
elseif(C>41000 && C<63700)
disp('bearing 6408')
else
disp('no bearing for this dia can withstand this much load')
end
case 45
if(C<6050)
disp('bearing 61809')
elseif(C>6050 && C<15600)
disp('bearing 16009')
elseif(C>15600 && C<21200)
disp('bearing 6009')
elseif(C>21200 && C<33200)
disp('bearing 6209')
elseif(C>33200 && C<52700)
disp('bearing 6309')
elseif(C>52700 && C<76100)
disp('bearing 6409')
else
disp('no bearing for this dia can withstand this much load')
end
case 50
if(C<6240)
disp('bearing 61810')
elseif(C>6240 && C<16300)
disp('bearing 16010')
elseif(C>16300 && C<21600)
disp('bearing 6010')
elseif(C>21600 && C<35100)
ME 401P: Computer Aided Design
disp('bearing 6210')
elseif(C>31500 && C<61800)
disp('bearing 6310')
elseif(C>61800 && C<87100)
disp('bearing 6410')
else
disp('no bearing for this dia can withstand this much load')
end
case 55
if(C<8320)
disp('bearing 61811')
elseif(C>8320 && C<19500)
disp('bearing 16011')
elseif(C>19500 && C<28100)
disp('bearing 6011')
elseif(C>28100 && C<43600)
disp('bearing 6211')
elseif(C>43600 && C<71500)
disp('bearing 6311')
elseif(C>71500 && C<99500)
disp('bearing 6411')
else
disp('no bearing for this dia can withstand this much load')
end
case 60
if(C<8710)
disp('bearing 61812')
elseif(C>8710 && C<19900)
disp('bearing 16012')
elseif(C>19900 && C<29600)
disp('bearing 6012')
elseif(C>29600 && C<47500)
disp('bearing 6212')
elseif(C>47500 && C<81900)
disp('bearing 6312')
elseif(C>81900 && C<108006)
disp('bearing 6412')
else
disp('no bearing for this dia can withstand this much load')
end
case 65
if(C<11700)
disp('bearing 61813')
elseif(C>11700 && C<21200)
disp('bearing 16013')
elseif(C>21200 && C<30700)
disp('bearing 6013')
elseif(C>30700 && C<55900)
disp('bearing 6213')
elseif(C>55900 && C<92300)
disp('bearing 6313')
elseif(C>92300 && C<119000)
disp('bearing 6413')
else
ME 401P: Computer Aided Design
disp('no bearing for this dia can withstand this much load')
end
case 70
if(C<13100)
disp('bearing 61814')
elseif(C>13100 && C<28100)
disp('bearing 16014')
elseif(C>28100 && C<37700)
disp('bearing 6014')
elseif(C>37700 && C<61800)
disp('bearing 6214')
elseif(C>61800 && C<104000)
disp('bearing 6314')
elseif(C>104000 && C<143000)
disp('bearing 6414')
else
disp('no bearing for this dia can withstand this much load')
end
case 75
if(C<12500)
disp('bearing 61815')
elseif(C>12500 && C<28600)
disp('bearing 10615')
elseif(C>28600 && C<30700)
disp('bearing 6015')
elseif(C>30700 && C<66300)
disp('bearing 6215')
elseif(C>66300 && C<112000)
disp('bearing 6315')
elseif(C>112000 && C<183000)
disp('bearing 6415')
else
disp('no bearing for this dia can withstand this much load')
end
end
Input
Output