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 中查找有关 Computational Geometry 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by