Plotting 2D

How do we draw a basic line graph?

We need a table of x, and y values to graph the line graph. Consider the example of graphing y=sin(x) for 2Pi range.

>> x=0:0.1:(4*pi);
>> y=sin(x);
>> plot(x, y)

First x vaules are generated, from 0 to 4*pi with interval of 0.1.
Then, the y values are generated using a built in Matlab function.
Then, the graphy is ploted.



How can I plot two or more graphs on the same graph, so that I can compare graphs?

You can accomplish that as follows:

Note that you will want to differentiate the second graph. The third parameter is describing how to differentiate the second graph. Use Matlab help to obtain other options.

>>z=cos(x);
>>plot(x, y), hold on
>>plot(x, z, 'ro')