Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session III Plotting in MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU MATLAB Tutorials.

Similar presentations


Presentation on theme: "Session III Plotting in MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU MATLAB Tutorials."— Presentation transcript:

1 Session III Plotting in MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU MATLAB Tutorials

2 Last Session….  MATLAB programming environment  Creating an M-file  Writing programs in M-file  Control Structures in MATLAB  Condition Loops…  Examples, examples, examples….. Department of Mechanical Engineering, LSU Session III

3 Session III Outline…. Plotting in MATLAB  2-D Graphics  3-D Graphics  Mesh, surface and contour plots in 3-D plots  Exercises in Graphics Department of Mechanical Engineering, LSU Session III

4 2D Graphics….  x=linspace(0,2*pi,40);  linspace Linearly spaced vector  linspace(x1, x2, N) generates N points  between x1 and x2  It is equivalent to x=(0:2*pi/40:2*pi)  y=sin(x)  plot(x,y) Department of Mechanical Engineering, LSU Session III

5 In MATLAB…. Department of Mechanical Engineering, LSU Session III

6 Adding a plot on the same plot….  x=linspace(0,2*pi,40);  y=sin(x);  z=cos(x);  plot(x,y,x,z) Department of Mechanical Engineering, LSU Session III

7 In MATLAB…. Department of Mechanical Engineering, LSU Session III

8 Changing Linestyles, Markers, and Colors….  x=linspace(0,2*pi,40);  y=sin(x);  z=cos(x);  plot(x,y,'gx',x,z,'ro') Department of Mechanical Engineering, LSU Session III

9 In MATLAB…. Department of Mechanical Engineering, LSU Session III

10 Linestyles, Markers, and Colors…. Symbol ColorSymbolLinestyle yyellow.Point mmagentaocircle ccyanxx-mark rred+plus ggreen*star bblue-solid line wwhite:dotted line kblack-.dash-dot line --dash-dash line Department of Mechanical Engineering, LSU Session III

11 Adding grids and Labels…. x=linspace(0,2*pi,40); y=sin(x); z=cos(x); plot(x,y,x,z) gridxlabel('Variable X') ylabel('Variable Y and Z') title('Sine and Cosine') Department of Mechanical Engineering, LSU Session III

12

13 Adding Text to the plot…. text(2.5,0.7,'sin(x)') %Adding text to the specific %location gtext('cos(x)') %Adding text to the location %you select with a mouse click Department of Mechanical Engineering, LSU Session III

14 In MATLAB…. Department of Mechanical Engineering, LSU Session III

15 Adding Legend…. legend('sin','cos') %This command line adds legend %to the plot. The order of %‘string1’,’string2’ should be %the same as it appears in the %plot(x,y,x,z) command line Department of Mechanical Engineering, LSU Session III

16 In MATLAB…. Department of Mechanical Engineering, LSU Session III

17 Using hold command…. x=linspace(0,2*pi,40); y=sin(x); plot(x,y) hold on grid z=cos(x); plot(x,z) hold off Department of Mechanical Engineering, LSU Session III

18 Other useful commands…. loglog: just as plot gives linear coordinate system, loglog gives log-log scale system semilogx: semilog scale plot, x-axis logarithmic semilogy: semilog scale plot, y-axis logarithmic axis([xmin xmax ymin ymax]): set the maximum and minimum values of the axes using values given in the row vector Department of Mechanical Engineering, LSU Session III

19 Using subplot command…. x=linspace(0,2*pi,40); y=sin(x); z=cos(x); a=sin(2*x); b=sin(4*x); subplot(2,2,1) plot(x,y),axis([0 2*pi -1 1]),title('sin(x)') subplot(2,2,2) plot(x,z),axis([0 2*pi -1 1]),title('cos(x)') subplot(2,2,3) plot(x,a),axis([0 2*pi -1 1]),title('sin(2x)') subplot(2,2,4) plot(x,b),axis([0 2*pi -1 1]),title('sin(4x)') Department of Mechanical Engineering, LSU Session III

20 In MATLAB…. Department of Mechanical Engineering, LSU Session III

21 3D Graphics…. The plot command from the 2-D graphics can be extended into 3-D graphics. For example, command plot3 works the same way as plot except plot3 requires three arguments. t=0:pi/50:10*pi; plot3(sin(t),cos(t),t); Department of Mechanical Engineering, LSU Session III

22 In MATLAB…. Department of Mechanical Engineering, LSU Session III

23 Mesh, Surface and Contour plots…. x=-7.5:0.5:7.5; y=x; [X,Y]=meshgrid(x,y); R=sqrt(X.^2+Y.^2)+eps; Z=sin(R)./R; mesh(X,Y,Z) Then change the last line to surf(X,Y,Z) and contour3(X,Y,Z,20)respectively Department of Mechanical Engineering, LSU Session III

24 In MATLAB…. Department of Mechanical Engineering, LSU Session III

25 In MATLAB…. Department of Mechanical Engineering, LSU Session III

26 In MATLAB…. Department of Mechanical Engineering, LSU Session III

27 Other 3D plotting features…. x=-7.5:0.5:7.5; y=x; [X,Y]=meshgrid(x,y); R=sqrt(X.^2+Y.^2)+eps; Z=sin(R)./R; colormap(gray) surfl(X,Y,Z) shading interp axis off Department of Mechanical Engineering, LSU Session III

28 In MATLAB…. Department of Mechanical Engineering, LSU Session III

29 Exercise…. The function sphere2 on the next slide is an example to draw a sphere at given position The surface points of the sphere is generated by x=x0+cos(phi)cos(theta) y=y0+cos(phi)sin(theta) z=z0+sin(phi) axis equal sets the aspect ratio so that equal tick mark increments on the x-,y- and z-axis are equal in size Department of Mechanical Engineering, LSU Session III

30 function sphere2(x0,y0,z0) % Draw a sphere at position (x0,y0,z0) theta=pi*(-1:0.02:1); phi=(pi/2)*(-1:0.02:1)'; x=x0+cos(phi)*cos(theta); y=y0+cos(phi)*sin(theta); z=z0+sin(phi)*ones(size(theta)); surfl(x,y,z); colormap(gray) shading interp axis equal axis ([-5 5 -5 5 -5 5]) axis off MATLAB Code…. Department of Mechanical Engineering, LSU Session III

31 Plot…. Department of Mechanical Engineering, LSU Session III

32 Exercise 2 We have two sets of measurement results…. Time(sec)Output(mv) Case 1Case 2 13.65.4 23.65.3 33.75.5 43.55.6 53.75.4 63.85.7 73.45.6 The results are saved in a text format file named result.dat Write a Matlab program to read the results and create a 2-D plot as shown in next slide Department of Mechanical Engineering, LSU Session III

33 Use Notepad to input the table in the format Save it as result.dat Matlab Code… fn=fopen('result.dat','r'); a=fscanf(fn,'%d%f%f\n',[3,7]); fclose(fn); x=a(1,:); y=a(2,:); z=a(3,:); Writing the code…. Department of Mechanical Engineering, LSU Session III

34 Plot output…. Department of Mechanical Engineering, LSU Session III

35 Recap…. Plotting in MATLAB  2-D Graphics  3-D Graphics  Mesh, surface and contour plots in 3-D plots  Exercises in Graphics

36 Next Session…. Mathematical Applications using MATLAB….  Using fplot function  Minimization  Zero finding  Curve fitting  Interpolation  Integration

37 Thank You


Download ppt "Session III Plotting in MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU MATLAB Tutorials."

Similar presentations


Ads by Google