Matlab multiple curves on same graph

AI Thread Summary
To plot multiple curves in MATLAB for the function y=x^i where i ranges from 1 to 10, the initial code only plots the last curve due to overwriting the variable y. To resolve this, the code should store each curve in a matrix by using y(i,:) = x.^i within the loop. After calculating all curves, the plotting can be done by first plotting the first curve and then using a loop to plot the remaining curves while holding the graph. This method ensures that all ten curves appear on the same graph. Properly managing the data structure allows for effective visualization of multiple functions in MATLAB.
sara_87
Messages
748
Reaction score
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
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
 

Similar threads

Replies
1
Views
2K
Replies
10
Views
2K
Replies
7
Views
1K
Replies
1
Views
2K
Replies
6
Views
2K
Replies
1
Views
1K
Back
Top