Optimize a function with a sum of series

11 次查看(过去 30 天)
Hello. Basically, I need to optimize the following function
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 个评论
Paul
Paul 2021-12-12
For n = 25, it looks like y is function of x1 - x25, i.e., . Is that correct?
What is ? That term doesn't show up in the equation for y.
Dmytro Stovolos
Dmytro Stovolos 2021-12-13
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
编辑:Abolfazl Chaman Motlagh 2021-12-12
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.
  1 个评论
Dmytro Stovolos
Dmytro Stovolos 2021-12-13
Hello. Thanks for the answer.
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by