matlab sheet 2 arrays - drahmednagib.com · dr./ ahmed nagib elmekawy 3 of 32 matlab sheet 2...

32
Faculty of Engineering Spring 2017 Mechanical Engineering Department Dr./ Ahmed Nagib Elmekawy 1 of 32 Matlab Sheet 2 Solution Matlab Sheet 2 Arrays 1. a. Create the vector x having 50 logarithmically spaced values starting at 10 and ending at 1000. b. Create the vector x having 20 logarithmically spaced values starting at 10 and ending at 1000. The m-file: x1=logspace(1,3) x2=logspace(1,3,20) 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

Upload: others

Post on 23-Oct-2019

67 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 1 of 32 Matlab Sheet 2 Solution

Matlab Sheet 2

Arrays

1. a. Create the vector x having 50 logarithmically spaced values starting at

10 and ending at 1000.

b. Create the vector x having 20 logarithmically spaced values starting at

10 and ending at 1000.

The m-file:

x1=logspace(1,3)

x2=logspace(1,3,20)

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

Page 2: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 2 of 32 Matlab Sheet 2 Solution

4. Use a single command to create a row vector (assign it to a variable named b)

with 11 elements, such that

b = 0 2 4 6 8 10 12 9 6 3 0

Do not type the vector explicitly.

The m-file:

b=[0:2:12 9:-3:0]

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

Page 3: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution

14

17

3

7

11

15

7. A Type this matrix in MATLAB and use MATLAB to carry out the following

Instructions:

𝑨 = [

3 7−5 9

−4 1210 2

6 1315 5

8 114 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

Page 4: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 4 of 32 Matlab Sheet 2 Solution

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 =

7 7 7 7 7

7 7 7 7 7

Page 5: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 5 of 32 Matlab Sheet 2 Solution

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 =

0 0 0 0 8

0 0 0 0 7

0 0 0 0 6

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 =

0 0 0 0 0

0 0 1 10 20

0 0 2 8 26

0 0 3 6 32

Page 6: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 6 of 32 Matlab Sheet 2 Solution

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

Page 7: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 7 of 32 Matlab Sheet 2 Solution

13. Given the matrix

𝐴 = [

3 7−5 9

−4 1210 2

6 1315 5

8 114 1

]

a. Find the maximum and minimum values in each column.

b. Find the maximum and minimum values in each row.

The m-file:

A = [3,7,-4,12;-5,9,10,2;6,13,8,11;15,5,4,1];

x1=min(A)

x2=max(A)

x3=min(A')

x4=max(A')

In Command Window:

x1 =

-5 5 -4 1

x2=

15 13 10 12

x3 =

-4 -5 6 1

x4 =

12 10 13 15

Page 8: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 8 of 32 Matlab Sheet 2 Solution

14. Given the matrix

𝐴 = [

3 7−5 9

−4 1210 2

6 1315 5

8 114 1

]

a. Sort each column and store the result in an array B.

b. Sort each row and store the result in an array C.

c. Add each column and store the result in an array D.

d. Add each row and store the result in an array E.

The m-file:

A = [3,7,-4,12;-5,9,10,2;6,13,8,11;15,5,4,1];

B = sort(A)

C = [sort(A')]'

D = sum(A)

E = sum(A')

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

Page 9: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 9 of 32 Matlab Sheet 2 Solution

15. Consider the following arrays.

𝑨 = [

1 4 2 2 4 100

7 9 7 3 𝜋 42

] 𝑩 = ln 𝑨

Write MATLAB expressions to do the following.

a. Select just the second row of 𝑩.

b. Evaluate the sum of the second row of B.

c. Multiply the second column of B and the first column of A element by element.

d. Evaluate the maximum value in the vector resulting from element-by element

multiplication of the second column of 𝑩 with the first column of A.

e. Use element-by-element division to divide the first row of A by the first three

elements of the third column of B. Evaluate the sum of the elements of the

resulting vector.

The m-file:

A = [1,4,2;2,4,100;7,9,7;3,pi,42];

B = log(A)

C = B(2,:)

D = max(B(:,2).*A(:,1))

E = sum(A(1,:)./B(1:3,3)')

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

Page 10: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 10 of 32 Matlab Sheet 2 Solution

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 =

1 1

5 5

9 9

13 13

17 17

Part (d)

d =

1 1

Page 11: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 11 of 32 Matlab Sheet 2 Solution

5 5

9 9

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

Page 12: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 12 of 32 Matlab Sheet 2 Solution

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 =

1 0 1 1 0

0 1 1 1 0

Part (b)

matrixB =

1 1 1 1

1 1 1 1

1 0 0 0

0 1 0 0

Part (c)

matrixC =

0 1 1 1 0

0 1 1 1 0

0 0 0 0 1

0 0 0 0 1

Page 13: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 13 of 32 Matlab Sheet 2 Solution

18. The following table shows the hourly wages, hours worked, and output (number

of widgets produced) in one week for five widget makers.

Use MATLAB to answer these questions:

a. How much did each worker earn in the week?

b. What is the total salary amount paid out?

c. How many widgets were made?

d. What is the average cost to produce one widget?

e. How many hours does it take to produce one widget on average?

f. Assuming that the output of each worker has the same quality, which worker

is the most efficient? Which is the least efficient?

The m-file:

wage=[5,5.5,6.5,6,6.25];

hours=[40,43,37,50,45];

output=[1000,1100,1000,1200,1100];

earnings=wage.*hours

total_salary=sum(earnings)

total_widgets=sum(output)

average_cost=total_salary/total_widgets

average_hours=sum(hours)/total_widgets

[maximum,most_efficient]=max(output./earnings)

[minimum,least_efficient]=min(output./earnings)

In Command Window:

earnings =

200.0000 236.5000 240.5000 300.0000 281.2500

total_salary =

1.2582e+003

total_widgets =

5400

Page 14: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 14 of 32 Matlab Sheet 2 Solution

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.

Page 15: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 15 of 32 Matlab Sheet 2 Solution

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:

Use MATLAB to find

(a) the compression x in each spring

(b) the potential energy stored in each spring.

The m-file:

force = [11,7,8,10,9];

k = [1000,600,900,1300,700];

x = force./k

energy = 0.5*k.*x.^2

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.

Page 16: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 16 of 32 Matlab Sheet 2 Solution

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.

Page 17: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 17 of 32 Matlab Sheet 2 Solution

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)');

Page 18: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 18 of 32 Matlab Sheet 2 Solution

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).

1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.31300

1350

1400

1450

1500

1550

X: 1.62

Y: 1467

D (mm)

T (

N)

Page 19: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 19 of 32 Matlab Sheet 2 Solution

22. Use MATLAB to find the products AB and BA for the following matrices:

𝐴 = [11 5−9 −4

] 𝐵 = [−7 −86 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

Page 20: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 20 of 32 Matlab Sheet 2 Solution

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

7(16)+ 3(12)+ 9(8)+ 2(14)+ 5(13) = 326

and the second quarter material cost is computed by

7(14)+ 3(15)+ 9(9)+ 2(13)+ 6(16) = 346

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.

Page 21: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 21 of 32 Matlab Sheet 2 Solution

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

From the vector yearly_costs we obtain the following information: yearly

materials cost = $1,304,000, yearly labor cost = $760,000, and yearly transportation

cost = $727,000. From the vector total_quarter_cost we find that the total costs

in each quarter are $691,000, $722,000, $596,000, and $782,000 respectively.

Page 22: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 22 of 32 Matlab Sheet 2 Solution

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

Page 23: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 23 of 32 Matlab Sheet 2 Solution

25. The lift-to-drag ratio is an indication of the effectiveness of an airfoil. The

equations for lift and drag are

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

Page 24: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 24 of 32 Matlab Sheet 2 Solution

The figure is shown below:

26. Solve the following equations

4.5𝑥1 + 6.8𝑥2 + 6𝑥3 + 2𝑥4 = 5

1.2𝑥1 + 3𝑥2 + 9.1𝑥3 + 10𝑥4 = 5

𝑥2 + 4𝑥3 + 5.1𝑥4 + 𝑥1 − 5.9 = 0

3𝑥1 + 𝑥2 + 𝑥3 + 5𝑥4 = 7.5

The m-file:

A=[4.5 6.8 6 2 ; 1.2 3 9.1 10 ; 1 1 4 5.1 ; 3 1 1 5] ;

B=[5 ; 7 ; 5.9 ;7.5] ;

X=inv(A)*B

In Command Window:

X =

4.4299

-4.1132

2.4475

-0.8248

-5 0 5 10 15 20 25-2

0

2

4

6

8

10

12

14

16

18

X: 3.81

Y: 17.94

Angle of Attack (degrees)

Lift/

Dra

g (

dim

ensio

nle

ss)

Page 25: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 25 of 32 Matlab Sheet 2 Solution

27. Find the roots of the following equations

a. 𝑦3 + 32𝑦2 + 𝑦 = 127

b. 3𝑑4 + 2𝑑3 + 𝑑2 = 65

The m-file:

p1=[12 32 1 -127] ;

p2=[3 2 1 0 -65] ;

y=roots(p1)

d=roots(p2)

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

Page 26: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 26 of 32 Matlab Sheet 2 Solution

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.

𝐹 =𝜋

4𝐴2 × 𝑃𝑟𝑒𝑠𝑠𝑢𝑟𝑒

𝐹𝐵𝑜𝑙𝑡 =𝐹

𝑁𝑜. 𝑜𝑓 𝐵𝑜𝑙𝑡𝑠

𝜎𝐵𝑜𝑙𝑡 =𝐹𝐵𝑜𝑙𝑡

𝜋4 𝑑𝐵𝑜𝑙𝑡

2

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

Page 27: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 27 of 32 Matlab Sheet 2 Solution

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

Page 28: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 28 of 32 Matlab Sheet 2 Solution

30. The pin connector shown in Fig. 4 is subjected

to a load of 18 KN. Find the stresses acting at

the pin if its diameter is (10, 12, 14, 16 and 20

mm). Take a=10 mm and b=15 mm.

𝐼 =𝜋

64𝑑4

𝜏 =𝐹

2𝜋𝑑2/4

𝜎𝑏 =𝐹(𝑎 + 𝑏)𝑑

8𝐼

𝜎𝑐 =𝐹

𝑏𝑑

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

Page 29: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 29 of 32 Matlab Sheet 2 Solution

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

Page 30: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 30 of 32 Matlab Sheet 2 Solution

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=

Page 31: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 31 of 32 Matlab Sheet 2 Solution

30.17 40.22 80.45 120.68 160.91 201.137

241.36 281.59 321.82 362.04 402.27

Fa=

55.26 73.68 147.36 221.048 294.73 368.41

442.097 515.77 589.46 663.14 736.82

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

Page 32: Matlab Sheet 2 Arrays - drahmednagib.com · Dr./ Ahmed Nagib Elmekawy 3 of 32 Matlab Sheet 2 Solution 14 17 3 7 11 15 7. A Type this matrix in MATLAB and use MATLAB to carry out the

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 32 of 32 Matlab Sheet 2 Solution

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