multiple line in 1 plot in matlab

To plot multiple lines in one plot in MATLAB, you can use the hold on command to enable the hold state of the current figure before plotting each line. Here's an example:

main.m
x = linspace(0, 2*pi, 100);  % Generate x values

% Generate y values for multiple lines
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);

% Plot the lines
plot(x, y1)
hold on
plot(x, y2)
plot(x, y3)

% Add labels and title
xlabel('x')
ylabel('y')
title('Multiple lines plot')

% Optional: Add a legend
legend('sin(x)', 'cos(x)', 'tan(x)')
330 chars
21 lines

This code will plot three lines (sin(x), cos(x), and tan(x)) on the same plot. The hold on command is used to keep the previous plot and allow subsequent plots to be overlaid on it. You can customize the plot by adding labels, titles, and legends as needed.

related categories

gistlibby LogSnag