Matlab multiple curves on same graph

Click For Summary
SUMMARY

The discussion focuses on plotting multiple curves in MATLAB using the function y=x^i for i ranging from 1 to 10. The original code only plots the last curve due to overwriting the variable y in each iteration. The solution provided involves storing each curve in a matrix y and using a loop to plot all curves on the same graph. The corrected code utilizes the 'hold on' command to overlay the plots effectively.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with matrix operations in MATLAB
  • Knowledge of plotting functions in MATLAB
  • Understanding of loops and indexing in MATLAB
NEXT STEPS
  • Explore MATLAB's plotting functions, specifically 'hold on' and 'hold off'
  • Learn about matrix manipulation in MATLAB for storing multiple datasets
  • Investigate advanced plotting techniques in MATLAB, such as legends and labels
  • Study MATLAB's function handles for dynamic plotting
USEFUL FOR

Students, educators, and researchers using MATLAB for data visualization, particularly those needing to plot multiple mathematical functions simultaneously.

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 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K