Minimizing a cost with constraint - lsqnonlin
4 views (last 30 days)
Show older comments
Hi, I'm pretty new to minimisation/optimisation algorithm and am very confused as to why my code doesn't work properly. Basically I am confused about lsqnonlin and fsolve I would like to enforce the equation xprimhat'*F*xhat = 0 while lsqnonlin is running but am unsure as to how to do this.
Also I'm not sure if my euclidean distance is right since the doc stated that lsqnonlin squares and sums the cost itself? I'm not sure how that influences my code.
Could anyone please tell me how I could enforce this constraint?
Thanks a lot or your help.
function [Fmin, Pprim] = minimizeFundM( F, P, Pprim, X, Xprim, cloudP, e2_skew)
options = optimset('Algorithm','levenberg-marquardt', 'Display', 'off');
Pprim = lsqnonlin(@(in)distance(X, Xprim, F, cloudP, P, in), Pprim, [],[], options);
Ppseud = pinv(P);
Fmin = e2_skew*Pprim*Ppseud;
end
function d = distance(x, xprim, F, cloudP, P, Pprim)
% backproject points
xhat = P*cloudP;
xprimhat = Pprim*cloudP;
%epipolar constraint
constraint = @(xhat) xprimhat'*F*xhat;
options = optimset('Algorithm','levenberg-marquardt', 'Display', 'off');
xhat = fsolve(constraint, xhat, options);
%euclidean distance
dist = x - xhat;
dist2 = xprim - xprimhat;
d = sqrt(sum(dist.^2+dist2.^2));
end
0 Comments
Answers (1)
Matt J
on 8 Mar 2015
Edited: Matt J
on 9 Mar 2015
You should be using fmincon, rather than lsqnonlin, if you have a nonlinear objective and constraints more complicated than simple upper and lower bounds. Beyond that, we cannot tell you if your function is "right", because only you know what optimization problem you are trying to solve! However, it appears that you have attempted to stick constraint enforcement inside your distance() objective function in some way, which I suspect is not what you truly want to do.
0 Comments
See Also
Categories
Find more on Quadratic Programming and Cone Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!