MATLAB: Using fminunc with a summation

fminconfminuncminimizationobjective-functionoptimizationsummation

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.

Best Answer

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.