MATLAB: How to fit imposing some conditions

conditionsfit

Hi every one, I've a matlab program. In the program there is a call to an external program for the fit function. This is the external program
function [estimates, model] = fitcurvedemo(xdata, ydata)
% Call fminsearch with a random starting point.
global ckgraconv_n ckdian
global A lambda sse R2
start_point = rand(1,2);%%scegli un valore casuale per tutti i parametri
model = @fun;
estimates = fminsearch(model, start_point);
function [sse, FittedCurve] = fun(params)
A = params(1);
lambda = params(2);
FittedCurve =( A.*ckgraconv_n + lambda.*ckdian); %%picco gaussiano allargato
ymean = mean(ydata);
SS = ydata-ymean;
SSt =sum(SS.^2);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
R2 = 1 - sse/SSt;
end
end
The y data are called ydata and the fit function is:
A.*ckgraconv_n + lambda.*ckdian
I must edit the fit so that the residual values are all positive, that is
(ydata-A.*ckgraconv_n - lambda.*ckdian)>0.
Then I must fit the data (finding A and lambda values) and imposing the condition
(ydata-A.*ckgraconv_n - lambda.*ckdian)>0.
I tried writing
function [sse, FittedCurve] = fun(params)
params=fminsearch(model,ydata-params(1).*ckgraconv_n - params(2).*ckdian>0);
A = params(1);
lambda = params(2);
but I get the error
Error using fminsearch (line 96)
FMINSEARCH accepts inputs only of data type double.
Error in fitcurvedemo/fun (line 11)
params=fminsearch(model,ydata-params(1)*ckgraconv_n - params(2)*ckdian>0);
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in fitcurvedemo (line 8)
estimates = fminsearch(model, start_point);
Error in sp2sp3diff_Titantah (line 659)
[estimates, model] = fitcurvedemo(xdata,ydata);
Please, can someone help me? Best regards

Best Answer

It is a linear fit, so lsqlin would be appropriate
C=[ckgraconv_n(:) , ckdian(:)];
d=ydata(:);
Aineq=C;
bineq=d;
solution=lsqlin(C,d,Aineq,bineq);