Presentation is loading. Please wait.

Presentation is loading. Please wait.

MatLab – Palm Chapter 5 2-D & 3-D Plots

Similar presentations


Presentation on theme: "MatLab – Palm Chapter 5 2-D & 3-D Plots"— Presentation transcript:

1 MatLab – Palm Chapter 5 2-D & 3-D Plots
Class 13.1 Palm Chapter: & 5.8 9/20/2018 ENGR 111A Fall 2004

2 RAT 13.1 As in INDIVIDUAL you have 2 minutes to answer the following question. What is the result of the following subplot and plot commands: x = [0:5]; y = x.^2; subplot(2,2,4) plot(x,y) Answer: Creates a plot of y vs. x on a figure window with four “panes”. This plot would be in the lower, right pane of the figure window. 9/20/2018 ENGR 111A Fall 2004

3 Solution 9/20/2018 ENGR 111A Fall 2004

4 Learning Objectives for Today’s Class
Students should be able to: Use the 2-D (X-Y) Plotting Functions. Make Subplots and Overlay Plots. Use Special Plot Functions. Use the Plot Editor. Use the 3-D (X-Y-Z) Plot Functions. 9/20/2018 ENGR 111A Fall 2004

5 Good Plotting Practices
Each axis must be labeled with the name of quantity plotted and units Axes should have tick marks at regular, convenient intervals Label multiple lines or include legend Use a title if needed Plot each measured data point with a symbol Use lines between data points to improve readability, but with caution Do not plot points if the curve is generated from a continuous function All fonts must be 8pt or greater at the final printed size GOOD NEWS: Matlab does most of this automatically! 9/20/2018 ENGR 111A Fall 2004

6 Example Plots Matlab Default Mixed fonts Plot markers on curve Smaller
fonts, but uniform size 9/20/2018 ENGR 111A Fall 2004

7 Important Plot Commands
plot(xdata,ydata,’csl’,’prop1’,val1,…) grid on; grid off title(‘Put title here’) xlabel(‘Use ^ for superscripts’) ylabel(‘Use _ for subscripts’) legend(‘Line 1’, ‘Line 2’, …) axis – lots of variants. axis square, axis equal, axis auto axis([xmin, xmax, ymin, ymax]) See online HELP 9/20/2018 ENGR 111A Fall 2004

8 In-Class Exercise 13.1-1 (10-min)
Individually solve T5.1-1 A. First, using plot() commands. B. Using fplot() (see page 266). Remember to label axes, pay attention to fonts, etc. Experiment with various axis and grid commands. Also try semilogy(), semilogx(), and loglog()instead of plot() 9/20/2018 ENGR 111A Fall 2004

9 Solution using plot() x = 0:0.1:35; y = 0.4*sqrt(1.8*x);
plot(x, y, ‘r-’) axis([ ]) title(‘Rocket trajectory’) xlabel(‘Distance [miles]’) ylabel(‘Height [miles]’) grid on 9/20/2018 ENGR 111A Fall 2004

10 Solution 9/20/2018 ENGR 111A Fall 2004

11 Solution using fplot()
f = ‘0.4 * sqrt(1.8*x)’ fplot(f, [0 35]) axis([ ]) title(‘Rocket trajectory’) xlabel(‘Distance [miles]’) ylabel(‘Height [miles]’) grid on 9/20/2018 ENGR 111A Fall 2004

12 5.2 Subplots and Overlay Plots (p. 271)
subplot(rows, cols, plot_num) hold on; hold off text(xpos, ypos, ‘text label’) print fname.ps % creates % postscript file Also use edit-copy figure Beware that what you see is not necessarily what you get 9/20/2018 ENGR 111A Fall 2004

13 In-Class Exercise 13.1-2 (10-min)
Individually work T5.2-2 on p. 280 Pay attention to the section Hints for Improving Plots on p 9/20/2018 ENGR 111A Fall 2004

14 Solution x = [0 1 2 3 4 5]; y1 = [11 13 8 7 5 9]; y2 = [2 4 5 3 2 4];
plot(x, y1, 'ko-') hold on plot(x, y2, 'kd--') legend('Data set 1', 'Data set 2') xlabel('Independent variable [--]') ylabel('Dependent variable [--]') 9/20/2018 ENGR 111A Fall 2004

15 Solution, continued 9/20/2018 ENGR 111A Fall 2004

16 5.3 Special Plot Types (p. 282) loglog(x, y, ‘csl’)
semilogx(x, y, ‘csl’) semilogy(x, y, ‘csl’) plotyy(x1, y1, ‘csl’, x2, y2, ‘csl’) 9/20/2018 ENGR 111A Fall 2004

17 Matlab Handles Handle: an index that relates to an object so that its properties can be changed. h = plot(x,y) set(h) will list all the things that can be set get(h) will list all current settings set(h,‘Property’, value) changes settings 9/20/2018 ENGR 111A Fall 2004

18 Automatic handles Just like ans stores answers in a default variable, figure handles are stored automatically. gca “get current axes” : handle to the current set of axes. gcf “get current figure” : handle to the current figure. 9/20/2018 ENGR 111A Fall 2004

19 Labeling plotyy() axes
ax = plotyy(x1, y1, x2, y2) axes(ax(1)) ylabel(‘y1 label’) axes(ax(2)) ylabel(‘y2 label’) 9/20/2018 ENGR 111A Fall 2004

20 In-Class Exercise 13.1-3 (10-min)
Individually solve T5.3-1 on p. 291 You may learn how to use the Plot Editor (section 5.4) on your own to edit your plots. 9/20/2018 ENGR 111A Fall 2004

21 Solution x = 0:0.01:1.5; yp = 2*x.^(-0.5); ye = 10.^(1-x);
subplot(2,2,1) plot(x, yp, 'k-', x, ye, 'k--') axis([ ]) xlabel('x') ylabel('y') text(0.1, 2, 'Power') text(0.3, 7, 'Exponential') set(gca,'XTick',[0.5:0.5:1.5]) subplot(2,2,2) semilogy(x, yp, 'k-', x, ye, 'k--') axis([ ]) xlabel('x') ylabel('y') text(0.4, 0.6, 'Exponential') text(0.8, 3, 'Power') set(gca,'XTick',[0.5:0.5:1.5]) set(gca,'YTick',[1 10]) subplot(2,2,3) loglog(x, yp, 'k-', x, ye, 'k--') axis([ ]) text(0.05, 2.5, 'Power') text(0.13, 10, 'Exponential') set(gca,'XTick',[ ]) 9/20/2018 ENGR 111A Fall 2004

22 Solution, continued 9/20/2018 ENGR 111A Fall 2004

23 5.4 The Plot Editor (p. 292) Sometimes it may be easier to work on plots using the plot function, etc. The Plot Editor is a good way to move text around, alter plot scales, add tick marks, etc. Rotating plots is VERY tricky. Read through pages and practice the various features. 9/20/2018 ENGR 111A Fall 2004

24 5.4 The Plot Editor (contd.)
Practice copying a figure (use one that you have just created) and paste it in a Word file. Use the Copy Figure option in the Edit menu on the figure window. 9/20/2018 ENGR 111A Fall 2004

25 5.8 3-D Plots (p. 334) 3-D plots are a lot like 2-D plots; just add the z-axis parameters. There are three kinds of 3-D plots: Three Dimensional Line Plots (p. 334). Surface Mesh Plots (p. 335). Contour Plots (p. 337). 3-D plot functions are in Table (p. 338). 9/20/2018 ENGR 111A Fall 2004

26 Surface plots For x = xmin : dx : xmax And y = ymin : dy : ymax
Use [x, y] = meshgrid(x, y) to generate a grid to calculate z on Then use, for example: z = x.^2 + y.^2 mesh(x, y, z) % or surf(x, y, z) 9/20/2018 ENGR 111A Fall 2004

27 Contour plots To label contours, use clabel: x = xmin : dx : xmax y = ymin : dy : ymax [x, y] = meshgrid(x, y) z = ??? [cs, h] = contour(x, y, z) clabel(cs, h) 9/20/2018 ENGR 111A Fall 2004

28 In-Class Exercise 13.1-4 (10-min)
Finally, work T5.8-1 on p Plot the data for x & y values from -5 to 5 in 0.5 increments and the z values from -20 to Be sure to label the contours on the contour plot. 9/20/2018 ENGR 111A Fall 2004

29 Solution [x, y] = meshgrid(-5:0.5:5) z = (x-2).^2 + 2*x.*y + y.^2;
subplot(2,2,1) surf(x,y,z) xlabel('x') ylabel('y') zlabel('z') axis([ ]) subplot(2,2,2) [cs,h] = contour(x,y,z) clabel(cs,h) 9/20/2018 ENGR 111A Fall 2004

30 Solution, continued 9/20/2018 ENGR 111A Fall 2004

31 Assignment 13.1 Individual assignment.
Palm MatLab – Chapter 5 (starting on p. 344): # 19, 25, 50. Due: In one week. Reading Assignment – Sections 5.5 to 5.7. 9/20/2018 ENGR 111A Fall 2004


Download ppt "MatLab – Palm Chapter 5 2-D & 3-D Plots"

Similar presentations


Ads by Google