If's and elses?
Show older comments
This may be a really stupid question, but here goes: I have an array, x, from 0 to c. I want to create an array of values for Z using two functions of x. So, if x is less than 90% of c, I would like Z to be equal to one function of x, and for all other values of x, I would like Z to be equal to a different function of x. I have tried for loops, while loops, and now I am experimenting with if's and else if's, but for the code shown below, I always seem to end up with the whole array of Z being the first function of x. I've also included the function for dZ in between the if's, and this has the same problem. Can anyone see where I am going wrong? Thanks
if 0 <= x <= Pc;
Z = ((M.*c)/P.^2).*((2.*P.*(x./c))-(x./c).^2);
dZ = ((Mc.*2)/(P.*c))-((2.*Mc.*x)./(P.^2.*c.^2));
elseif Pc <= x <= c;
Z = (Mc./((1-P).^2)).*((1-(2.*P))+(2.*P.*(x./c))-((x./c).^2));
dZ = ((Mc)./((1-P).^2)).*((1-(2.*P))+(2.*P.*(1./c))-(2*x./c.^2));
end
Answers (2)
Walter Roberson
on 4 Dec 2013
In MATLAB,
if 0 <= x <= Pc
means
if ((0 <= x) <= Pc)
which compares 0 to x, returning either 0 (false) or 1 (true), and then compares that 0 or 1 to Pc.
Also, in MATLAB, when you have an "if" condition, it is considered to be satisfied only if all of the values in the matrix are non-zero. For example,
if [2 5] <= 3
is false because not all values on the left hand side are <= 3.
See any() and all() and see "for", and also research "logical indexing"
dpb
on 4 Dec 2013
One of the most common difficulties in Matlab syntax for newbies -- if is T iff all(condition) is T. So, if your x vector has a portion of values that don't satisfy the conditions (as it surely must or there's no point) then the condition isn't satisfied and the clauses aren't executed.
"Logical addressing" is one way...
x1=x(iswithin(x,0,Pc));
Z(ix) = ((M.*c)/P.^2).*((2.*P.*(x1./c))-(x1./c).^2);
dZ = ((Mc.*2)/(P.*c))-((2.*Mc.*x1)./(P.^2.*c.^2));
x2=x(iswithin(x,Pc,c;
Z = (Mc./((1-P).^2)).*((1-(2.*P))+(2.*P.*(x2./c))-((x2./c).^2));
dZ = ((Mc)./((1-P).^2)).*((1-(2.*P))+(2.*P.*(1./c))-(2*x2./c.^2));
I wrote w/ temporaries that can be dispensed with (with care in keeping parens nesting correct and also w/o the possible refinement if the two areas are together inclusive of, in that case, x2=~x1. But since you wrote the if...elseif... instead of if...else I presume that isn't the case.
iswithin is just a simpler utility helper function or syntactic candy to put the details at a lower level...
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
4 Comments
Alex
on 4 Dec 2013
Walter Roberson
on 4 Dec 2013
Take the code starting with "function flg" and write it to a file named "iswithin.m"
Alex
on 4 Dec 2013
I switched horses in midstream and didn't unsaddle the first -- ix should be the return from the iswithin call -- a logical vector where the condition is true. Then instead of x1 and x2 use x(ix) in the expressions. Well, here, let me just rewrite the first consistently--
ix=iswithin(x,0,Pc);
Z(ix) = ((M.*c)/P.^2).*((2.*P.*(x(ix)./c))-(x*ix)./c).^2);
dZ(ix) = ((Mc.*2)/(P.*c))-((2.*Mc.*x(ix))./(P.^2.*c.^2));
ix=iswithin(x,Pc,c);
Z(ix) = (Mc./((1-P).^2)).*((1-(2.*P))+(2.*P.*(x(ix)./c))-((x(ix)./c).^2));
dZ(ix) = ((Mc)./((1-P).^2)).*((1-(2.*P))+(2.*P.*(1./c))-(2*x(ix)./c.^2));
This time I did reuse ix and didn't save the two conditional x-vectors separately but used the logical addressing thruout.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!