# Optimize a function with a sum of series

11 views (last 30 days)
Dmytro Stovolos on 12 Dec 2021
Commented: Dmytro Stovolos on 13 Dec 2021
Hello. Basically, I need to optimize the following function  The minimum value for 'n' is 2. Imagine we have n = 25. I have a sum of series so I tried the following:
syms i n
x0=[2:25];
i=[1:25];
f1= @(x)(x(1)+7)^8
f2 = @(x)((0.01*x(i)-i)^2)/i
All looks good until I try to sum both parts
fun = f1+symsum(f2,i,2,25);
Check for missing argument or incorrect argument data type in call to function 'symsum'.
If the previous step was correct, I would call "fminunc(fun,x0);" to optimize the function.
Could you explain why I get this error? 'i' should be a symbolic variable because of being created by syms call. Set of 'x' is defined. I must have missed something but can't find out what.
##### 2 CommentsShowHide 1 older comment
Dmytro Stovolos on 13 Dec 2021
Hello Paul, is a starting value of the corresponding x value. For example, (1) = 2; (2) = 3. Yes, for n = 25 the function represents summation from to .

Abolfazl Chaman Motlagh on 12 Dec 2021
Edited: Abolfazl Chaman Motlagh on 12 Dec 2021
Hi , you don't need necessarily symboilic functionality for this problem. here is a simple solution. (you can run it in single .m file)
clear;
n = 25;
x0 = 1:n;
optimoptions = optimoptions(@fminunc,'Algorithm','quasi-newton',...
'OptimalityTolerance',1e-10,'MaxFunctionEvaluations',1e7,...
,'PlotFcn','optimplotx','Display','iter'); % this line is just for illustration of convergence process
[X,fval,exitflag,output] = fminunc(@(x)(Cost(x,n)),x0,optimoptions);
function y=Cost(x,n)
y = (x(1)+7)^8;
for i=2:n
y = y + (1/i)*((0.1*x(i)-i)^2);
end
end
you may ask why i use optimoptions so serious. in your problem the optimal solution is obvious because every single term is squared of a linear expression. so if x(1) became -7 and for i=2:n, x(i) = i/0.01. the function became 0. with this knowlegde i use more options to see the actual convergence of function. you can test that with no options, optimization stop at very high value point.
Dmytro Stovolos on 13 Dec 2021
If I run with optimoptions I get "Local minimum possible." but if I run without it I get "Local minimum found."
Indeed, fval is high without optimoptions but with this variable the solution is not guaranteed. Please correct me if I am wrong.

R2020b

### Community Treasure Hunt

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

Start Hunting!