how to switch the values of a variable

조회 수: 6 (최근 30일)
Alaura Mohair
Alaura Mohair 2019년 12월 5일
댓글: Rik 2019년 12월 5일
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
  댓글 수: 1
Rik
Rik 2019년 12월 5일
This code will not result in the issue you describe. Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2019년 12월 5일
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
  댓글 수: 2
Alaura Mohair
Alaura Mohair 2019년 12월 5일
편집: Rik 2019년 12월 5일
Thank you for the help. I think it will work, however, i need to plot a bode diagram. Here is the question:
Plot Bode diagram of the system for m=1, 2, 3, 5, 7 kg load.
This bode diagram includes a transfer function. I will included the rest of my code.
clc;
clear;
M=90; %kg
zeta= 0.07;
k=415000; %N/m
e=0.15; %m
array=[1 2 3 5 7];
for i=1:5
m=array(i);
end
wn=sqrt(k/M);
c=M*(2*zeta*wn);
numG=[(m*e)/M 0 0];
denG=[1 2*zeta*wn (wn)^2];
sysG=tf(numG,denG);
bode(sysG)
hold on
Could you possibly tell me how to use this to create a bode diagram that plots all 5 m values?
Rik
Rik 2019년 12월 5일
You should put the call to bode inside your loop. I lack the toolboxes to test it for you, but it should either work as expected with hold on, or you should be able to extract the x and y data that forms the bode plot and use plot to create your intended result.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by