plot more than one variable in matlab

To plot multiple variables in MATLAB, you can use the plot function along with the hold function. Here's an example:

main.m
x = 1:10;
y1 = x;
y2 = x.^2;
y3 = x.^3;

% plot y1
plot(x, y1, '-r')
hold on % enable the "hold" mode
% plot y2
plot(x, y2, '--b')
% plot y3
plot(x, y3, ':g')
hold off % turn off the "hold" mode

% Add title and axis labels
title('Multiple variables plot')
xlabel('X-axis')
ylabel('Y-axis')
291 chars
19 lines

In this example, we define three variables y1, y2, and y3, each representing a different function of x. We then use the plot function to plot each variable against x.

To plot multiple variables on the same figure, we use the hold function to enable the "hold" mode. This allows us to add more plots to the same figure without overwriting the previous ones. Once we're done plotting all the variables, we turn off the "hold" mode by calling hold off.

Finally, we add a title and labels to the axes using the title, xlabel, and ylabel functions.

gistlibby LogSnag