26. USING THE PLOT COMMAND TO PLOT MULTIPLE CURVES The command: plot(x,y,u,v) plots y versus x and v versus u on the same plot. By default, the computer makes the curves in different colors. The curves can have a specific style by using: plot(x,y,’color_linestyle_marker’,u,v, ’color_linestyle_marker’) More curves can be added.
27.
28.
29.
30. x=[0:0.1:10]; y=500-0.5*9.81*x.^2; xd=[0:10]; yd=[500 495 490 470 430 390 340 290 220 145 60]; plot(x,y,'g',xd,yd,'mo--') xlabel('TIME (s)') ylabel('HEIGHT (m)') title('Height as a Function of Time') legend('Model','Data') axis([0 11 0 600]) text(1,100,'Comparison between theory and experiment') EXAMPLE OF A FORMATTED PLOT WITH TWO CURVES Below is the script file of the falling object plot in lecture 4. Plot y versus x in green, and plot yd versus xd in magenta, circle markers, and dashed line. Creating a vector of time (data) xd Calculated height y for each x Creating a vector of time x Creating a vector of height (data) yd
32. Subplots For example, the command: subplot(3,2, p ) Creates 6 plots arranged in 3 rows and 2 columns. subplot(3,2,1) subplot(3,2,2) subplot(3,2,3) subplot(3,2,5) subplot(3,2,4) subplot(3,2,6)
33. Example: Plot the 3-D surface described by the equation, in the region in the x-y plane from x = -5 to 5 and y = -4 to 4. Step 1) solve the equation for z, (continued on the next slide) 3-D Plotting
34. Step 2) create two vectors x and y . The values of x and y will determine the region in the xy plane over which the surface is plotted. >> x = linspace(-3,3,7); >> y = linspace(-2,2,5); Step 3) Use the meshgrid function to create two matrices, X and Y . We will use these to compute Z . >> [X , Y] = meshgrid(x,y)
36. 5- Step 4) compute Z by using the X and Y created by meshgrid , >> R = sqrt(X.^2+Y.^2); >> Z = cos(X).*cos(Y).*exp(-R./4) Z =
37. 5- Step 4) compute Z by using the X and Y created by meshgrid , >> R = sqrt(X.^2+Y.^2); >> Z = cos(X).*cos(Y).*exp(-R./4); Step 5) graph in 3-D by applying the surface function, >> surf(X,Y,Z);