f(x) Faculty of Engineering Spring 217 Mechanical Engineering Department Matlab Sheet 3 - Solution Plotting in Matlab 1. a. Estimate the roots of the following equation by plotting the equation. x 3 3x 2 + 5x sin ( πx 4 5π 4 ) + 3 = b. Use the estimates found in part a to find the roots more accurately with the fzero function. row clc; clear;clf x = (-2:.1: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 =.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.5 and 1.1, all of these methods give the roots x =.4795, 1.1346, and 3.8318. In Command Window: z = 3.8318 5-5 -1-15 -2-25 -2-1 1 2 3 4 x Dr./ Ahmed Nagib Elmekawy 1 of 21 Matlab Sheet 3 Solution
Force (Newtons) Faculty of Engineering Spring 217 Mechanical Engineering Department 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). A = [,-7,6;5,-4,3;1,-1,9;15,1,;2,2,-1]; plot(a(:,1),a(:,2:3)) xlabel('time (seconds)'), ylabel('force (Newtons)') gtext('column 1'),gtext('Column 2') 1 8 6 Column 2 4 2-2 -4-6 Column 1-8 2 4 6 8 1 12 14 16 18 2 Time (seconds) Dr./ Ahmed Nagib Elmekawy 2 of 21 Matlab Sheet 3 Solution
Percent Error x and sin(x) Error: sin(x) - x Faculty of Engineering Spring 217 Mechanical Engineering Department 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 x 1. For the second, plot the approximation error sin x - x versus x for x 1. For the third, plot the relative error [sin(x) - x]/sin(x) versus x for x 1. How small must x be for the approximation to be accurate within 5 percent? x = :.1: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(1*(sin(x)-x))./sin(x)),xlabel('x (radians)'),... ylabel('percent Error'),grid 1.5 x sin(x).2.15.1.5.5 1 x (radians).5 1 x (radians) 2 15 1 5.5 1 x (radians) Dr./ Ahmed Nagib Elmekawy 3 of 21 Matlab Sheet 3 Solution
Left and Right Sides Faculty of Engineering Spring 217 Mechanical Engineering Department 4. You can use trigonometric identities to simplify the equations that appear in many applications. Confirm the identity tan(2x) = 2 tan x/(1 - tan 2 x) by plotting both the left and the right sides versus x over the range x 2π. x =:.1: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') 1 5-5 -1-15 -2 Left Right -25 1 2 3 4 5 6 7 x (radians) Dr./ Ahmed Nagib Elmekawy 4 of 21 Matlab Sheet 3 Solution
x(t), y(t) Faculty of Engineering Spring 217 Mechanical Engineering Department 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. x(t) = 1 e.5t sin(3t + 2) y(t) = 7 e.4t cos(5t 3) t = :.2:1; x = 1*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)') 1 8 x(t) y(t) 6 4 2-2 -4-6 -8 1 2 3 4 5 6 7 8 9 1 t Dr./ Ahmed Nagib Elmekawy 5 of 21 Matlab Sheet 3 Solution
Displacement y (Inches) Faculty of Engineering Spring 217 Mechanical Engineering Department 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 y(t) = 1 f 1 2 f 2 2 [cos(f 2t) cos(f 1 t)] where y is the displacement in mm and t is the time in seconds. Plot y versus t over the range t 2 for f 1 = 8 rad/sec andf 2 = 1 rad/sec. Be sure to choose enough points to obtain an accurate plot. t = :.2:2; y = (1/(64-1))*(cos(8*t)-cos(t)); plot(t,y) xlabel('t (seconds)') ylabel('displacement y (Inches)').4.3.2.1 -.1 -.2 -.3 -.4 2 4 6 8 1 12 14 16 18 2 t (seconds) Dr./ Ahmed Nagib Elmekawy 6 of 21 Matlab Sheet 3 Solution
Faculty of Engineering Spring 217 Mechanical Engineering Department 7. Oscillations in mechanical structures and electric circuits can often be t τ y(t) = e sin(ωt + φ) 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 τ = 1, ω = π and φ = 2. c. Apply your criterion, and plot y(t) for τ =.1, ω = π and φ = 2. % Part a t = :.2:4; y = exp(-t/1).*sin(pi*t+2); plot(t,y) xlabel('t'),ylabel('y') title('part a') %Part b figure(2) t2 = :.1:.4; y2 = exp(-t2/.1).*sin(8*pi*t2+2); plot(t2,y2) xlabel('t'),ylabel('y') title('part b') Dr./ Ahmed Nagib Elmekawy 7 of 21 Matlab Sheet 3 Solution
y y Faculty of Engineering Spring 217 Mechanical Engineering Department s: 1 Part a.8.6.4.2 -.2 -.4 -.6 -.8-1 5 1 15 2 25 3 35 4 t 1 Part b.8.6.4.2 -.2 -.4.5.1.15.2.25.3.35.4 t Dr./ Ahmed Nagib Elmekawy 8 of 21 Matlab Sheet 3 Solution
Faculty of Engineering Spring 217 Mechanical Engineering Department 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 x(t) of the mass is given by the equation: x(t) = 2f o ω 3 n ω 3 sin (ω n ω 2 t) sin ( ω n + ω 2 where F(t) = F sin ωt, and f = F /m, ω is the frequency of the applied force, and ω n is the natural frequency of the helicopter. When the value of ω is close to the value of ω n, the vibration consists of fast oscillation with slowly changing amplitude called beat. Use f = 12 N/kg, ω n =1 rad/s, and ω=12 rad/s to plot x(t) as a function of t for t 1 s. t) clc; clear;clf f=12; wn=1; w=12; t=:.1:1; x=2*f/(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') Dr./ Ahmed Nagib Elmekawy 9 of 21 Matlab Sheet 3 Solution
Displacement, m Faculty of Engineering Spring 217 Mechanical Engineering Department.4 Helicopter Body Vibrations.3.2.1 -.1 -.2 -.3 -.4 1 2 3 4 5 6 7 8 9 1 Time, s Dr./ Ahmed Nagib Elmekawy 1 of 21 Matlab Sheet 3 Solution
Faculty of Engineering Spring 217 Mechanical Engineering Department 9. A railroad bumper is designed to slow down a rapidly moving railroad car. After a 2, kg railroad car traveling at 2 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 t 4 s. Make two plots on one page. t=:.1: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') Dr./ Ahmed Nagib Elmekawy 11 of 21 Matlab Sheet 3 Solution
Speed, m/s Position, m Faculty of Engineering Spring 217 Mechanical Engineering Department 2 Railroad Bumper Response 1.5 1.5.5 1 1.5 2 2.5 3 3.5 4 2 15 1 5-5.5 1 1.5 2 2.5 3 3.5 4 Time, s Dr./ Ahmed Nagib Elmekawy 12 of 21 Matlab Sheet 3 Solution
Faculty of Engineering Spring 217 Mechanical Engineering Department 1. The curvilinear motion of a particle is defined by the following parametric equations: The velocity of the particle is given by, where v x = dx dt and v y = dy dt. x = 52t 9t 2 m and y = 125t 5t 2 m v = v x 2 + v y 2 For 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.1 s. t=:.1:5; x=52*t-9*t.^2; y=125-5*t.^2; vx=52-18*t; vy=-1*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)') Dr./ Ahmed Nagib Elmekawy 13 of 21 Matlab Sheet 3 Solution
speed(m/s) y(m) Faculty of Engineering Spring 217 Mechanical Engineering Department ylabel('y(m)') text(4,8,['time of min speed = ',... num2str(tmin,'%.1f'),' s']) subplot(2,1,2) plot(t,v) xlabel('time(s)') ylabel('speed(m/s)') 15 Particle Dynamics 1 5 time of min speed = 2.2 s 1 2 3 4 5 6 7 8 x(m) 8 6 4 2.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time(s) Dr./ Ahmed Nagib Elmekawy 14 of 21 Matlab Sheet 3 Solution
speed(m/s) Faculty of Engineering Spring 217 Mechanical Engineering Department 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 2,, 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 P 2. Label the axes and provide a title for the plot. P=:2; Q=12*sqrt(P).*(1-.1*sqrt(P)); plot(p,q) title('small Community Fire Fighting Water Needs') xlabel('population, Thousands') ylabel('water Demand, gal/min') ylabel('speed(m/s)') 14 Small Community Fire Fighting Water Needs 12 1 8 6 4 2 2 4 6 8 1 12 14 16 18 2 Population, Thousands Dr./ Ahmed Nagib Elmekawy 15 of 21 Matlab Sheet 3 Solution
Faculty of Engineering Spring 217 Mechanical Engineering Department 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 σ e = F A and ε e = L L o L o where L o and A o are the initial gauge length and the initial cross-sectional area of the specimen, respectively. The true stress σ t and strain ε t are defined by σ t = F and ε A L t = ln L. o L o L 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 L o =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 m 2, the unit of the stress is pascals (Pa). L=.254; r=.64; A=pi*r^2; F=[ 1331 21485 31963 34727 37119 3796 3955... 4758 4986 4176 41255 41481 41564]; L=[25.4 25.474 25.515 25.575 25.615 25.693 25.752 25.978... 26.419 26.52 26.6 26.728 27.13 27.441]/1; sigmae=f/a; ee=(l-l)/l; sigmat=f.*l/(a*l); et=log(l/l); Dr./ Ahmed Nagib Elmekawy 16 of 21 Matlab Sheet 3 Solution
Stress, Pa Faculty of Engineering Spring 217 Mechanical Engineering Department plot(ee,sigmae,et,sigmat,'--') title('stress-strain Definitions') legend('engineering','true','location','southeast') xlabel('strain') ylabel('stress, Pa') 3.5 x 18 Stress-Strain Definitions 3 2.5 2 Engineering True 1.5 1.5.1.2.3.4.5.6.7.8.9 Strain Dr./ Ahmed Nagib Elmekawy 17 of 21 Matlab Sheet 3 Solution
Height, m Faculty of Engineering Spring 217 Mechanical Engineering Department 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 XX, where XX is 1t (i.e., NACA 12 has t=.12 ). Plot the shape of a NACA 2 airfoil with a cord length of 1.5 m. t=.2; c=1.5; xc=:.1:1; y1=t*c/.2*(.2969*sqrt(xc)-.126*xc... -.3516*xc.^2+.2843*xc.^3-.115*xc.^4); y2=-y1; plot(xc*c,y1,xc*c,y2) axis equal title('naca 2 Airfoil') xlabel('width, m') ylabel('height, m') NACA 2 Airfoil.5.4.3.2.1 -.1 -.2 -.3 -.4 -.5.5 1 1.5 Width, m Dr./ Ahmed Nagib Elmekawy 18 of 21 Matlab Sheet 3 Solution
Faculty of Engineering Spring 217 Mechanical Engineering Department 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 t 2 s. clc; clear;clf t=:.1:2; r=25+3*(1-exp(sin(.7*t))); theta=2*pi*(1-exp(-.2*t)); polar(theta,r) title('squirrel Trajectory (m)') Squirrel Trajectory (m) 9 3 12 6 15 2 1 3 18 21 33 24 27 3 Dr./ Ahmed Nagib Elmekawy 19 of 21 Matlab Sheet 3 Solution
y z Faculty of Engineering Spring 217 Mechanical Engineering Department 15. Obtain the surface and contour plots for the function showing the minimum at x = y =. z = x 2 2xy + 4y 2 [X,Y] = meshgrid(-4:.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') 2 1 4 2 y -2-4 -4-2 x 2 4 4 2-2 -4-4 -3-2 -1 1 2 3 4 x Dr./ Ahmed Nagib Elmekawy 2 of 21 Matlab Sheet 3 Solution
y T Faculty of Engineering Spring 217 Mechanical Engineering Department 16. A square metal plate is heated to 8 C at the corner corresponding to x = y =1. The temperature distribution in the plate is described by T = 8 e (x 1)2 e 3(y 1)2 Obtain the surface and contour plots for the temperature. Label each axis. What is the temperature at the corner corresponding to x = y =? [X,Y] = meshgrid(:.1:1); T = 8*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 1 5 1.5 y.2 x.4.6.8 1 1.8.6.4.2.1.2.3.4.5.6.7.8.9 1 x Dr./ Ahmed Nagib Elmekawy 21 of 21 Matlab Sheet 3 Solution