When you wish to plot a function you must first choose an independent variable. This variable will form an axis of the plot, more then likely the horizontal or x-axis. The vertical component of the plot is then found by using the function that is being plotted and the points along the axis formed by the independent variable. each point then has a vertical and horizontal component. Finally, to guide the eye a line is usually drawn from one point to the next.
In other plotting programs a lot of this is taken for granted, but in Matlab you nust define plots with a little more care.
First, the independent variable and its range must be defined. At the same time the number of points to be used in the plot can also be declared.
>> x=linspace(0,2*pi,30);
This creates thirty points between 0 and 2pi to be used in a plot. Next, you need to define the function that will be plotted.
>> y=sin(x);
At this point you have an independent variable, a declared
range, the number of points to use in the plot, and the function
to be plotted. The final part is to call the Matlab function
plot
to view the result.
>> plot(x,y);
This will produce a plot of the sine wave from 0 to 2pi.
By adding or reducing the number of points in linspace
the plot can be made to have more or less detail.
You may have noticed that in Matlab the first argument to
plot
is the independent variable while in other
plotting programs the equation came first. Matlab simply puts
the first argument on the x-axis and the second argument on
the y-axis. If you were to call plot
like this
>> plot(y,x);
you would obtain a sin wave that is proceeding upward.
plot
can plot more then one function at a
time, simply be sure to pair cooresponding information together
correctly. An example is shown below.
>> x=linspace(0,2*pi,40);
>> y=sin(x);
>> z=cos(x);
>> plot(x,y,x,z);
This will present a plot of both a sine and cosine curve from 0 to 2pi using 40 points. The order of the arguments could just as well have been changed to this
>> plot(x,y,z,x);
which would have resulted in a sine curve proceeding to the right of the plot and a cosine curve proceeding upward. This is just to show how Matlab first looks at the arguments in pairs and the places the first argument in a pair on the horizontal or x-axis. I don't think a cosine and sine curve going in different directions would be very useful.
plot3
allows you make plots in three dimensions.
plot3
lloks at the arguments passed to it in groups
of three. With plot
above, the sine and cosine
curves were both plotted. Now with plot3
we can use
the sine and cosine curves together to form a single plot.
>> x=linspace(0,2*pi,40);
>> y=sin(x);
>> z=cos(x);
>> plot3(y,z,x);
The example shown above will generate a three dimensional
spiral the uses the cosine and sine of x
. Again the
range of x
had to be declared along with the number
of points to use in the plot.