matlab sheet 3 solution - ahmed nagib

21
Faculty of Engineering Spring 2017 Mechanical Engineering Department Dr./ Ahmed Nagib Elmekawy 1 of 21 Matlab Sheet 3 – Solution Matlab Sheet 3 - Solution Plotting in Matlab 1. a. Estimate the roots of the following equation 3 − 3 2 + 5 sin ( 4 5 4 )+3=0 by plotting the equation. b. Use the estimates found in part a to find the roots more accurately with the fzero function. The m-file: row clc; clear;clf x = (-2:0.01:4); y=x.^3-3*x.^2+5*x.*sin(pi*x/4-5*pi/4)+3; plot(x,y) xlabel('x'),ylabel('f(x)'),grid z=fzero('x.^3-3*x.^2+5*x.*sin(pi*x/4-5*pi/4)+3',3.8) From the plot with either method we can estimate the roots to be x = 0.5, 1.1, and 3.8. b) Then use the fzero function as follows, for the point x = 3.8. Repeating these steps for the estimates at 0.5 and 1.1, all of these methods give the roots x = 0.4795, 1.1346, and 3.8318. In Command Window: z = 3.8318 Figure -2 -1 0 1 2 3 4 -25 -20 -15 -10 -5 0 5 x f(x)

Upload: others

Post on 10-Jun-2022

37 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 1 of 21 Matlab Sheet 3 – Solution

Matlab Sheet 3 - Solution

Plotting in Matlab

1. a. Estimate the roots of the following equation

𝑥3 − 3𝑥2 + 5𝑥 sin (𝜋𝑥

4−

5𝜋

4) + 3 = 0

by plotting the equation.

b. Use the estimates found in part a to find the roots more accurately with

the fzero function.

The m-file:

row clc; clear;clf x = (-2:0.01:4);

y=x.^3-3*x.^2+5*x.*sin(pi*x/4-5*pi/4)+3;

plot(x,y)

xlabel('x'),ylabel('f(x)'),grid

z=fzero('x.^3-3*x.^2+5*x.*sin(pi*x/4-5*pi/4)+3',3.8)

From the plot with either method we can estimate the roots to be x = −0.5, 1.1, and 3.8. b) Then use the fzero function as follows, for the point x = 3.8. Repeating these steps for the estimates at −0.5 and 1.1, all of these methods give the roots x = −0.4795, 1.1346, and 3.8318.

In Command Window:

z =

3.8318

Figure

-2 -1 0 1 2 3 4

-25

-20

-15

-10

-5

0

5

x

f(x)

Page 2: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

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

2. Plot columns 2 and 3 of the following matrix A versus column 1. The data in

column 1 are time (seconds). The data in columns 2 and 3 are force (newtons).

The m-file:

A = [0,-7,6;5,-4,3;10,-1,9;15,1,0;20,2,-1];

plot(A(:,1),A(:,2:3))

xlabel('Time (seconds)'),

ylabel('Force (Newtons)')

gtext('Column 1'),gtext('Column 2')

Figure

0 2 4 6 8 10 12 14 16 18 20-8

-6

-4

-2

0

2

4

6

8

10

Time (seconds)

Forc

e (

New

tons)

Column 1

Column 2

Page 3: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 3 of 21 Matlab Sheet 3 – Solution

3. Many applications use the following “small angle” approximation for the

sine to obtain a simpler model that is easy to understand and analyze. This

approximation states that sin x ≈ x, where x must be in radians. Investigate

the accuracy of this approximation by creating three plots. For the first, plot sin

x and x versus x for 0 ≤ x ≤ 1. For the second, plot the approximation error sin

x - x versus x for 0 ≤ x ≤ 1. For the third, plot the relative error [sin(x) - x]/sin(x)

versus x for 0 ≤ x ≤ 1. How small must x be for the approximation to be accurate

within 5 percent?

The m-file:

x = 0:0.01:1;

subplot(2,2,1)

plot(x,sin(x),x,x)

xlabel('x (radians)'),ylabel('x and sin(x)')

gtext('x'),gtext('sin(x)')

subplot(2,2,2)

plot(x,abs(sin(x)-x))

xlabel('x (radians)'),ylabel('Error: sin(x) - x')

subplot(2,2,3)

plot(x,abs(100*(sin(x)-x))./sin(x)),xlabel('x

(radians)'),...

ylabel('Percent Error'),grid

Figure

0 0.5 10

0.5

1

x (radians)

x a

nd s

in(x

) x

sin(x)

0 0.5 10

0.05

0.1

0.15

0.2

x (radians)

Err

or:

sin

(x)

- x

0 0.5 10

5

10

15

20

x (radians)

Perc

ent

Err

or

Page 4: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 4 of 21 Matlab Sheet 3 – Solution

4. You can use trigonometric identities to simplify the equations that appear

in many applications. Confirm the identity tan(2x) = 2 tan x/(1 - tan2 x)

by plotting both the left and the right sides versus x over the range 0 ≤ x ≤ 2𝜋.

The m-file:

x =0:0.01:2*pi;

left = tan(2*x);

right = 2*tan(x)./(1-tan(x).^2);

plot(x,left,'-rs',x,right)

xlabel('x (radians)')

ylabel('Left and Right Sides')

legend('Left','Right','location', 'best')

Figure

0 1 2 3 4 5 6 7-250

-200

-150

-100

-50

0

50

100

x (radians)

Left

and R

ight

Sid

es

Left

Right

Page 5: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 5 of 21 Matlab Sheet 3 – Solution

5. The following functions describe the oscillations in electric circuits and the

vibrations of machines and structures. Plot these functions on the same plot.

Because they are similar, decide how best to plot and label them to avoid

confusion.

𝑥(𝑡) = 10 𝑒−0.5𝑡 sin(3𝑡 + 2)

𝑦(𝑡) = 7 𝑒−0.4𝑡 cos(5𝑡 − 3)

The m-file:

t = 0:0.02:10;

x = 10*exp(-.5*t).*sin(3*t+2);

y = 7*exp(-.4*t).*cos(5*t-3);

plot(t,x,t,y,':')

xlabel('t'),ylabel('x(t), y(t)')

legend('x(t)','y(t)')

Figure

0 1 2 3 4 5 6 7 8 9 10-8

-6

-4

-2

0

2

4

6

8

10

t

x(t

), y

(t)

x(t)

y(t)

Page 6: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 6 of 21 Matlab Sheet 3 – Solution

6. In certain kinds of structural vibrations, a periodic force acting on the structure

will cause the vibration amplitude to repeatedly increase and decrease with

time. This phenomenon, called beating, also occurs in musical sounds. A

particular structure’s displacement is described by

𝑦(𝑡) =1

𝑓12 − 𝑓2

2[cos(𝑓2𝑡) − cos(𝑓1𝑡)]

where y is the displacement in mm and t is the time in seconds. Plot y versus t

over the range 0 ≤ t ≤ 20 for 𝑓1 = 8 rad/sec and𝑓2 = 1 rad/sec. Be sure to choose

enough points to obtain an accurate plot.

The m-file:

t = 0:0.02:20;

y = (1/(64-1))*(cos(8*t)-cos(t));

plot(t,y)

xlabel('t (seconds)')

ylabel('Displacement y (Inches)')

Figure

0 2 4 6 8 10 12 14 16 18 20-0.04

-0.03

-0.02

-0.01

0

0.01

0.02

0.03

0.04

t (seconds)

Dis

pla

cem

ent

y (

Inches)

Page 7: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 7 of 21 Matlab Sheet 3 – Solution

7. Oscillations in mechanical structures and electric circuits can often be

𝑦(𝑡) = 𝑒−𝑡 𝜏⁄ sin(𝜔𝑡 + 𝜙)

where t is time and 𝜔 is the oscillation frequency in radians per unit time.

The oscillations have a period of 2𝜋/𝜔, and their amplitudes decay in time

at a rate determined by 𝜏, which is called the time constant. The smaller 𝜏

is, the faster the oscillations die out.

a. Use these facts to develop a criterion for choosing the spacing of the t values

and the upper limit on t to obtain an accurate plot of y(t).

(Hint: Consider two cases: 4𝜏 > 2𝜋 𝜔⁄ and 4𝜏 < 2𝜋 𝜔⁄ )

b. Apply your criterion, and plot y(t) for 𝜏 = 10, 𝜔 = 𝜋 and 𝜙 = 2.

c. Apply your criterion, and plot y(t) for 𝜏 = 0.1, 𝜔 = 𝜋 and 𝜙 = 2.

The m-file:

% Part a

t = 0:0.02:40;

y = exp(-t/10).*sin(pi*t+2);

plot(t,y)

xlabel('t'),ylabel('y')

title('Part a')

%Part b

figure(2)

t2 = 0:0.001:0.4;

y2 = exp(-t2/.1).*sin(8*pi*t2+2);

plot(t2,y2)

xlabel('t'),ylabel('y')

title('Part b')

Page 8: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 8 of 21 Matlab Sheet 3 – Solution

Figures:

0 5 10 15 20 25 30 35 40-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

t

y

Part a

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

t

y

Part b

Page 9: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 9 of 21 Matlab Sheet 3 – Solution

8. The vibrations of the body of a helicopter due to the periodic force applied by

the rotation of the rotor can be modeled by a frictionless spring-mass-damper

system subjected to an external periodic force. The position 𝑥(𝑡) of the mass is

given by the equation:

𝑥(𝑡) =2𝑓𝑜

𝜔𝑛3 − 𝜔3

sin (𝜔𝑛 − 𝜔

2 𝑡) sin (

𝜔𝑛 + 𝜔

2 𝑡)

where 𝐹(𝑡) = 𝐹0 sin 𝜔𝑡, and 𝑓0 = 𝐹0/𝑚, ω is the frequency of the applied

force, and 𝜔𝑛 is the natural frequency of the helicopter. When the value of 𝜔

is close to the value of 𝜔𝑛, the vibration consists of fast oscillation with

slowly changing amplitude called beat. Use 𝑓0 = 12 N/kg, 𝜔𝑛=10 rad/s,

and 𝜔=12 rad/s to plot 𝑥(𝑡) as a function of t for 0 ≤ t ≤ 10 s.

The m-file:

clc; clear;clf

f0=12; wn=10; w=12;

t=0:.1:10;

x=2*f0/(wn^3-w^3)*sin((wn-w)*t/2).*...

sin((wn+w)*t/2)

plot(t,x)

title('Helicopter Body Vibrations')

xlabel('Time, s')

ylabel('Displacement, m')

Page 10: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 10 of 21 Matlab Sheet 3 – Solution

Figure

0 1 2 3 4 5 6 7 8 9 10-0.04

-0.03

-0.02

-0.01

0

0.01

0.02

0.03

0.04Helicopter Body Vibrations

Time, s

Dis

pla

cem

ent,

m

Page 11: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 11 of 21 Matlab Sheet 3 – Solution

9. A railroad bumper is designed to slow down a rapidly moving railroad car. After

a 20,000 kg railroad car traveling at 20 m/s engages the bumper, its

displacement x (in meters) and velocity v (in m/s) as a function of time t (in

seconds) is given by:

Plot the displacement and the velocity as a function of time for 0 ≤ t ≤ 4 s. Make

two plots on one page.

The m-file:

t=0:.01:4;

x=4.219*(exp(-1.58*t)-exp(-6.32*t));

v=26.67*exp(-6.32*t)-6.67*exp(-1.58*t);

subplot(2,1,1)

plot(t,x)

title('Railroad Bumper Response')

ylabel('Position, m')

subplot(2,1,2)

plot(t,v)

ylabel('Speed, m/s')

xlabel('Time, s')

Page 12: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 12 of 21 Matlab Sheet 3 – Solution

Figure

0 0.5 1 1.5 2 2.5 3 3.5 40

0.5

1

1.5

2Railroad Bumper Response

Positio

n,

m

0 0.5 1 1.5 2 2.5 3 3.5 4-5

0

5

10

15

20

Speed,

m/s

Time, s

Page 13: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 13 of 21 Matlab Sheet 3 – Solution

10. The curvilinear motion of a particle is defined by the following parametric

equations:

𝑥 = 52𝑡 − 9𝑡2 m and 𝑦 = 125𝑡 − 5𝑡2m

The velocity of the particle is given by

𝑣 = √𝑣𝑥2 + 𝑣𝑦

2

, where 𝑣𝑥 =𝑑𝑥

𝑑𝑡 and 𝑣𝑦 =

𝑑𝑦

𝑑𝑡 .

For 0 ≤ t ≤ 5 s make one plot that shows the position of the particle (y versus

x), and a second plot (on the same page) of the velocity of the particle as a

function of time. In addition, by using MATLAB’s min function, determine the

time at which the velocity is the lowest, and the corresponding position of the

particle. Using an asterisk marker, show the position of the particle in the first

plot. For time use a vector with spacing of 0.1 s.

The m-file:

t=0:.1:5;

x=52*t-9*t.^2; y=125-5*t.^2;

vx=52-18*t; vy=-10*t;

v=sqrt(vx.^2+vy.^2);

[vmin indx]=min(v);

tmin=t(indx);

subplot(2,1,1)

plot(x,y,x(indx),y(indx),'*')

title('Particle Dynamics')

xlabel('x(m)')

Page 14: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 14 of 21 Matlab Sheet 3 – Solution

ylabel('y(m)')

text(40,80,['time of min speed = ',...

num2str(tmin,'%.1f'),' s'])

subplot(2,1,2)

plot(t,v)

xlabel('time(s)')

ylabel('speed(m/s)')

Figure

0 10 20 30 40 50 60 70 800

50

100

150Particle Dynamics

x(m)

y(m

)

time of min speed = 2.2 s

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 520

40

60

80

time(s)

speed(m

/s)

Page 15: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 15 of 21 Matlab Sheet 3 – Solution

11. The demand for water during a fire is often the most important factor in the

design of distribution storage tanks and pumps. For communities with

populations less than 200,000, the demand Q (in gallons/min) can be calculated

by:

where P is the population in thousands. Plot the water demand Q as a function

of the population P (in thousands) for 0 ≤ P ≤ 200. Label the axes and provide a

title for the plot.

The m-file:

P=0:200;

Q=1020*sqrt(P).*(1-0.01*sqrt(P));

plot(P,Q)

title('Small Community Fire Fighting Water Needs')

xlabel('Population, Thousands')

ylabel('Water Demand, gal/min')

ylabel('speed(m/s)')

Figure

0 20 40 60 80 100 120 140 160 180 2000

2000

4000

6000

8000

10000

12000

14000Small Community Fire Fighting Water Needs

Population, Thousands

speed(m

/s)

Page 16: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 16 of 21 Matlab Sheet 3 – Solution

12. A In a typical tension test a dog bone shaped specimen is pulled in a machine.

During the test, the force F needed to pull the specimen and the length L of a

gauge section are measured. This data is used for plotting a stress-strain

diagram of the material. Two definitions, engineering and true, exist for stress

and strain. The engineering stress and strain are defined by

𝜎𝑒 =𝐹

𝐴0 and 𝜀𝑒 =

𝐿−𝐿𝑜

𝐿𝑜 where 𝐿𝑜 and 𝐴𝑜 are the initial gauge length and the

initial cross-sectional area of the specimen, respectively. The true stress 𝜎𝑡 and

strain 𝜀𝑡 are defined by 𝜎𝑡 =𝐹

𝐴0

𝐿

𝐿𝑜 and 𝜀𝑡 = ln

𝐿

𝐿𝑜 .

The following are measurements of force and gauge length from a tension test

with an aluminum specimen. The specimen has a round cross section with

radius 6.4 mm (before the test). The initial gauge length is 𝐿𝑜=25 mm. Use the

data to calculate and generate the engineering and true stress-strain curves,

both on the same plot. Label the axes and use a legend to identify the curves.

Units: When the force is measured in newtons (N) and the area is calculated in

m2, the unit of the stress is pascals (Pa).

The m-file:

L0=.0254; r0=.0064; A0=pi*r0^2;

F=[0 13031 21485 31963 34727 37119 37960 39550 ...

40758 40986 41076 41255 41481 41564];

L=[25.4 25.474 25.515 25.575 25.615 25.693 25.752 25.978

...

26.419 26.502 26.600 26.728 27.130 27.441]/1000;

sigmae=F/A0; ee=(L-L0)/L0;

sigmat=F.*L/(A0*L0); et=log(L/L0);

Page 17: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 17 of 21 Matlab Sheet 3 – Solution

plot(ee,sigmae,et,sigmat,'--')

title('Stress-Strain Definitions')

legend('Engineering','True','location','SouthEast')

xlabel('Strain')

ylabel('Stress, Pa')

Figure

0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.090

0.5

1

1.5

2

2.5

3

3.5x 10

8 Stress-Strain Definitions

Strain

Str

ess,

Pa

Engineering

True

Page 18: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 18 of 21 Matlab Sheet 3 – Solution

13. The shape of a symmetrical four-digit NACA airfoil is described by the equation

where c is the cord length and t is the maximum thickness as a fraction of the

cord length ( tc= maximum thickness). Symmetrical four-digit NACA airfoils

are designated NACA 00XX, where XX is 100t (i.e., NACA 0012 has t=0.12 ). Plot

the shape of a NACA 0020 airfoil with a cord length of 1.5 m.

The m-file:

t=0.2; c=1.5; xc=0:.01:1;

y1=t*c/0.2*(0.2969*sqrt(xc)-0.1260*xc...

-0.3516*xc.^2+0.2843*xc.^3-0.1015*xc.^4);

y2=-y1;

plot(xc*c,y1,xc*c,y2)

axis equal

title('NACA 0020 Airfoil')

xlabel('Width, m')

ylabel('Height, m')

Figure

0 0.5 1 1.5

-0.5

-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

NACA 0020 Airfoil

Width, m

Heig

ht,

m

Page 19: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 19 of 21 Matlab Sheet 3 – Solution

14. The position as a function of time of a squirrel running on a grass field is given

in polar coordinates by:

Plot the trajectory (position) of the squirrel for

0 ≤ t ≤ 20 s.

The m-file:

clc; clear;clf

t=0:.1:20;

r=25+30*(1-exp(sin(0.07*t)));

theta=2*pi*(1-exp(-0.2*t));

polar(theta,r)

title('Squirrel Trajectory (m)')

Figure

10

20

30

30

210

60

240

90

270

120

300

150

330

180 0

Squirrel Trajectory (m)

Page 20: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 20 of 21 Matlab Sheet 3 – Solution

15. Obtain the surface and contour plots for the function

𝑧 = 𝑥2 − 2𝑥𝑦 + 4𝑦2

showing the minimum at 𝑥 = 𝑦 =0.

The m-file:

[X,Y] = meshgrid(-4:0.4:4);

Z = X.^2-2*X.*Y+4*Y.^2;

subplot(2,1,1)

mesh(X,Y,Z)

xlabel('x'),ylabel('y')

zlabel('z'),grid

subplot(2,1,2)

contour(X,Y,Z),xlabel('x'), ylabel('y')

Figure

-4-2

0 24

-4-2

02

40

100

200

xy

z

x

y

-4 -3 -2 -1 0 1 2 3 4-4

-2

0

2

4

Page 21: MATLAB Sheet 3 Solution - Ahmed Nagib

Faculty of Engineering Spring 2017 Mechanical Engineering Department

Dr./ Ahmed Nagib Elmekawy 21 of 21 Matlab Sheet 3 – Solution

16. A square metal plate is heated to 80°C at the corner corresponding to 𝑥 = 𝑦 =1.

The temperature distribution in the plate is described by

𝑇 = 80 𝑒−(𝑥−1)2 𝑒−3(𝑦−1)2

Obtain the surface and contour plots for the temperature. Label each axis.

What is the temperature at the corner corresponding to 𝑥 = 𝑦 =0?

The m-file:

[X,Y] = meshgrid(0:0.1:1);

T = 80*exp(-(X-1).^2).*exp(-3*(Y-1).^2);

subplot(2,1,1)

mesh(X,Y,T)

xlabel('x'),ylabel('y')

zlabel('T'),grid

subplot(2,1,2)

contour(X,Y,T)

xlabel('x'), ylabel('y')

T(1,1)

In Command Window:

ans =

1.4653

Figure

0 0.2 0.4 0.6 0.8 1

0

0.5

10

50

100

xy

T

x

y

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.2

0.4

0.6

0.8

1