Matlab multiple curves on same graph

In summary, the person is trying to plot multiple curves in Matlab using the function y=x^i, where i goes from 1 to 10. They attempted to do so by creating a for loop and using the plot function, but it only plots the last case (when i=10). They are seeking help to plot all 10 curves on the same graph. A solution would be to modify the code by creating a matrix for y and using the hold on and hold off functions in the plot command.
  • #1
sara_87
763
0

Homework Statement



Hi all,
I am trying to plot multiple curves in matlab.
If i have the function:
y=x^i

where i goes from 1 to 10.
I want to plot the function y for each i.

Homework Equations





The Attempt at a Solution



x=[1 2 3 4 5 6 7 8 9 10]
for i =1:10
y = x.^i
end
plot(x,y)

this returns 10 sets of the answer y. I want to plot these 10 curves on the same graph.
the above code only plots x against y for the last case (when i=10)

any help would be very much appreciated.
thank you
 
Physics news on Phys.org
  • #2
sara_87 said:

Homework Statement



Hi all,
I am trying to plot multiple curves in matlab.
If i have the function:
y=x^i

where i goes from 1 to 10.
I want to plot the function y for each i.

Homework Equations


The Attempt at a Solution



x=[1 2 3 4 5 6 7 8 9 10]
for i =1:10
y = x.^i
end
plot(x,y)

this returns 10 sets of the answer y. I want to plot these 10 curves on the same graph.
the above code only plots x against y for the last case (when i=10)

any help would be very much appreciated.
thank you

You can change your code to:

x=[1 2 3 4 5 6 7 8 9 10]
for i =1:10
y(i,:) = x.^i
end
plot(x,y(1,:))
hold on
for i = 2:10
plot(x,y(i,:))
end
hold off
 

1. How do I plot multiple curves on the same graph in Matlab?

To plot multiple curves on the same graph in Matlab, you can use the "hold on" command after each plot function. This will prevent the previous plot from being erased and allow you to add additional curves to the same graph.

2. Can I customize the colors and styles of the curves on the same graph in Matlab?

Yes, you can use the "Color" and "LineStyle" parameters in the plot function to customize the colors and styles of your curves. Additionally, you can use the "legend" function to label each curve and specify its color and style.

3. How can I add a title and labels to my graph with multiple curves in Matlab?

To add a title and labels to your graph, you can use the "title", "xlabel", and "ylabel" functions. These functions allow you to specify the text and font properties for your title and labels.

4. Is it possible to save a graph with multiple curves in Matlab?

Yes, you can save your graph as an image file by using the "saveas" function. You can specify the file format and resolution in the function, and it will save the graph with all the curves and labels.

5. Can I plot multiple curves with different axes on the same graph in Matlab?

Yes, you can use the "yyaxis" function to create a second y-axis and plot a curve with a different scale on the same graph. This allows for comparison between two different data sets with different units or ranges.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • Engineering and Comp Sci Homework Help
Replies
7
Views
886
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
854
  • Engineering and Comp Sci Homework Help
Replies
1
Views
939
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
  • Calculus and Beyond Homework Help
Replies
10
Views
986
Back
Top