MATLAB: How to perform regression and impose a constraint on the regression function using the Statistics toolbox in MATLAB 7.13 (R2011b)

constrainconstraintfminconOptimization ToolboxregressregressionStatistics and Machine Learning Toolbox

I want to perform regression on a set of X and Y data, but I want to constrain the function f in some way; for instance, to insure that the regression satisfies y0=f(x0) for a specified point (x0,y0). How do I do this in MATLAB 7.13 (R2011b)?

Best Answer

You can do this using the FMINCON function in the Optimization Toolbox. As an example, you can try to fit the curve f(x)=m*x+b with the constraint that f(1)=2. The steps you will need to use are:
1) Write a function which computes the error in the fit given vectors of coefficients and the X-Y data. A small example of such a function is:
function error=myfun(coeff,x,y)
m=coeff(1);b=coeff(2);
error=sum((m*x+b-y).^2);
% Taking the sum of the squares of the residuals to minimize
2) Write a constraint function, such as:
function [ineq,eq]=mycon(coeff,x,y)
m=coeff(1);b=coeff(2);
x0=1;y0=2;
% We want the curve to pass through (1,2)
ineq=0;
% Nonlinear inequality constraint
eq=abs(m*x0+b-y0);
% Linear inequality constraint
The EQ value is the difference between the function value at x0 (m*x0+b) and the value you want the function to have at x0.
3) Choose initial guesses for the coefficients and generate the data:
ic=[1 1];
% Initial coefficient guesses
xdata=1:10;
ydata=5*xdata-3+rand(size(xdata));
% Generate slightly noisy x and y data
4) Call FMINCON.
MyCoeffs=fmincon(@myfun,ic,[],[],[],[],[],[],@mycon,[],xdata,ydata);