Using optimisation function "fminunc" for nth number of time
2 views (last 30 days)
Show older comments
OLUWAFEMI AKINMOLAYAN
on 13 Sep 2023
Commented: OLUWAFEMI AKINMOLAYAN
on 13 Sep 2023
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 Comments
Accepted Answer
Walter Roberson
on 13 Sep 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 Comments
Walter Roberson
on 13 Sep 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
More Answers (1)
Matt J
on 13 Sep 2023
Edited: Matt J
on 13 Sep 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 Comments
Walter Roberson
on 13 Sep 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
on 13 Sep 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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!