If's and elses?

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)

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"

1 Comment

Alex
Alex on 4 Dec 2013
So how would I go about doing the following basic example:
A is from 0 to 5 with a step of 1 [0 1 2 3 4 5]
If i wanted to create an array for which B is equal to 2A when A is 3 or less, and equal to 3A when a is above 3.
After you recommended for loops, i tried the following:
A = 0:5
for A = 0:3;
B = 2*A
end
for A = 3:5
B = 3*A
end
But obviously this doesnt work either, so I am really unsure how to do this

Sign in to comment.

dpb
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
Alex on 4 Dec 2013
When I try your code in my M file, I get the error:
Undefined function 'iswithin' for input arguments of type 'double'.
I need to be able to plot x against z to produce a single curve, and this is where I am having the trouble.
Take the code starting with "function flg" and write it to a file named "iswithin.m"
Alex
Alex on 4 Dec 2013
Ah thankyou! Now i get an error saying that ix is an undefined variable, what am I missing here?
dpb
dpb on 4 Dec 2013
Edited: dpb 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.

Sign in to comment.

Products

Asked:

on 4 Dec 2013

Edited:

dpb
on 4 Dec 2013

Community Treasure Hunt

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

Start Hunting!