Matlab, anonymous functions, making a piecewise function, how?

3.4k Views Asked by At

If I want to make a piecewise function where lets say $f(x) = x^2 +1$ at $1 \leq t \leq 2$ and zero elsewhere. How can I make this possible using an anonymous function? I wanted to invent y = @(t) ((t.^2 + 1)*(t>=1)&&(t<=2))
But that obviously did not work out at all.

Thanks

1

There are 1 best solutions below

5
On BEST ANSWER

The function you have works for scalar input, but I guess you want to make it for vectors, in that case you need to adjust it a bit:

y = @(t) ((t.^2 + 1).*(t>=1)&(t<=2))