MATLAB Multiple Plots: Subplot Guide
In MATLAB, when plotting multiple graphs using the plot function, you can use the subplot function to divide the graph into multiple subplots, and then plot different data in each subplot.
Here is an example code demonstrating how to plot multiple graphs in MATLAB.
% 创建数据
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
% 创建第一个子图
subplot(2, 1, 1);
plot(x, y1);
title('Sine Function');
xlabel('x');
ylabel('y');
% 创建第二个子图
subplot(2, 1, 2);
plot(x, y2);
title('Cosine Function');
xlabel('x');
ylabel('y');
In the example above, we first created two sets of data, y1 and y2, representing the sine and cosine functions, respectively. Subsequently, we used the subplot function to divide the plot into two rows and one column for each data set. Finally, we added titles and axis labels using the title, xlabel, and ylabel functions.
Running the code above will allow you to plot two graphs in MATLAB, each showing the sine and cosine functions.