How to apply constraints for polynomial to polynomial parameters?

I have a polynomial that I use for least squares regression on experimental data to find coefficients $$a_0,a_1,a_2$$:

$$f(x)=a_0+a_1 x+a_2 x^2$$

I have a constraint for $$f(x)$$, e.g. :

$$Y

and I know the range of the variable $$x$$. How do I apply this knowledge to constrain parameters $$a_0$$,$$a_1$$,$$a_2$$?

My solution was to create a linearly spaced vector of values in the allowed range of $$x$$ and supplying them to the polynomial. The result of the polynomial was then checked against the constraint. Is there a better way to do this? I used Matlab and fmincon function.

If you're using a computer, then all you really need to check are the two end points of your range, and if $$x=-\frac{a_1}{2a_2}$$ is within your range then you need to check that too.

In fact, that's probably the easiest thing to do by hand as well.

The reason that this works is that $$x=-\frac{a_1}{2a_2}$$ is the extremum point of your parabola, and on either side of that point the function is monotonic.

That's the simple way to do it, although you should probably not use fmincon, as the problem is a simple quadratic program (quadratic objective from your least-squares objective, and your constraints will be linear in the parameters)

Since you are in MATLAB, here is a quick-start via the Toolbox YALMIP (disclaimer, developed by me)

% Data
t = 0:0.1:1;
y = sin(pi*t)+0.2*randn(1,length(t));
clf;plot(t,y);

sdpvar a0 a1 a2
yhat = a0 + a1*t + a2*t.^2;

% Grid points to evaluate constraints
xi = 0:0.01:1;
yi = a0 + a1*xi + a2*xi.^2;

% Solve the problem. Best possible solver will automatically be selected
Constraints = [0 <= yi <= 1];
Objective = sum((y-yhat).^2);
optimize(Constraints,Objective)

% Plot the computed polynomial in the evaluation points
hold on
plot(xi,value(yi))

% The parameters
value([a0 a1 a2])