Using optimisation function "fminunc" for nth number of time
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2023-9-13
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 个评论
Walter Roberson
2023-9-13
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
更多回答(1 个)
Matt J
2023-9-13
编辑:Matt J
2023-9-13
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 个评论
Walter Roberson
2023-9-13
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
2023-9-13
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!