plot two different functions in matlab in matlab

To plot two different functions in MATLAB, you can use the plot() function multiple times with different input arguments for the X and Y values of each function. Here's an example code that plots two functions sin(x) and cos(x) in the range of -pi to pi:

main.m
x = -pi:0.1:pi;  % Create a range of x values
y1 = sin(x);      % Calculate y values for sin(x)
y2 = cos(x);      % Calculate y values for cos(x)

% Plot both functions on the same graph
plot(x, y1, 'r-', x, y2, 'b--')
legend('sin(x)', 'cos(x)')  % Add a legend to the graph
xlabel('x')  % Add a label to the x-axis
ylabel('y')  % Add a label to the y-axis
title('Plot of sin(x) and cos(x)')  % Add a title to the graph
420 chars
11 lines

This code will create a graph that looks like this:

plot of sin(x) and cos(x)

The first plot() call plots the sin(x) function with a solid red line, while the second plot() call plots the cos(x) function with a dashed blue line. The legend() function adds a legend to the graph so that you can differentiate between the two functions, and the xlabel(), ylabel(), and title() functions add labels and a title to the graph to make it easy to understand.

gistlibby LogSnag