How to solve this problem using loop and If?

조회 수: 2 (최근 30일)
Leonardus Risky
Leonardus Risky 2018년 8월 5일
댓글: Star Strider 2018년 8월 6일
Hello Everybody, i now make this script using loop but now i want to keep using loop but i have this 2 condition. first if the calculation run tethadot variable under 5000*2*pi/60 i use the variable values same as in the script for tethaddot and tetha but second condition if the calcuation reach tethadot >= 5000*2*pi/60 the calculation using this variable tethadot=5000*2*pi/60, tethaddot=0, tetha=5000*t*2*pi/60. how to solve this loop uing if inside it? and finally how can i plot sbx vs Resp and sbx vs tethadot i one figure.Thank You.

채택된 답변

Star Strider
Star Strider 2018년 8월 5일
I am not certain what you want to do. I added an if block that appears to do what you want. It may not produce the result you want.
Try this:
M = [14.29 0; 0 14.29];
beta = 0.0002;
C1 = beta.*[150000 0;0 375000];
C2 = [0 -2.871; 2.871 0];
K1 = [1345000 0;0 1570000];
K2 = [0 -2.871; 0 0];
tstart = 0;
tstop = 14;
deltat = 0.001;
sbx = tstart:deltat:tstop;
Resp = zeros(1,length(sbx));
tethadot = zeros(1,length(sbx));
for i = 1:length(sbx)
t = sbx(i) ;
tethadot = (416.7*t)*(2*pi/60);
tetha = ((416.7*(t^2))/2)*(2*pi);
tethaddot = 416.7;
if tethadot >= 5000*2*pi/60 % Test & Replace ‘if’ Block
tethaddot = 0;
tetha = 5000*t*2*pi/60;
end
C = C1 + (tethadot.*C2);
K = K1 + (tethaddot.*K2);
F1 = [1.299*(10^-5)*sin(tetha);1.299*(10^-5)*cos(tetha)];
F2 = [-1.299*(10^-5)*cos(tetha);1.299*(10^-5)*sin(tetha)];
f1 = (tethadot.^2).*F1;
f2 = (tethaddot.^2).*F2;
F = f1 + f2;
A = (4.*M)./(deltat^2) + (2/deltat).*C + K;
dresp = A\F;
Resp(i) = sqrt(dresp(1)^2+dresp(2)^2);
tethadotv(i) = tethadot; % Create Vector Of ‘tethadot’ Values
end
figure(1)
subplot(2,1,1)
plot(sbx,Resp, '-b', 'LineWidth',1)
xlabel('sbx')
ylabel('Resp')
grid
subplot(2,1,2)
plot(sbx, tethadotv, '-m', 'LineWidth',1)
xlabel('sbx')
ylabel('tethadot')
grid
I prefer to use subplot here because ‘Resp’ and ‘tethadot’ have very different magnitudes.
Experiment to get your desired result.
  댓글 수: 2
Leonardus Risky
Leonardus Risky 2018년 8월 6일
okay thank you for your help. i need still try this and make some kind of change but anyway thanks a lot.
Star Strider
Star Strider 2018년 8월 6일
As always, my pleasure.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by