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

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

-

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

@user1945925 Note that you evaluate x2(t2-2) which is all zero –  Dennis Jaheruddin Jan 22 '13 at 13:48