MATLAB: How to use fminunc with summation

fminuncminimumsummationunconstrained multivariable function

I am trying to solve the following function with fminunc:
Σ((x(i)-1).^2) for i = 1 to 20
My code is below:
fun4 = @(x) sum((x(i)-1).^2, i, 1, 20)
[x,fval,exitflag,output]= fminunc(fun4,[1,1])
And, it gives the following error:
??? Error using ==> sym.sym>notimplemented at 2514
Function 'subsindex' is not implemented for MuPAD symbolic objects.
Could you help me, please?

Best Answer

I found the answer of my question:
The summation should be written like this:
fun4 = @(x) sum((x(1:2)-1).^2)
[x,fval,exitflag,output]= fminunc(fun4,[1,1])
x(1:2) expresses the interval, the begining and end values of summation
And the important thing is that: the number of x_i values should be match with the number of values of the input vector [1,1]
For example; for x(1:5) , the input vector may be like this: [1,1,1,1,1]
Thanks for your interest.