From: Steven Lord on

"Shaun " <swdy7(a)umkc.edu> wrote in message
news:hlmfc5$54o$1(a)fred.mathworks.com...
>I am wanting to plot a piecewise continuous function, I am not able to find
>the correct syntax to do this. Here is a simple example that should give a
>triangle centered at x=0,
>
> f(x)= { 1+x -1<=x<=0
> { 1-x 0<x<=1
>
> This is how I am incorrectly trying to do this (it seems others suggest
> this approach over using the inline function)
>
> x=-1:0.01:1; f = @(x) ( (1+x).*(-1<=x<=0) + (1-x).*(0<x<=1) );

The problem here is the inequality pieces.

(-1 <= x <= 0) does NOT mean "x is between -1 and 0 inclusive" -- it in fact
means x < -1! The reason for that is because (-1 <= x <= 0) is equivalent
to ( (-1 <= x) <= 0). If x is greater than or equal to -1, this reduces to
(1 <= 0) which returns false; if x is less than -1, it reduces to (0 <= 0)
which is true.

You can use James's suggestion that uses the functional form of the AND
function, or you can use the operator form:

f = @(x) (1+x).*(-1 <= x & x <= 0) + ...

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Shaun on
I was definitely looking at that the wrong way, thanks for making that point clear.