Optimization in a for loop
10 views (last 30 days)
Show older comments
Hi,
I have the following code which is a simiplification of a more complex one. The code contains an optimization problem (prob) that finds the optimum values of U1 and U2 to minimize the Root-Mean-Square-Error (prob.Objective) between the simulated (Heat_flow) and measured (M) data. As you can see, the code works and actually finds the optimum solutions. However, those solutions vary for each iteration of the loop.
What I would like to do, is to have only two optimum solutions (for U1 and U2, respectively) that do not vary at each iteration. I tried to introduce a constraint (prob.Constraints.U1=eq(U1,U1(1))), but the solver finds a solution for the first iteration and then applies it to the following ones.
How can I do it?
Thank you!
%Input data (outdoor temperature and areas)
T_out=[18 16 14 15 19 14 17];
Area_1=16;
Area_2=19;
%Measured data
M=[79 84 64 76 86 56 76]';
%Definition of the optimization problem
prob=optimproblem("Description","Esempio");
%Definition of the optimization variables (U-values)
U1 = optimvar('U1',7,1,"LowerBound",0);
U2 = optimvar('U2',7,1,"LowerBound",0);
%Calculation of the heat transfer coefficient
Heat_transfer=U1*Area_1+U2*Area_2;
%Loop for perfoming the calculation for each measurement
Heat_flow=[];
for i=1:max(size(T_out))
Heat_flow=Heat_transfer*(20-T_out(i));
end
%Definition of the objective function (minimization of RMSE)
prob.Objective=((sum((M-Heat_flow).^2))/7)^(1/2);
%Definition of the initial guess (not a linear problem)
initialGuess.U1 = [1 1 1 1 1 1 1];
initialGuess.U2 = [1 1 1 1 1 1 1];
%Solution of the problem
[sol,opt]=solve(prob,initialGuess)
4 Comments
Matt J
on 4 Oct 2023
It would be advisable to reformulate your objective in purely quadratic form. Then, more specialized solvers like lsqlin can be used.
prob.Objective= sum( (M-Heat_flow).^2 ); %equivalent to what you had before.
Accepted Answer
Matt J
on 4 Oct 2023
Edited: Matt J
on 4 Oct 2023
Perhaps this is what you wanted. If so, it is a rather ill-posed problem, because your C matrix is only rank 1.
%Input data (outdoor temperature and areas)
T_out=[18 16 14 15 19 14 17];
Area_1=16;
Area_2=19;
%Measured data
M=[79 84 64 76 86 56 76]';
%Definition of the optimization problem
prob=optimproblem("Description","Esempio");
%Definition of the optimization variables (U-values)
C=(20-T_out(:))*[Area_1,Area_2];
U = optimvar('U',2,1,"LowerBound",0);
%Definition of the objective function (minimization of RMSE)
prob.Objective=sum(C*U-M).^2;
%Solution of the problem
[sol,opt]=solve(prob)
2 Comments
Matt J
on 4 Oct 2023
Edited: Matt J
on 4 Oct 2023
That's irrelevant. Each row C(i,:) represents the coefficients for a particular step. I could have used a loop to create C in a way that depends in some way on previous steps, e.g.,
C=nan( numel(T_out), 2);
for i=1:height(C)
C(i,:)=(20-T_out(i))([Area1,Area2]);
if i>1
C(i,:)=C(i,:) + C(i-1,:);
end
end
More Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox 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!