How to write multiple levels of if-else statements
16 views (last 30 days)
Show older comments
Hi,
i am trying to write a code for the following:
h(x,y) = h_init, if (X,Y)not equal to Q
h(x,y) = h_init+h_step, if (X,Y)equal to Q
where Q is:
-3/4<X<3/4, and
[(1/sqrt3)X+(K-.5)*sqrt(3)/2]<Y<[X/sqrt3 + sqrt3/4], if Y>0 OR
[-X/sqrt3-sqrt3/4]<Y<[-X/sqrt3+(0.5-K)sqrt3/2], if Y<0
This is my current code but it is giving me errors:
K=0.2; % ratio of length of inner and outer triangles 0<K<1
R_P=sqrt((16*delta_sq*r_1^2)/(3*sqrt(3)*(1-K^2)));
h_step = 5e-06; % step microasperity depth
nHi=max(size(X));
nHj=max(size(Y));
% flat step condition
for ii=1:nHi,
for jj=1:nHj,
if (X(ii)/R_P)>(-3/4) & (X(ii)/R_P)<(3/4),
if ((Y(ii)/R_P)>0),
(Y(ii)/R_P)>((X(ii)/(R_P*sqrt(3)))+(((K-0.5)*sqrt(3))/2)) & (Y(ii)/R_P)<((X(ii)/(R_P*sqrt(3)))+(sqrt(3))/4),
h(ii,jj)=h_init+h_step;
if ((Y(ii)/R_P)<0),
(Y(ii)/R_P)>((-(X(ii))/(R_P*sqrt(3)))-(sqrt(3))/4) & (Y(ii)/R_P)<((-(X(ii))/(R_P*sqrt(3)))+(((0.5-K)*sqrt(3))/2)),
h(ii,jj)=h_init+h_step
elseif (X(ii)/R_P)<(-3/4) & (X(ii)/R_P)>(3/4),
h(ii,jj)=h_init;
end
end
end
end
end
I think the mistake lies with the different levels of the if-condition. The code provided gives me errors as some parts of the domain are not defined, as per Matlab error. Any help will be appreciated.
1 Comment
Accepted Answer
Geoff Hayes
on 24 Aug 2014
Tim - there are a couple of things that you need to address in the conditions for your if/elseif statements.
Replace the single ampersands (&) with double ampersands so that short circuiting of the logical operations can occur. While the single ampersand works, it doesn't employ the short-circuiting behaviour. For example, your first if condition would become
if (X(ii)/R_P)>(-3/4) && (X(ii)/R_P)<(3/4)
Note how I removed the comma from the end of the statement (not needed). You could also replace the above with the absolute function, abs
if abs(X(ii)/R_P) < 3/4
unless you are concerned with performance. Now with this if you will need an else, because if X doesn't satisfy this condition then you know that (X,Y)not equal to Q so the above becomes
if abs(X(ii)/R_P) < 3/4
% maybe Q is satisfied
else
% Q is definitely not satisfied
h(ii,jj)=h_init;
end
Now we move to the section commented above maybe Q is satisfied, and do something similar with the Y
if abs(X(ii)/R_P) < 3/4
% maybe Q is satisfied
if (Y(ii)/R_P)>0
% maybe Q is satisfied
elseif (Y(ii)/R_P)<0
% maybe Q is satisfied
else
% Y is zero, so Q definitely not satisfied
h(ii,jj)=h_init;
end
else
% Q is definitely not satisfied
h(ii,jj)=h_init;
end
So I've added the three cases for Y being positive, negative and zero. Again I've removed commas from the end of the conditions. I've done it this way because I don't see how having two conditions separated by a comma will correctly give you what you need
if ((Y(ii)/R_P)>0),(Y(ii)/R_P)>((X(ii)/(R_P*sqrt(3)))+(((K-0.5)*sqrt(3))/2))
Now repeat this pattern of maybe Q satisfied and Q definitely not satisfied for the above if and elseif. You will always want to include a final else body so that h is initializes to something.
Do note how your elseif statement will never evaluate to true
elseif (X(ii)/R_P)<(-3/4) & (X(ii)/R_P)>(3/4)
The code tries to see if X(ii)/R_P is less than -3/4 AND X(ii)/R_P is greater than 3/4. Usually, you would want to replace this with a logical or like
elseif (X(ii)/R_P)<(-3/4) || (X(ii)/R_P)>(3/4)
but I think that you can do away with altogether because we have added the final else statement to the block of code to handle when X doesn't fit in the interval (-3/4,3/4).
2 Comments
Geoff Hayes
on 24 Aug 2014
Hi Tim - yes I think that you almost have the second condition, it is just the use of commas that is confusing me. So it should be something more like
if (Y(ii)/R_P)>0
if (Y(ii)/R_P)>((X(ii)/(R_P*sqrt(3)))+(((K-0.5)*sqrt(3))/2)) && ...
(Y(ii)/R_P)<((X(ii)/(R_P*sqrt(3)))+(sqrt(3))/4)
h(ii,jj)=h_init+h_step;
else
h(ii,jj)=h_init;
end
elseif (Y(ii)/R_P)<0
% etc.
else
% etc.
end
The above inner if can probably be simplified since there is a common (X(ii)/(R_P*sqrt(3))) term on both sides (that can be left until later). Just note the use of the double ampersand and the else statement.
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer 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!