SOLUTION Define time values t 0110 Define acceleration of gravity g 980 Compute

Solution define time values t 0110 define

This preview shows page 7 - 11 out of 32 pages.

SOLUTION >> % Define time values >> t = 0:1:10; >> % Define acceleration of gravity >> g = 9.80; >> % Compute displacement y >> y = g * t.^2; >> % Plot >> plot(t,y,'b--') >> % Create labels >> grid on >> xlabel('Time [s]') >> ylabel('Distance fallen [m]') >> title('Distance fallen as a function of time on Earth') 0 1 2 3 4 5 6 7 8 9 10 0 100 200 300 400 500 600 700 800 900 1000 Time [s] Distance fallen [m] Distance fallen as a function of time on Earth Hint: You can define the color and the style of a curve when using the plot command. In the example below, ‘b- -‘ stands for blue dashed line. Other possible options are “:” for a dotted line, “-.” for a dash-dotted line, “-“ for a solid line. The colors are designated by the first letter of the desired color (b,r,g,y… k = black). If the style or the color is not specified, MATLAB uses its default settings. See help plot for additional information. Hint: You can zoom in and out, draw arrows, add titles, labels, legends, or write text directly in the figure using the menu bar. A YOUR TURN Repeat the same exercise for Mercury. Planet g Earth 9.80 m/s 2 Jupiter 24.79 m/s 2 Mercury 3.72 m/s 2
Image of page 7
PART 2 Multiple functions can be plotted on the same figure using the hold on and hold off commands. Plot the displacement curve for both the Earth and Jupiter in the same figure and add a legend. SOLUTION >> % Compute displacement for Earth >> g = 9.80; >> t = 0:1:10; >> y = g * t.^2; >> % Plot displacement for Earth >> plot(t,y,'b--') >> grid on >> hold on >> % Both functions are plotted in the same figure >> % Compute displacement for Jupiter >> g = 24.79; >> y = g * t.^2; >> % Plot displacement for Jupiter >> plot(t,y,'r-') >> % Create labels >> title('Distance fallen as a function of time') >> xlabel('Time [s]') >> ylabel('Distance fallen [m]') >> % Add a legend >> legend('Earth','Jupiter') >> % End of the hold command >> hold off 0 1 2 3 4 5 6 7 8 9 10 0 500 1000 1500 2000 2500 Distance fallen as a function of time Time [s] Distance fallen [m] Earth Jupiter YOUR TURN Plot all three curves for the three planets in the same figure. Use different styles for each of the planets and include a legend PART 3 The three curves can also be plotted on the same figure in three different plots using the subplot command. Plot the curves for Earth and Jupiter using subplot . SOLUTION >> % Compute displacement for Earth >> t = 0:1:10;
Image of page 8
>> g = 9.80; >> y = g*t.^2; >> % Plot >> subplot (2,1,1) >> plot (t,y,'b--') >> xlabel ('Time [s]') >> ylabel ('Distance fallen [m]') >> title ('Distance fallen as a function of time on Earth') >> grid on >> % Define displacement for Jupiter >> g=24.79; >> y = g*t.^2; >> % Plot >> subplot(2,1,2) >> plot(t,y,'r-') >> xlabel('Time [s]') >> ylabel('Distance fallen [m]') >> title('Distance fallen as a function of time on Jupiter') >> grid on 0 1 2 3 4 5 6 7 8 9 10 0 200 400 600 800 1000 Time [s] Distance fallen [m] Distance fallen as a function of time on Earth 0 1 2 3 4 5 6 7 8 9 10 0 500 1000 1500 2000 2500 Time [s] Distance fallen [m] Distance fallen as a function of time on Jupiter YOUR TURN Using subplot , create three different displacement plots in the same window corresponding to each of the three planets.
Image of page 9
Programming 7. How to use a script Commands : clear Clear all variables close Close the current figure Open a new script (or .m) file.
Image of page 10
Image of page 11

You've reached the end of your free preview.

Want to read all 32 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes