Plotting Multiple Graphs in MATLAB

In summary, to plot multiple curves on the same graph in Matlab, use the "hold on" command and multiple plot statements. Simply change the variable values and use the same plot statement to add them to the same graph. An example of this is shown with two curves, y1 and y2, being plotted in different colors on the same graph using the "hold on" command.
  • #1
adnan jahan
96
0
Dear Fellows,

I need to plot a graph in matlab, and stuck in obtaining multi plot from it for different constant values... following example will explain my situation.


x=1;
y=3;
c=x^2+4y+9z
z=linspace(0,1.2);
plot(z,c,'r')
in this I want to plot for y=3,y=4,y=5 and y=0 all curves in one graph.
 
Physics news on Phys.org
  • #2
Use the "hold on;" command and multiple plot statements.
 
  • #3
For different plot I need to past the whole program and change the only variable(y) and
plot(x,y,'r')
hold on
plot(x,y1,'r--')
hold on
plot(x,y2,'b')
Is this what you are saying?

can you please give small example so that I can understand clearly...
 
  • #4
small example,

x = 0:0.1:10;
y1 = x^2 + 2x + 1;
y2 = x^2 + 4x + 1;

so you have two curves, y1 and y2.

plot(x,y1,'r');
hold on
plot(x,y2,'b');

this will plot y2 on the same axis as y1. you only need to type hold on once; after you type it, all further curves are put on the same plot.
 
  • #5


Hello,

Thank you for reaching out for assistance with plotting multiple graphs in MATLAB. I understand your situation and I will be happy to help you with this.

To plot multiple graphs in MATLAB, you can use the "hold on" command after your initial plot command. This will allow you to plot multiple curves on the same graph. Additionally, you can use the "legend" command to label each curve with the corresponding value of y.

Here is an example code that you can use for your situation:

x = 1;
y_values = [0, 3, 4, 5]; % array of y values
z = linspace(0,1.2); % create array of z values

hold on % use this command to plot multiple curves on the same graph

for i = 1:length(y_values) % loop through each y value
c = x^2 + 4*y_values(i) + 9*z; % calculate c for each y value
plot(z, c) % plot the curve for each y value
end

legend('y = 0', 'y = 3', 'y = 4', 'y = 5') % label each curve with corresponding y value

I hope this helps you plot your desired graph. Let me know if you have any further questions or need any additional assistance. Keep up the good work in your scientific endeavors!

Best,
 

FAQ: Plotting Multiple Graphs in MATLAB

1. How do I plot multiple graphs in MATLAB?

To plot multiple graphs in MATLAB, you can use the plot function multiple times, each time specifying the data and formatting for the desired graph. Alternatively, you can use the hold function to hold the current plot and add new plots to it. You can also use the subplot function to create a grid of subplots and plot different graphs on each subplot.

2. Can I customize the appearance of each graph when plotting multiple graphs in MATLAB?

Yes, you can customize the appearance of each graph by specifying different formatting options for each plot function call. You can also use the legend function to add a legend to each graph and label them accordingly.

3. How do I save multiple graphs in MATLAB?

To save multiple graphs in MATLAB, you can use the print function. This function allows you to save the current figure as an image file, such as a PNG or JPEG. You can also use the savefig function to save the figure as a MATLAB figure file.

4. Can I plot different types of graphs together in MATLAB?

Yes, you can plot different types of graphs together in MATLAB. For example, you can plot a line graph and a scatter plot on the same figure by using the plot and scatter functions, respectively. You can also use the bar function to create bar graphs alongside other types of graphs.

5. How do I create a figure with multiple graphs in MATLAB?

To create a figure with multiple graphs in MATLAB, you can use the figure function to create a new figure window. Then, you can use the various plotting functions, such as plot and scatter, to add graphs to the figure. You can also use the subplot function to create a grid of subplots within the figure.

Similar threads

Replies
11
Views
3K
Replies
2
Views
3K
Replies
1
Views
4K
Replies
4
Views
1K
Replies
1
Views
1K
Replies
1
Views
746
Replies
1
Views
3K
Back
Top