plot two functions in the same graph in matlab

To plot two functions in the same graph in MATLAB, you can use the plot function multiple times within the same figure. Here is an example code snippet that shows how to plot two functions in the same graph:

main.m
% Define the x values
x = -10:0.1:10;

% Define the two functions
y1 = sin(x);
y2 = cos(x);

% Create a figure to hold the plot
figure;

% Plot the first function
plot(x, y1, 'b', 'LineWidth', 2);  % 'b' specifies blue color

% Hold on to the current figure to allow for multiple plots
hold on;

% Plot the second function
plot(x, y2, 'r', 'LineWidth', 2);  % 'r' specifies red color

% Add grid, labels, and legend
grid on;
xlabel('x-axis');
ylabel('y-axis');
legend('sin(x)', 'cos(x)');

% Title for the plot
title('Plot of sin(x) and cos(x) Functions');

% Hold off to stop further plots from being added to the same figure
hold off;
637 chars
31 lines

By using the hold on and hold off commands, you can plot multiple functions in the same graph.

related categories

gistlibby LogSnag