Using fminunc with a summation

2 views (last 30 days)
Nila
Nila on 12 Sep 2014
Edited: Matt J on 18 Sep 2014
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.

Accepted Answer

Matt J
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
Nila
Nila on 18 Sep 2014
Hi Matt,
Thanks for your help! I actually played with the first solution you provided to make sure I was getting what I wanted through each function and I did! The minimization that resulted from it looks good.
Thanks again!
Matt J
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!