Using optimisation function "fminunc" for nth number of time

2 visualizaciones (últimos 30 días)
I aim trying to optimise some functions at a goal. The have common expressions but the constant value changes on every instsance. I tried using for loop, but the code give same output everytime.
clc
clear all
x0 = [1;1;1];
freq=xlsread('data.xlsx', 'Sheet1', 'A1:A3026');
T1 =xlsread('data.xlsx', 'Sheet1', 'B1:B3026');
T2=xlsread('data.xlsx', 'Sheet1', 'C1:C3026');
Y = [T1 T2];
n = 1:15;
for n = 1:15
objfun = @(x)objectiveFcn(x,Y,freq,n);
% options = optimoptions("fminunc","Display","iter","PlotFcn",...
% ["optimplotfunccount","optimplotfval","optimplotfirstorderopt"]);
[solution,objectiveValue] = fminunc(objfun,x0);
% Clear variables
% clearvars objfun options
solution
end
% objectiveValue
% solution
function f = objectiveFcn(x,Y,freq,n)
for n= 1:15
% f= cell(1,3);
f = (Y(n,1) - real(cos((1+1i*x(1))*(2*pi*freq(n)*0.002/(1600))+x(2)+x(3)))).^2 + ...
(Y(n,2) - imag(cos((1+1i*x(1))*(2*pi*freq(n)*0.002/(1600))+x(2)+x(3)))).^2;
end
end
  2 comentarios
Torsten
Torsten el 13 de Sept. de 2023
Editada: Torsten el 13 de Sept. de 2023
I cannot make sense of your code. Maybe you could try again to state your problem in a mathematical manner.
OLUWAFEMI AKINMOLAYAN
OLUWAFEMI AKINMOLAYAN el 13 de Sept. de 2023
Editada: OLUWAFEMI AKINMOLAYAN el 13 de Sept. de 2023
I am trying to minimise the function "f"' using three parameters. However the function "f" is nth number of functions. Hence, i should have nth number of parameters x(n,3) after optimsation. I only chose n to be 15 as an example.
I believe i have to find a way to index each objective function with its nth value in other to find solution assign to each index. but dont know how?

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Sept. de 2023
Do not use a for loop inside the objective. Each iteration of the for loop, you are overwriting all of f so the result is the same as if you had done only the final iteration of the loop.
Instead, have the for loop be outside and have the call to the function pass in the "current" value of the parameter to the objective function; see http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
  4 comentarios
Walter Roberson
Walter Roberson el 13 de Sept. de 2023
However you should also consider
function f = objectiveFcn(x,a1,a2,b)
expr = cos((1+1i*x(1))*(2*pi*b*0.002/(1600))+x(2)+x(3));
f = (a1 - real(expr)).^2 + (a2 - imag(expr)).^2;
end

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 13 de Sept. de 2023
Editada: Matt J el 13 de Sept. de 2023
It is because the n that you pass to objectiveFcn(x,Y,freq,n) is being overwritten by n=3 inside your objective function code.
function f = objectiveFcn(x,Y,freq,n) % n is given as input here
for n= 1:3 %n is overwritten here
%(also, note that the first two iterations n=1 and n=2 don't do anything)
f = (Y(n,1) - real(cos((1+1i*x(1))*(2*pi*freq(n)*0.002/(1600))+x(2)+x(3)))).^2 + ...
(Y(n,2) - imag(cos((1+1i*x(1))*(2*pi*freq(n)*0.002/(1600))+x(2)+x(3)))).^2;
end
end
  5 comentarios
Walter Roberson
Walter Roberson el 13 de Sept. de 2023
If I understand correctly, you want to minimize once for freq(1), then minimize for freq(2), then for freq(3), and so on, with each minimization not affecting the rest. If that is correct then use the strategy I indicated in my Answer.
If the information about the different frequencies is required all in the same call, to do some kind of composite minimization, then what I outlined would not be appropriate.
Matt J
Matt J el 13 de Sept. de 2023
I dont intend to sum the expression. The n input just serve purpose of iteration
Then it's not clear why you have a for-loop in your objectiveFcn. Possibly, this is what you want is,
function f = objectiveFcn(x,Y,freq,n)
f = (Y(n,1) - real(cos((1+1i*x(1))*(2*pi*freq(n)*0.002/(1600))+x(2)+x(3)))).^2 + ...
(Y(n,2) - imag(cos((1+1i*x(1))*(2*pi*freq(n)*0.002/(1600))+x(2)+x(3)))).^2;
end

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by