Optimize a function with a sum of series

18 visualizzazioni (ultimi 30 giorni)
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 Commenti
Paul
Paul il 12 Dic 2021
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 il 13 Dic 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 .

Accedi per commentare.

Risposta accettata

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh il 12 Dic 2021
Modificato: Abolfazl Chaman Motlagh il 12 Dic 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.
  1 Commento
Dmytro Stovolos
Dmytro Stovolos il 13 Dic 2021
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by