From: Greig on
I would like to define a piecewise function handle but I'm not too sure how to do it.
Basically something like:

fn=@(x) if(x<0) 1
elseif (x>1) 0
else (1/n)(1-x)^((1/n)-1)
end

where n is a constant.
Cheers.
From: Gene on
"Greig " <greig(a)abc.com> wrote in message <hp141p$cvu$1(a)fred.mathworks.com>...
> I would like to define a piecewise function handle but I'm not too sure how to do it.
> Basically something like:
>
> fn=@(x) if(x<0) 1
> elseif (x>1) 0
> else (1/n)(1-x)^((1/n)-1)
> end
>
> where n is a constant.
> Cheers.

Greig:
One way to to this is to use 0-1 (logicals)

fn = @(x) 1*(x<=0) ...
+ (1/n)*(1-x).^(1/n-1).*(x > 0).*(x <= 1) ...
+ 0 * (x > 1);

Of course, the last line can be omitted, as can the 1* in the first line. BTW your function is not well-defined at x = 1. You may want to move the x = 1 condition

emc

From: Greig on
"Gene" <ecliff(a)vt.edu> wrote in message <hp2jau$eat$1(a)fred.mathworks.com>...
> "Greig " <greig(a)abc.com> wrote in message <hp141p$cvu$1(a)fred.mathworks.com>...
> > I would like to define a piecewise function handle but I'm not too sure how to do it.
> > Basically something like:
> >
> > fn=@(x) if(x<0) 1
> > elseif (x>1) 0
> > else (1/n)(1-x)^((1/n)-1)
> > end
> >
> > where n is a constant.
> > Cheers.
>
> Greig:
> One way to to this is to use 0-1 (logicals)
>
> fn = @(x) 1*(x<=0) ...
> + (1/n)*(1-x).^(1/n-1).*(x > 0).*(x <= 1) ...
> + 0 * (x > 1);
>
> Of course, the last line can be omitted, as can the 1* in the first line. BTW your function is not well-defined at x = 1. You may want to move the x = 1 condition
>
> emc
>

Thank for that. You're right about the limit x=1, it's the reason I'm defining it as a piecewise function, I forgot the "=>".

Cheers