Two vectors x and y are needed to plot a two dimensional graph of a function
y=f(x) where x is domain of the function while y is the range of the function. The graph is plotted using the
1. Plot a graph for the function
y=sin(x) from 0 to 720 degrees.
2. Plot a graph for the function
y=sin(x) from 0 to 720 degrees with step size of 30 degree.
Plotting multiple graphs in the same plot
In the same way, multiple graphs can also be plotted in the single plot. For example, the graphs of
cos(x) from 0 degree to 1080 degrees can be plotted as,
In the first graph, the domain x is on the x-axis and the range of the function
sin(x) denoted by y is on the y-axis. And in the second graph the domain x is again on the x-axis and the range of the function
cos(x) denoted by z is on the y-axis.
The hold on and hold off commands
Multiple graphs can also be plotted using hold on and hold off commands. The first graph is plotted using the plot command then the hold on command is typed. After this, other graphs are plotted.The hold on command allows other graphs to be plotted in the same Figure Window. And in the end, hold off command is typed that stops this process and returns Matlab to the default mode where the previous graph is updated with the new graph.
In the next example, we plot three graphs on the same plot where y is a function, and y1 and y2 are its first and second derivatives respectively. The domain of the functions [-4,4] with step size of 0.01. Consider the function,
>> hold on
>> hold off