Using optimisation function "fminunc" for nth number of time
Show older comments
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
Accepted Answer
More Answers (1)
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
OLUWAFEMI AKINMOLAYAN
on 13 Sep 2023
Each iteration overwrites the current f with the value on the right-hand side. Perhaps you meant to sum the expressions in the loop like below. However, it is still not clear what you want to do with the input argument n.
function f = objectiveFcn(x,Y,freq,n) % n is given as input here
f=0;
for j= 1:3 %n is overwritten here
%(also, note that the first two iterations n=1 and n=2 don't do anything)
f = f + (Y(j,1) - real(cos((1+1i*x(1))*(2*pi*freq(j)*0.002/(1600))+x(2)+x(3)))).^2 + ...
(Y(j,2) - imag(cos((1+1i*x(1))*(2*pi*freq(j)*0.002/(1600))+x(2)+x(3)))).^2;
end
end
OLUWAFEMI AKINMOLAYAN
on 13 Sep 2023
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
Categories
Find more on Problem-Based Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!