This document discusses various plotting techniques in MATLAB. It shows how to:
1) Create a basic plot by specifying the x and y variables.
2) Plot multiple curves simultaneously by passing multiple x and y variable pairs to the plot command.
3) Add options like setting axis scales or limits after creating the plot.
This document discusses various plotting techniques in MATLAB. It shows how to:
1) Create a basic plot by specifying the x and y variables.
2) Plot multiple curves simultaneously by passing multiple x and y variable pairs to the plot command.
3) Add options like setting axis scales or limits after creating the plot.
rt.uits.iu.edu/visualization/analytics/math/matlab-plotting.php 1/5 UITS Research Technologies Cyberinfrastructure enabling research and creative activities Plotting with Matlab All Matlab variables must have numerical values: >> x = -10:.1:10; The basic plot command: >> plot(sin(x)) Note that the horizontal axis is marked according to the index, not the value of x. Fix this as follows: >> plot( x, sin(x) ) This is the same thing as plotting "parametric curves." We can plot the "inverse relationship" (for example, the squaring function and +/- square root) easily: INDIANA UNIVERSITY 24.11.2013 Plotting with Matlab rt.uits.iu.edu/visualization/analytics/math/matlab-plotting.php 2/5 >> plot( x, x.^2 ) >> plot( x.^2, x ) or a spiral in two or in three dimensions: >> t = 0:.1:10; plot( t .* cos(t), t .* sin(t) ) >> plot3( t .* cos(t), t .* sin(t), t ) Plot several curves simultaneously with plot(x1, y1, x2, y2, ...): >> plot( x, cos(x), x, 1 - x.^2./2, x, 1 - x.^2./2 + x.^4./24 ) 24.11.2013 Plotting with Matlab rt.uits.iu.edu/visualization/analytics/math/matlab-plotting.php 3/5 Let's add some options [first plot, then options] -- the order of the options can affect the result! We will first make the scales [the "ratio"] equal, then set the plotting window to be -6 < x < 6, -2 < y < 2. Here's another example, a semilog plot. In the graphic we also have introduced another variation, the "plot matrix" -- to learn more, enter the command help subplot >> plot( x, exp(x) ), set(gca,'yscale','log') 24.11.2013 Plotting with Matlab rt.uits.iu.edu/visualization/analytics/math/matlab-plotting.php 4/5 Here are other ways to graph multiple curves, using matrices (plotted by columns) and using "hold." Functions of two variables may be plotted, as well, but some "setup" is required! >> [x y] = meshgrid(-3:.1:3, -3:.1:3); >> z = x.^2 - y.^2; Here are two options for plotting the surface. Look at the help page for details. 24.11.2013 Plotting with Matlab rt.uits.iu.edu/visualization/analytics/math/matlab-plotting.php 5/5