Please help!! Need to constrain/force LSQNONLIN to pass through a given point e.g. the origin
4 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Yu Jiang
2014-8-11
编辑:Yu Jiang
2014-8-11
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
0 个评论
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!