I keep getting this error when trying to use a flat parabola function as my objective function:

f(x) = 0, a<=x<=b;

= x^2, otherwise

I am using matlab function handle to define it:

f = {@(var)((2*(var-Tu(i))^2).*(var>=Tu(i)) + (0).*(and(Tl(i)<=var,var<=Tu(i))) + (2*(var-Tl(i))^2).*(var<=Tl(i)))};

This handle works fine with fmincon().

Can someone please suggest any other way that I can express this function? I will be using a sum of such functions as my final objective function.

Thanks a lot for the help.