How to solve this problem using loop and If?
1 view (last 30 days)
Show older comments
Leonardus Risky
on 5 Aug 2018
Commented: Star Strider
on 6 Aug 2018
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.
0 Comments
Accepted Answer
Star Strider
on 5 Aug 2018
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!