Professional Documents
Culture Documents
Matlab Sheet 2
Arrays
2. Create a row vector that has the following elements: 8, 10/4, 12×1.4, 51,
tan 85°,√26 , and 0.15.
The m-file:
row=[8 10/4 12*1.4 51 tand(85) sqrt(26) 0.15]
In Command Window:
row =
8 2.5 16.8 51 11.43 5.099 0.15
3. Create a row vector in which the first element is 1 and the last element is 43,
with an increment of 6 between the elements (1, 7, 13, … , 43).
The m-file:
row=1:6:43
In Command Window:
row =
1 7 13 19 25 31 37 43
In Command Window:
b=
0 2 4 6 8 10 12 9 6 3 0
5. Create two row vectors: a=2:3:17 and b=3:4:15. Then, by only using the name
of the vectors (a and b), create a row vector c that is made from the elements of
a followed by the elements of b.
The m-file:
a=2:3:17; b=3:4:15;
c=[a,b]
In Command Window:
c=
2 5 8 11 14 17 3 7 11 15
6. Create two column vectors: a=[2:3:17]’ and b=[3:4:15]’. Then, by only using the
name of the vectors (a and b), create a column vector c that is made from the
elements of a followed by the elements of b.
The m-file:
a=[2:3:17]'; b=[3:4:15]';
c=[a;b]
In Command Window:
c=
2
5
8
11
14
17
3
7
11
15
7. A Type this matrix in MATLAB and use MATLAB to carry out the following
Instructions:
3 7 −4 12
𝑨 = [−5 9 10 2 ]
6 13 8 11
15 5 4 1
a. Create a vector v consisting of the elements in the second column of A.
b. Create a vector w consisting of the elements in the second row of A.
The m-file:
A = [3,7,-4,12;-5,9,10,2;6,13,8,11;15,5,4,1];
v = A(:,2)
w = A(2,:)
In Command Window:
v=
7
9
13
5
w=
-5 9 10 2
8. Create the following matrix by using vector notation for creating vectors with
constant spacing and/or the linspace command. Do not type individual
elements explicitly.
The m-file:
A=[130:-20:10; linspace(1,12,7); 12:10:72]
In Command Window:
A=
A=
130 110 90 70 50 30 10
1. 2.8333 4.6667 6.5000 8.3333 10.1667 12.0000
12 22 32 42 52 62 72
9. Create the following matrix by typing one command. Do not type individual
elements explicitly.
The m-file:
%alternative C = [linspace(7,7,5); linspace(7,7,5)]
C=7*ones(2,5)
In Command Window:
C=
77777
77777
10. Create the following matrix by typing one command. Do not type individual
elements explicitly.
The m-file:
D=[zeros(3,4) [8:-1:6]']
In Command Window:
D=
00008
00007
00006
11. Create the following matrix by typing one command. Do not type individual
elements explicitly.
The m-file:
F=[linspace(0,0,5); zeros(3,2) [1:3;10:-2:6;20:6:32]']
In Command Window:
F=
00000
0 0 1 10 20
0 0 2 8 26
0 0 3 6 32
12. Compute the length and absolute value of the following vectors:
a. 𝑥 = [2 4 7]
b. 𝑦 = [2 − 4 7]
c. 𝑥 = [5 + 3𝑖 − 3 + 4𝑖 2 − 7𝑖]
The m-file:
x = [2,4,7];
ans1=length(x)
ans2=abs(x)
y=[2,-4,7];
ans3=length(y)
ans4=abs(y)
z=[5+3i,-3+4i,2-7i];
ans5=length(z)
ans6=abs(z)
In Command Window:
ans1 =
3
ans2 =
2 4 7
ans3 =
3
ans4 =
2 4 7
ans5 =
3
ans6 =
5.83 5 7.28
In Command Window:
x1 =
-5 5 -4 1
x2=
15 13 10 12
x3 =
-4 -5 6 1
x4 =
12 10 13 15
In Command Window:
B=
-5 5 -4 1
3 7 4 2
6 9 8 11
15 13 10 12
C=
-4 3 7 12
-5 2 9 10
6 8 11 13
1 4 5 15
D=
19 34 18 26
E=
18 16 38 25
In Command Window:
B=
0 1.38 0.69
0.69 1.38 4.60
1.94 2.19 1.94
1.09 1.14 3.73
C=
0.693 1.38 4.60
D=
6.68
E=
15.38
F=
3.3391
16. By hand (pencil and paper) write what will be displayed if the following
commands are executed by MATLAB. Check your answers by executing the
commands with MATLAB. (Parts (b), (c), (d), and (e) use the vector that was
defined in part (a).)
(a) a=1:4:17 (b) b=[a(1:3) a] (c) c=[a;a]’
(d) d=[a’ a’]’ (e) e=[[a; a; a; a; a] a’]
The m-file:
disp('Part (a)')
a=1:4:17
disp('Part (b)')
b=[a(1:3) a]
disp('Part (c)')
c=[a;a]'
disp('Part (d)')
d=[a' a']
disp('Part (e)')
e=[[a; a; a; a; a] a']
In Command Window:
Part (a)
a=
1 5 9 13 17
Part (b)
b=
1 5 9 1 5 9 13 17
Part (c)
c=
11
55
99
13 13
17 17
Part (d)
d=
11
55
99
13 13
17 17
Part (e)
e=
1 5 9 13 17 1
1 5 9 13 17 5
1 5 9 13 17 9
1 5 9 13 17 13
1 5 9 13 17 17
17. Using the zeros, ones, and eye commands create the following arrays by typing
one command:
The m-file:
disp('Part (a)')
matrixA=[eye(2) ones(2) zeros(2,1)]
disp('Part (b)')
matrixB=[ones(2,4);eye(2) zeros(2)]
disp('Part (c)')
matrixC=[zeros(2,1) ones(2,3) zeros(2,1); zeros(2,4)
ones(2,1)]
In Command Window:
Part (a)
matrixA =
10110
01110
Part (b)
matrixB =
1111
1111
1000
0100
Part (c)
matrixC =
01110
01110
00001
00001
18. The following table shows the hourly wages, hours worked, and output (number
of widgets produced) in one week for five widget makers.
In Command Window:
earnings =
200.0000 236.5000 240.5000 300.0000 281.2500
total_salary =
1.2582e+003
total_widgets =
5400
average_cost =
0.2330
average_hours =
0.0398
maximum =
5
most_efficient =
1
minimum =
3.9111
least_efficient =
5
The workers earned $200, $236.50, $240.50, $300, and $281.25
respectively. The total salary paid out was$1258.20, and 5400 widgets
were made. The average cost to produce one widget was 23.3 cents, and it
took an average of 0.0398 hr to produce one widget. The first worker, who
produced 5 widgets per dollar of earnings, was the most efficient. The fifth
worker, who produced 3.911 widgets per dollar of earnings, was the least
efficient.
19. The potential energy stored in a spring is kx2/2, where k is the spring constant
and x is the compression in the spring. The force required to compress the spring
is kx. The following table gives the data for five springs:
In Command Window:
x=
0.0110 0.0117 0.0089 0.0077 0.0129
energy =
0.0605 0.0408 0.0356 0.0385 0.0579
The unit for compression is a meter; the unit for energy is a joule.
20. A water tank consists of a cylindrical part of radius r and height h, and a
hemispherical top. The tank is to be constructed to hold 500 m3 of fluid when
filled. The surface area of the cylindrical part is 2𝜋rh, and its volume is 𝜋r2h. The
surface area of the hemispherical top is given by 2𝜋r2, and its volume is given by
2𝜋r3/3. The cost to construct the cylindrical part of the tank is $300/m2 of
surface area; the hemispherical part costs $400/m2. Plot the cost versus r for 2
≤ r ≤ 10 m, and determine the radius that results in the least cost. Compute the
corresponding height h.
The m-file:
r = [2:0.01:10];
V = 500;
h = (V-2*pi*r.^3/3)./(pi*r.^2);
cost = 600*pi*r.*h+800*pi*r.^2;
plot(r,cost),
xlabel('Radius (meters)')
ylabel('Cost ($)')
[radius,mincost] = ginput(1)
hmin = (V-2*pi*radius.^3/3)./(pi*radius.^2)
In Command Window:
The plot is shown in the figure. The minimum cost is $91334. The optimum
radius is 4.8915 m. The required height is 3.3909 m.
21. A cable of length Lc supports a beam of length Lb, so that it is horizontal when
the weight W is attached at the beam end. The principles of statics can be used
to show that the tension force T in the cable is given by
𝐿𝑏 𝐿𝑐 𝑊
𝑇=
𝐷√𝐿2𝑏 − 𝐷2
where D is the distance of the cable attachment point to the beam pivot. See Fig.
1.
a. For the case where W = 400 N, Lb = 3 m, and Lc = 5 m, use element-by
element operations and the min function to compute the value of D that
minimizes the tension T. Compute the minimum tension value.
b. Check the sensitivity of the solution by plotting T versus D. How much
can D vary from its optimal value before the tension T increases 10
percent above its minimum value?
Fig. 1.
The m-file:
x = 10; W = 400;
Lb = 3;
Lc = 5;
D = [0:0.01:Lb];
T = Lb*Lc*W./(D.*sqrt(Lb^2-D.^2));
[minT, k] = min(T)
minD = D(k)
Tension_at_min=T(k)
Distance_at_min=D(k)
%plotting
Dplot = [1.5:0.001:2.2];
upper = 1.1*minT
Tplot = Lb*Lc*W./(Dplot.*sqrt(Lb^2-Dplot.^2));
plot(Dplot,Tplot,[1.5,2.2],[upper,upper])
xlabel('D (mm)');
ylabel('T (N)')
grid
In Command Window:
a.
minT =
1.3333e+03
k=
213
minD =
2.1200
Tension_at_min =
1.3333e+03
Distance_at_min =
2.1200
The solution is minT = 1.3333e+003 and minD = 2.12, which correspond to a
tension of T = 1333 N and a distance of D = 2.12 m.
b. The upper tension value is 1.1(1333) = 1467 N. The intersection of the two lines on
the plot gives the solution, which is approximately D = 1.6 m (1.62 is a more accurate
value).
1550
1500
X: 1.62
Y: 1467
1450
T (N)
1400
1350
1300
1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3
D (mm)
22. Use MATLAB to find the products AB and BA for the following matrices:
11 5 −7 −8
𝐴=[ ] 𝐵=[ ]
−9 −4 6 2
The m-file:
A = [11,5;-9,-4];
B = [ -7,-8;6,2];
ans1=A*B
ans2=B*A
In Command Window:
ans1 =
-47 -78
39 64
ans2 =
-5 -3
48 22
23. The following tables show the costs associated with a certain product and the
production volume for the four quarters of the business year. Use MATLAB to
find
a. The quarterly costs for materials, labor, and transportation.
b. The total material, labor, and transportation costs for the year
c. The total quarterly costs.
Solution
For part (a) note that the first quarter material cost is computed by
and so on. Thus the quarterly costs can be computed by multiplying the transpose of
the matrix of unit costs by the matrix of quarterly production volumes. The resulting 3
× 4 matrixis quarterly_costs. Its first row contains the material costs, its second
row contains the labor costs, and the third row contains the transportation costs. The
four columns of quarterly_costs correspond to the four quarters.
For part (b) the yearly costs for materials, labor, and transportation are found by
summing the rows of quarterly_costs, or equivalently, by summing the columns
of the transpose of quarterly_costs.
For part (c) the total quarterly costs are found by summing the columns of
quarterly_costs.
The m-file:
unit_cost = [7,3,2;3,1,3;9,4,5;2,5,4;6,2,1];
quarterly_volume =
[16,14,10,12;12,15,11,13;8,9,7,11;14,13,15,17;13,16,12,
18];
quarterly_costs = unit_cost'*quarterly_volume
yearly_costs = sum(quarterly_costs')
total_quarter_cost = sum(quarterly_costs)
In Command Window:
quarterly_costs =
326 346 268 364
188 190 168 214
177 186 160 204
yearly_costs =
1304
760
727
total_quarter_cost =
691 722 596 782
The following table was created from the matrix quarterly_costs. All costs are in
thousands of dollars.
Table
Quarterly Costs
Category Quarter 1 Quarter 2 Quarter 3 Quarter 4
Materials 326 346 268 364
Labor 188 190 168 214
Transportation 177 186 160 204
24. Aluminum alloys are made by adding other elements to aluminum to improve
its properties, such as hardness or tensile strength. The following table shows
the composition of ve commonly used alloys, which are known by their alloy
numbers (2024, 6061, and so on). Obtain a matrix algorithm to compute the
amounts of raw materials needed to produce a given amount of each alloy. Use
MATLAB to determine how much raw material of each type is needed to produce
1000 tons of each alloy.
Solution
The amount of copper (Cu) needed to produce 1000 tons of each alloy is obtained by
multiplying the Cu column in the table by 1000(0.01) = 10. (The 0.01 is neeed to
convert the table’s percents into decimal values.) Thus we need
1000(0.01)(4.4+0+0+1.6+0) = 60 tons of copper. Extending this method, we can see
that we must multiply the matrix composition obtained from the table by a row
vector consisting of five columns containing the value 10.
The m-file:
composition =
[4.4,1.5,.6,0,0;0,1,0,.6,0;0,1.4,0,0,4.5;1.6,2.5,0,0,5.
6;0,.3,0,7,0];
alloy = 10*ones(1,5)
raw_material = alloy*composition
In Command Window:
alloy =
10 10 10 10 10
raw_material =
60 67 6 76 101
where, for a particular airfoil, the lift and drag coefficients versus angle of
attack , are given by
Using the first two equations, we see that the lift-to-drag ratio is given simply by
the ratio CL /CD.
Plot L/D versus , for -2° ≤ 𝛼 ≤ 22°. Determine the angle of attack that maximizes
L/D.
The m-file:
rho = 0.002378;
S = 36;
alpha = [-2:0.01:22];
CL = [4.47e-5,1.15e-3,6.66e-2,0.102];
CD = [5.75e-6,5.09e-4,1.81e-4,1.25e-2];
LoverD = polyval(CL,alpha)./polyval(CD,alpha);
plot(alpha,LoverD)
xlabel('Angle of Attack \alpha (degrees)')
ylabel('Lift/Drag (dimensionless)')
grid
In Command Window:
max_ratio =
17.9415
k=
582
alpha_at_max_ratio =
3.81
16
14
12
Lift/Drag (dimensionless)
10
-2
-5 0 5 10 15 20 25
Angle of Attack (degrees)
In Command Window:
X=
4.4299
-4.1132
2.4475
-0.8248
In Command Window:
y=
-2.1186 + 1.4999i
-2.1186 - 1.4999i
1.5706
d=
-2.3008
-0.1706 + 2.1767i
-0.1706 - 2.1767i
1.9754
28. The hydraulic cylinder shown ill figure has a bore diameter (A) and operates at
a pressure of 5.5 MPa. The diameter of bolts used are 20mm and the bore
diameter varies from (200 to 1000 mm). Find the safety factor of if the bolt is
made from carbon steel with yield stress of 460 MPa. The number of bolts used
is 6.
𝜋 2
𝐹= 𝐴 × 𝑃𝑟𝑒𝑠𝑠𝑢𝑟𝑒
4
𝐹
𝐹𝐵𝑜𝑙𝑡 =
𝑁𝑜. 𝑜𝑓 𝐵𝑜𝑙𝑡𝑠
𝐹𝐵𝑜𝑙𝑡
𝜎𝐵𝑜𝑙𝑡 = 𝜋
2
4 𝑑𝐵𝑜𝑙𝑡
Fig. 2.
The m-file:
d=20 ; A=200:100:1000 ; p=5.5 ; sy=460 ; N=6 ;
F=p*pi*(A.^2)/4 ;
Fb=F/N ;
st=Fb/(pi*(d^2)/4);
n=sy./st
In Command Window:
n=
5.0182 2.2303 1.2545 0.8029 0.5576 0.4096 0.3136 0.2478 0.2007
29. A screw jack as shown in Fig.3 has the following data: Outer and inner diameter
of the screw is 20 mm and 15 mm, pitch 5 mm, square threads, coefficient of
friction, 𝜇, is 0.15 and its lead, 𝑙, is 10 mm. Find the torque required, 𝑇, for rising
the car if the capacity of the screw jack, F = (10,12,1.1,16,18,20,2'2) kN. The
following formula can be applied,
𝑙 + 2𝜇𝜋𝑑𝑚 sec 𝛼
𝑇=𝐹
𝜋𝑑𝑚 − 𝜇𝑙 sec 𝛼
where
𝑑𝑚 mean diameter= (inner diameter +outer diameter)/2
𝛼: angle and equal to zero
Fig.3 .
The m-file:
do=20 ; di=15 ; L=10 ; mew=0.15;
F=[10 12 14 16 18 20 22]*1000 ;
dm=(do+di)/2 ;
T=F*(L+mew*pi*dm)/(pi*dm-mew*L)/1000
In Command Window:
T=
3.412 4.094 4.776 5.459 6.141 6.824 7.506
Fig.4.
The m-file:
F=18*1000 ; a=10 ; b=15 ;
d=[10 12 14 16 20] ; I=pi*(d.^4)/64 ;
taw=F./(2*pi*(d.^2)/4)
sb=F*(a+b)*d./(4*I*2)
sc=F./(b*d)
In Command Window:
taw=
114.591 79.577 58.465 44.7623 28.647
sb=
1145.915 663.145 417.607 279.764 143.239
sc=
120 100 85.71 60
31. A shaft is subjected to an axial load of 12 KN and bending moment of 300 N.m.
Find the suitable diameter of the shaft if the allowable stress for shaft material
200 Mpa.
The m-file:
P=12*1000 ; M=300*1000 ; sa=200 ;
p1=[pi*sa 0 -4*P -32*M ];
d=roots(p1)
In Command Window:
d=
25.83
-12.91 +20.6i
-12.91 -20.6i
32. A 0.75 kW electric motor runs at 30 rev/s; see the fig. 5. The gears has a normal
pressure angle (∅𝑛 ) of 20°, helix angle (𝜑) is 30° and normal module (𝑚𝑛 ) is 2
mm find the different forces acting between the gears if the power transmitted
changes from 0.75 to 10 kW,
d=diameter of gear
N = number of teeth for gear
𝑚𝑡 = 𝑑/𝑁, tangential module
𝑚𝑛 = 𝑚𝑡 cos 𝜑
tan ∅𝑡
tan ∅𝑛 =
cos 𝜑
𝐹𝑡 = 𝑇⁄𝑅, tangential force
𝑅 = 𝑑𝑖𝑎𝑚𝑒𝑡𝑒𝑟 𝑜𝑓 𝑔𝑒𝑎𝑟⁄2
𝐹𝑟 = 𝐹𝑡 tan ∅𝑡 , radial force
𝐹𝑎 = 𝐹𝑡 tan 𝜑, axial force
Fig. 5.
The m-file:
power=[0.75 1 2 3 4 5 6 7 8 9 10]*1000 ;
w=2*pi*30 ;phn=20*pi/180 ;ep=30*pi/180 ; mn=2/1000 ;
N=36 ;
T=power/w ;
pht=atan(tan(phn)*cos(ep));
mt=mn/cos(ep);
d=mt*N ;
R=d/2 ;
Ft=T/R
Fr=Ft*tan(pht)
Fa=Ft*tan(ep)
In Command Window:
Ft =
95.71 127.62 255.24 382.86 510.48 638.11
765.73 893.35 1020.97 1148.6 1276.22
Fr=
33. For the pressure cylinder shown in fig. 6. determine the radial and tangential
stresses for different values of r (60, 70, 80, 90, 100). The internal pressure is 6
bar and no external pressure. The inner and
outer diameters of the cylinder are 120 and
200 mm.
𝑝𝑖 𝑟𝑖2 − 𝑝𝑜 𝑟𝑜2 − 𝑟𝑖2 𝑟𝑜2 (𝑝𝑜 − 𝑝𝑖 )/𝑟 2
𝜎𝑡 =
𝑟𝑜2 − 𝑟𝑖2
𝑝𝑖 𝑟𝑖2 − 𝑝𝑜 𝑟𝑜2 + 𝑟𝑖2 𝑟𝑜2 (𝑝𝑜 − 𝑝𝑖 )/𝑟 2
𝜎𝑟 =
𝑟𝑜2 − 𝑟𝑖2
Fig. 6.
The m-file:
po=0 ; pi=0.6 ; ro=100 ; ri=60 ;
r=[60 70 80 90 100]
st=(pi*(ri^2)-po*(ro^2)-((ri*ro)^2)*(po-
pi)./(r.^2))/(ro^2-ri^2)
sr=(pi*(ri^2)-po*(ro^2)+((ri*ro)^2)*(po-
pi)./(r.^2))/(ro^2-ri^2)
In Command Window:
st =
1.275 1.026 0.864 0.754 0.675
sr =
-0.6 -0.351 -0.189 -0.0791 0
34. For the flat belt shown in fig 7, study the effect of large pulley diameter on both
belt length and contact angle, also find the driving torque acting on small pulley
if the power transmitted 12 kW, the speed of small pulley is 1200 rpm and its
diameter is 200 mm, the center distance between the two shafts (C) is 1 m.
𝐿 = [4𝐶 2 − (𝐷 + 𝑑)2 ]0.5 + 0.5(𝐷 + 𝑑)𝜃
𝐷+𝑑
𝜃 = 𝜋 + 2 sin−1
2𝐶
Fig. 7.
The m-file:
x = 10; power=12*1000 ;ws=2*pi*1200/60 ; c=1 ;
d=200/1000 ;
D=[200 300 400 500 600]/1000 ;
theta=pi+2*asin((D+d)/(2*c))
L=sqrt(4*(c^2)-((D+d).^2))+(D+d).*(theta/2)
Ts=power/ws
In Command Window:
theta =
3.54 3.64 3.75 3.85 3.96
L=
2.66 2.84 3.03 3.22 3.41
Ts =
95.49