3 views (last 30 days)

I have a project where the mass of an object is 1,2,3,5,and 7 and the value of m will be used to plot a graph. I cannot figure out how to make the code run where the first curve on the graph is when m=1 and the second curve on the graph shows where m=2....up until the last curve where m=7. In need all 5 curves to be plotted. I tried using the for loop but i dont know if that is correct. there is no error code, but only the m=7 curve is showing up.

This is what i have so far for the array and loop

array=[1 2 3 5 7];

for i=1:5

m = array(i);

disp (m)

end

Rik
on 5 Dec 2019

I guess boldly:

This is probably your current code:

figure(1),clf(1)%don't forget clf during debuggin

array=[1 2 3 5 7];

x=linspace(0,3*pi,1000);

for i=1:5

m=array(i);

y=m*sin(x*m);%generate random y

plot(x,y)

end

In which case the issue is that plot replaces the previous contents of your axes, just as the NextPlot property tells it to. You can easily modify this by including hold on:

figure(1),clf(1)%don't forget clf during debuggin

array=[1 2 3 5 7];

x=linspace(0,3*pi,1000);

for i=1:5

m=array(i);

y=m*sin(x*m);%generate random y

hold on

plot(x,y)

hold off

end

Rik
on 5 Dec 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495109-how-to-switch-the-values-of-a-variable#comment_774958

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495109-how-to-switch-the-values-of-a-variable#comment_774958

Sign in to comment.