Changing Values in a while loop

조회 수: 5(최근 30일)
Craig Chambers
Craig Chambers 2022년 11월 13일
답변: the cyclist 2022년 11월 13일
I'm trying to run a loop where two of the variables needed for the final equation change each time. Im trying to get 9 different values however its only spitting out the last one and only the last one. Not sure what i'm doing wrong any help is appreciated.
%Static Values
m=20;
m0=2;
e=0.2;
wr=(21+2/3)*pi;
i=0;
for i=0:1:8
i=i+1
if i==2 k=5000; z=0.2; %Changing k and z for 9 cases
elseif i==3 k=5000; z=0.4;
elseif i==3 k=5000; z=0.99;
elseif i==4 k=15000; z=0.2;
elseif i==5 k=15000; z=0.4;
elseif i==6 k=15000; z=0.99;
elseif i==7 k=90000; z=0.2;
elseif i==8 k=90000; z=0.4;
elseif i==9 k=90000; z=0.99;
wn=sqrt(k/m);
r=wr/wn;
n=2.*z.*r;
Fun=(m0*e/m)*(r.^2/(sqrt((1-r.^2).^2)+n.^2))
T=7;
Ft=Fun*T*i %final value
end
end

답변(2개)

Walter Roberson
Walter Roberson 2022년 11월 13일
if i==2 k=5000; z=0.2; %Changing k and z for 9 cases
elseif i==3 k=5000; z=0.4;
The scope of the statements expected to be done upon if ends when the elseif starts.
elseif i==3 k=5000; z=0.99;
The scope of the statements expected to be done upon the previous elseif ends when this elseif starts.
elseif i==4 k=15000; z=0.2;
elseif i==5 k=15000; z=0.4;
elseif i==6 k=15000; z=0.99;
elseif i==7 k=90000; z=0.2;
elseif i==8 k=90000; z=0.4;
elseif i==9 k=90000; z=0.99;
The scope of the statements expected to be done for this elseif would end at the point where an elseif started, if there were an elseif. But there is no elseif, so the scope of statements for this if ends at the first unmatched end
wn=sqrt(k/m);
r=wr/wn;
n=2.*z.*r;
Fun=(m0*e/m)*(r.^2/(sqrt((1-r.^2).^2)+n.^2))
T=7;
Ft=Fun*T*i %final value
end
which is there.
In MATLAB, if and elseif do not extend to end-of-line: they extend to the first elseif or end that is at the same nesting level.

the cyclist
the cyclist 2022년 11월 13일
I could not tell exactly what your code is supposed to do, but here I made a couple modifications:
  1. Bring the end statement that is associated with the if statement up, just after the last elseif. I think this is what you intend.
  2. Define Ft as a vector, and index the value into it for each iteration of the for loop.
  3. Ran your loop over only 1:8 instead of 0:8. The reason is that you do not define k or z when happen when i==1 in your if statement.
%Static Values
m=20;
m0=2;
e=0.2;
wr=(21+2/3)*pi;
i=0;
Ft = zeros(1,8);
for i=1:1:8
i=i+1;
if i==2 k=5000; z=0.2; %Changing k and z for 9 cases
elseif i==3 k=5000; z=0.4;
elseif i==3 k=5000; z=0.99;
elseif i==4 k=15000; z=0.2;
elseif i==5 k=15000; z=0.4;
elseif i==6 k=15000; z=0.99;
elseif i==7 k=90000; z=0.2;
elseif i==8 k=90000; z=0.4;
elseif i==9 k=90000; z=0.99;
end
wn=sqrt(k/m);
r=wr/wn;
n=2.*z.*r;
Fun=(m0*e/m)*(r.^2/(sqrt((1-r.^2).^2)+n.^2));
T=7;
Ft(i)=Fun*T*i; %final value
end
Ft
Ft = 1×9
0 0.2532 0.2648 0.5611 0.4736 0.1765 5.1919 1.6748 0.3191
You have a few oddities in your code that I did not try to fix:
  • You loop over a particular range of i, then immediately add 1 to it. Why not just loop over the values you want?
  • You set i=0 before the loop, but that value is never used.
  • You could have just set k and z as vectors of values, and not used the awkward for loop and if statements. It seems to me that you would find the MATLAB Onramp tutorial useful.

범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

태그

Community Treasure Hunt

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

Start Hunting!

Translated by