Please help!! Need to constrain/force LSQNONLIN to pass through a given point e.g. the origin

4 Ansichten (letzte 30 Tage)
Hello
I am struggling to implement constraints on nonlinear regression using lsqnonlin (or fmincon), where I need the fitted function to pass through a given point.
e.g. I have the simple function
function diff = fit_simp(x,X,Y)
A=x(1);
B=x(2);
diff = A*log(X+B) + A*log(1+B) - Y;
end
and the simple X and Y data attached (X and Y in columns 1 and 2 respectively of XY.txt). Then use lsqnonlin as follows:
X0=[-1,0];%Initial conditions
x=lsqnonlin(@fit_simp,X0,[],[],[],X,Y);
Y_new = x(1)*log(X+x(2))+x(1)*log(1+x(2));
plot(X,Y,'+r',X,Y_new,'b');
However the problem I have is that the function needs to pass through the (X,Y) point (1,0).
I have tried reading many help pages and tutorials to solve this issue however I haven't found any solution that applies to this situation. I have tried something similar with fmincon but cannot figure out how to implement the constraints.
So now I am devoid of ideas. I bought the Curve Fitting, Statistics and Optimization Toolboxes to specifically help me with this problem. Please help!!
Many thanks in advance, Chris
  3 Kommentare
Chris
Chris am 11 Aug. 2014
Hi Yu
It is important that the least-squares curve for the model I am adopting [Y = A*log(X+B) + A*log(1+B)] is constrained such that when X=1, Y=0.
I am trying to calculate the values of coefficients A and B that minimize the fit, subject to this constraint.
Hopefully this helps explains the problem better?
Cheers, Chris
Chris
Chris am 11 Aug. 2014
Sorry the X=1,Y=0 is a bad example, as the function can't go through this point. I'll change the example to when X=0.3, Y=1.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Yu Jiang
Yu Jiang am 11 Aug. 2014
Bearbeitet: Yu Jiang am 11 Aug. 2014
Hi Chris
From what I understand, you would like to fit the nonlinear model with your data set, and you would like to know how to impose the nonlinear equality constraint
Fit_simp(x, 0.3, 1) = 0.
In order to achieve data fitting with constraints, I suggest you use the function fmincon (See Documentation) , which allows you to specify nonlinear constraints.
You may consider the following steps:
1. Revise the objective function as follows
function diff = fit_simp(x,X,Y)
A=x(1);
B=x(2);
diff = norm(A*log(X+B) + A*log(1+B) - Y);
end
The new function returns a scalar as the data fitting error. You may save the function as a MATLAB file under the same name as the function. See the documentation for the function norm.
2. Create the nonlinear constraint function
function [c,ceq] = nlcon(x, X, Y)
A=x(1);
B=x(2);
c = [];
ceq = A*log(X+B) + A*log(1+B) - Y;
end
This function has two outputs, c is the output of inequality nonlinear constraints and ceq is the output of the equality constraints. Since we only have equality constraints here, I set c=[]. Again, save this function in a MATLAB file.
3. In the main MATLAB script or in the command window, try the following code (Assuming X and Y are column vectors which contain your data set)
>> X0=[1,0];
>> x=fmincon(@(x) fit_simp(x,X,Y),X0,[],[],[],[],[],[],@(x) nlcon(x,0.3,1));
Then, you should have the expected answer. If you would like to have better data fitting accuracy, you can always specify the options (See Documentation) for fmincon.
For more information regrading nonlinear constraints, you can refer to the link below:
-Yu

Weitere Antworten (1)

Chris
Chris am 11 Aug. 2014
Perfect! Thank you so much Yu, your help is greatly appreciated. Chris

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by