Is my code correct for finding the distance between a point and a surface?

조회 수: 1 (최근 30일)
Hello
We have a point, an (hyper)surface and the distance function like . The surface in my example is .
P = [0.4 0.4 0.3]; % the point
f = @(x) sqrt(sum(x)); % the surface
distsq = @(x) sum((x-P(1:end-1)).^2) + (f(x)-P(end)).^2; % the distance function squared,want to minimize
[x,fval] = fmincon(distsq,[0.5 0.5],[],[],[],[],[0 0],[1 1])
I want to go higher in dimensions and see how it performs. I just don't know how can I be somewhat sure that the result from fmincon is correct. I'm interested only in the hypercube.

채택된 답변

Matt J
Matt J 2019년 9월 19일
편집: Matt J 2019년 9월 19일
It's largely correct, except that your function distsq is not differentiable at x=0. So, if there's a chance the solution might lie there (but I think it's impossible if P(n) and at least one other P(i) are greater than zero), then I would make a transformation to get rid of the non-differentiability. In this case, this could be,
distsq = @(x) sum((x-P(1:end-1)).^2) + (f(x).^2-P(end).^2).^2;
Note however that for the specific f in your example, the transformation turns the problem into a linear least squares problem, so that you can use lsqlin instead of fmincon,
C=[speye(n-1);ones(1,n-1)];
d=P; d(end)=d(end)^2;
[x,fval] = lsqlin(C,d,[],[],[],[],[0 0],[1 1]);
This also has the advantage that lsqlin is globally convergent and doesn't require an initial guess.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Quadratic Programming and Cone Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by