Run same nonlinear optimization with coefficients changing each time

Hi Everyone,
I am running an optimization in matlab with both linear and non linear contraints, using fmincon. I have therefore a main file, where the optimization runs and where the linear constraints are and two function files, one with the objective function and one with the nonlinear constraints ([c],[ceq]).
The code is working but now I need it to run multiple times because two constants that I import from excel change (lets call them 'mass' and 'energy'). These constants are present in all files (which has been my biggest problem when I try to write a for loop), this means they are present in the linear constraints, in the nonlinear constraints and in the main function.
This is an easy representation of how it looks like:
MAIN FILE:
Mass= xlsread('Excel_file.xlsx','sheet1','B1:B1'); %this should change to B2:B2, B3:B3, and B4:B4
Energy=xlsread('Excel_file.xlsx','sheet1','A1:A1'); %this should change to A2:A2, A3:A3, and A4:A4
linear constraints:
const1 = 2*x(1) + 3*Mass*x(2);
const2 = 4*Energy*x(1) + 4*Mass*x(2);
A=[const1;const2];
b=[2;1];
nonlcon=@(x)NonLinConst (x(1), x(2));
fun = @(x)objective_function(x(1), x(2));
ub,lb,x0,etc...
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);
Objective_function:
function optfun = objective_function (y,z)
optfun=32*Energy*y - Mass*z-45;
end
NonLinConst:
function [c,ceq] = NonLinConst (y,z)
c = 2*Energy*y^2-10;
ceq = Mass*z^2-120;
end
What I need now is that the optimization runs, lets say 4 times : first with Mass1 and Energy1, then Mass2 and Energy2.. until Mass4 with Energy4...then save the results in a matrix and find the minimum of them all.
The biggest concern here is the iteration... I´ve tried with for loops for days but still haven´t got it !
I would appreciate any help! Thanks

5 Comments

You can try loading Mass and Energy from Excel at once (if they do not change between iterations). Then if you organise them into an array where each of the columns (or rows, however you want it) would represent data for each iteration.
%Data=Xlsread()
For i=1:num_times
% generate constraints from Data
% run optimisation
% save results
end
% Find min of obtained results
Sorry, I am on the phone, so it doesn't look tidy
Hi Mario,
Thanks for you answer, I already did that and created, as you say a vector for each coefficient... then I created a for loop as you put it in there, so that each time it would just call a different row of the vector.. But my main problem is knowing how to call all of the functions (objective function and nonlinear constraints) in the for loop and make them change with each iteration, given that they look like this in the main code:
nonlcon=@(x)NonLinConst (x(1), x(2));
fun = @(x)objective_function(x(1), x(2));
and inside of each function the respective Mass and Energy coefficients appear. Did I explain myself correctly?
for i=1:length(Mass_vector)
%I call, for example, the objective function:
fun = @(x)objective_function(x(1), x(2));
how can I make this vary as I want it to?
fun = @(x) objective_function(x(1),x(2),Mass_vector(i),Energy_vector(i))
Same for the other functions.
Hi Torsten,
thanks, I will try that! Should I change something too in the files of the objective function and the nonlinear constraints? or should they remain the same?
function optfun = objective_function(y,z,Mass,Energy)
...
end
Same for the other functions.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2019b

Asked:

on 10 Apr 2020

Commented:

on 11 Apr 2020

Community Treasure Hunt

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

Start Hunting!