Plotting with Matlab

All Matlab variables must have numerical values:

>> x = -10:.1:10;

The basic plot command:

>> plot(sin(x))
sin plot

Note that the horizontal axis is marked according to the index, not the value of x. Fix this as follows:

>> plot( x, sin(x) )
sin with correct x-axis

This is the same thing as plotting "parametric curves."

We can plot the "inverse relationship" (for example, the squaring function and +/- square root) easily:
>> plot( x, x.^2 ) >> plot( x.^2, x )
x squared
square root of 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 )
flat spiral
helix

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 )
multiple plot

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.

multiple plot with constrained axis

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')
semilog plot

Here are other ways to graph multiple curves, using matrices (plotted by columns) and using "hold."

plot with hold
plot with 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.

meshgrid plot