Using fminunc with a summation
2 views (last 30 days)
Show older comments
Hello,
I am trying to minimize this objective function f = Σ|(sqrt(a(i,1)-b(1)).^2+(a(i,2)-b(2)).^2+(a(i,3)-b(3)).^2)-r|.
where I am attempting to find the value for b which minimizes the function and i = 1:n. I am using fminunc but I am unsure of how to pass the summation to it in a valid way. Any advice would be greatly appreciated.
0 Comments
Accepted Answer
Matt J
on 12 Sep 2014
Edited: Matt J
on 12 Sep 2014
One way,
fun=@(b) norm( sqrt(sum(bsxfun(@minus,a,b(:).').^2,2))-r ,1);
[b_opt,fun_opt,eflag,s]=fminunc(fun,b0);
It looks like a doubtful choice of objective function, however. For one thing, if you hope to achieve f=0, it will be hard, because the function is non-differentiable there.
4 Comments
Matt J
on 18 Sep 2014
Edited: Matt J
on 18 Sep 2014
Be careful, though. Getting what you want in one case is often not a guarantee that everything is "working". In particular, what happens when you test the best-case scenario with simulated points a(i,:) lying exactly on the surface of a sphere? Then the optimum value will be f=0 and the solver will have to deal with non-differentiability as it converges toward that point.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!