Help with MATLAB not checking/saving the conditions of an if statement within a for loop
1 view (last 30 days)
Show older comments
k1 = 1;
k2 = 2;
lb = [0 0 0];
ub = [50 50 50];
n = 1000;
length = 0;
p = 3;
xn = lhsdesign(n,p);
x = bsxfun(@plus,lb,bsxfun(@times,xn,(ub-lb)));
tol = 0.1;
for i = 1:1000
k3 = x(randi([1 n]),1);
k4 = x(randi([1 n]),2);
k5 = x(randi([1 n]),3);
parasite=@(t,X) [k1.*X(1).*X(2)-k2.*X(1); k3-k4*X(2)-k5*X(1)];
[t,X]=ode45(parasite,[0 20],[1 1]);
a1(i) = X(end,1);
a2(i) = X(end,2);
if a1(i) < tol || (2-tol) < a2(i) || a2(i) < (2+tol)
k3a(i) = k3;
k4a(i) = k4;
k5a(i) = k5;
else
k3a(i) = 0;
k4a(i) = 0;
k5a(i) = 0;
end
end
figure(1)
plot3(k3a,k4a,k5a,'Linestyle','none','Marker','o')
I am having an issue where the if statement is not function as intended. If the values of a1 or a2 are within 0.1 above or below 0 for a1 and 2 for a2, the values for k3a, k4a, and k5a are to be set to the value they are calculated as during the for loop, if not the values of k3a, k4a and k5a are set to 0.
However, the conditions set within the if statement do not work, and do not affect the values of k3a,k4a, or k5a. What is wrong within this code/ what can be changed to achieve this as stated above.
Cheers
0 Comments
Accepted Answer
madhan ravi
on 22 Oct 2018
TRY:
if a1(i) < tol && (2-tol) < a2(i) && a2(i) < (2+tol)
didn't go through the whole code
More Answers (0)
See Also
Categories
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!