Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
pls Help with fmincon!

Subject: pls Help with fmincon!

From: Hannes

Date: 21 Jun, 2012 05:51:07

Message: 1 of 5

I am trying to optimize the energy usage in a smart house using fmincon to make it profitable for the user. The house has a solar panel and a wind turbine that generate energy, a consumption profile that show much much energy the house need. The energy storage is used as a battery in the basement and a plugged in Electric Vehicle (EV) during a certain time during the day. The optimization will be for the next 24 hours that i have estimated, based on that i will make a decision what to do the next time step. I have access to data every 10 min, which means 24h*6=144 data. The system can use both the EV and the battery to save or to drain energy from the battery and EV. According a study i made i will keep the state of charge of the battery and EV between 15%-85% and there is a maximum of charging/discharging energy from the battery. In the problem i have included the energy prices for both
sell and buy, since the price to buy energy is higher than selling. I have excluded the EV to make it simple in the beginning and will include it later. The problem is that i get the output of the variables out of bound. The code is presented below!

///////////////////////////////main/////////////////////////////////////

P_demand=10*ones(144,1); % house consumption
P_prod=1*ones(144,1); % house energy production


% equalities
Aeq=zeros(144,432);
Aeq(1,1:3)=[1 1 0];
for i=1:143
Aeq(i+1,(3*i+1):(i+1)*3)=[1 1 0];
end
Beq=P_demand-P_prod;

% xstart
P_market_start=P_demand-P_prod; % count how much to sell and buy energy
P_batt_start=zeros(144,1); % battery effect charge or discharge the battery
Soc_batt_start=0.5*ones(144,1); % start battery level
x0=[P_market_start P_batt_start Soc_batt_start];

% Lb
P_market_Lb=-inf; %x1
P_batt_Lb=-3400; %x2
Soc_batt_Lb=0.15; %x3
Lb=[P_market_Lb; P_batt_Lb; Soc_batt_Lb];

% Ub
P_market_Ub=inf; %x1
P_batt_Ub=1020; %x2
Soc_batt_Ub=0.85; %x3
Ub=[P_market_Ub; P_batt_Ub; Soc_batt_Ub];

%%
% * 'interior-point'
% * 'sqp'
% * 'active-set'
% * 'trust-region-reflective' (default)

options = optimset('Algorithm','interior-point');
[x,fval]= fmincon(@optObjective1,x0,[],[],Aeq,Beq,Lb,Ub,[],options)

/////////////////////////optObjective1/////////////////////////////////////////////

function f = optObjective1(x)

P_market=x(1);
P_batt=x(2);
Soc_batt=x(3);
Soc_batt_start=0.5;


Price_buy=1;
Price_sell=1;

Uoc=5000;
Ri=100;
Q0=24000/Uoc;
dt=600;
c=1;
f1=zeros(1,1);
f2=zeros(1,1);


Ib=(Uoc - sqrt(Uoc^2-4*Ri*P_batt))/2*Ri;

Soc_batt=(-Ib/Q0)*dt + Soc_batt;


if P_market<=0
       f1=P_market;
       f2=0;
else
        f1=0;
        f2=P_market;
end


f=f1.*Price_buy + f2.*Price_sell +c*(Soc_batt-Soc_batt_start)^2;

end
//////////////////////////////////////////////////////////////////

Any advice to change or add something to the script?

            

Subject: pls Help with fmincon!

From: Sargondjani

Date: 21 Jun, 2012 06:07:06

Message: 2 of 5

Please be more precise than "The problem is that i get the output of the variables out of bound."

If there is an error: can you supply the full error code? and the line in which the error occured?
Otherwise explain more detailed.

Subject: pls Help with fmincon!

From: Hannes

Date: 21 Jun, 2012 06:22:09

Message: 3 of 5

"Sargondjani" wrote in message <jrudma$9ml$1@newscl01ah.mathworks.com>...
> Please be more precise than "The problem is that i get the output of the variables out of bound."
>
> If there is an error: can you supply the full error code? and the line in which the error occured?
> Otherwise explain more detailed.

Thanks for the replay!

When i run it i get as below, as you can see x3 should be 0.85 top.
Can i change the function evaluation limit? Or change something! How do i fix this?


Solver stopped prematurely.

fmincon stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 3000 (the default value).


x =

    9.0000 4.5000 4.5000
    0.0000 4.5000 4.5000
    0.5025 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000
    4.5000 4.5000 4.5000
    4.5000 4.5000 4.5000
    9.0000 0 0.5000


fval =

    9.0000

Subject: pls Help with fmincon!

From: Johan Lofberg

Date: 21 Jun, 2012 10:53:07

Message: 4 of 5

There are several issues with the model. To begin with, you have 144*3 decision variables, but the objective only uses three variables. I've mailed you to discuss things in detail (as you are a student from our lab :-)

"Hannes " <samal111@student.liu.se> wrote in message <jrueih$cum$1@newscl01ah.mathworks.com>...
> "Sargondjani" wrote in message <jrudma$9ml$1@newscl01ah.mathworks.com>...
> > Please be more precise than "The problem is that i get the output of the variables out of bound."
> >
> > If there is an error: can you supply the full error code? and the line in which the error occured?
> > Otherwise explain more detailed.
>
> Thanks for the replay!
>
> When i run it i get as below, as you can see x3 should be 0.85 top.
> Can i change the function evaluation limit? Or change something! How do i fix this?
>
>
> Solver stopped prematurely.
>
> fmincon stopped because it exceeded the function evaluation limit,
> options.MaxFunEvals = 3000 (the default value).
>
>
> x =
>
> 9.0000 4.5000 4.5000
> 0.0000 4.5000 4.5000
> 0.5025 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
>
>
> fval =
>
> 9.0000

Subject: pls Help with fmincon!

From: Sargondjani

Date: 21 Jun, 2012 11:31:07

Message: 5 of 5

"Hannes " <samal111@student.liu.se> wrote in message <jrueih$cum$1@newscl01ah.mathworks.com>...
> "Sargondjani" wrote in message <jrudma$9ml$1@newscl01ah.mathworks.com>...
> > Please be more precise than "The problem is that i get the output of the variables out of bound."
> >
> > If there is an error: can you supply the full error code? and the line in which the error occured?
> > Otherwise explain more detailed.
>
> Thanks for the replay!
>
> When i run it i get as below, as you can see x3 should be 0.85 top.
> Can i change the function evaluation limit? Or change something! How do i fix this?
>
>
> Solver stopped prematurely.
>
> fmincon stopped because it exceeded the function evaluation limit,
> options.MaxFunEvals = 3000 (the default value).
>
>
> x =
>
> 9.0000 4.5000 4.5000
> 0.0000 4.5000 4.5000
> 0.5025 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
> 4.5000 4.5000 4.5000
> 4.5000 4.5000 4.5000
> 9.0000 0 0.5000
>
>
> fval =
>
> 9.0000

the problem is that fmincon does not do what you want it to do...

the objective function only calculates f for the first period. and even there x(1) and x(2) do not matter, because P_buy=P_sell and sum(f1+f2)=P_market, so f1.*Price_buy + f2.*Price_sell will not change when x(1) and x(2) are changed.

SO basically fmincon only tries to minimize the last part of the equations PLUS the constraints. the solution for the last part is x(3)=0.5.

Anyway: a loop through time is missing!!! Also the variables that you are trying to optimize for (x) has to be a vector, not a matrix. So you have to change x into a vector.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us