fmincon get the wrong answer
i have a optimization problem that fmincon get the wrong answer, there are a demand power(2) that can be generate or buy with different price, the codes are as follow:
a = 0.005;
b = 6;
c = 100;
% Define the cost function for generating energy (quadratic cost function) for the current microgrid
Cg_i = @(Ec, a, b, c) a * Ec^2 + b * Ec + c;
% Given prices
prices = [33, 26];
mprice = 27; % Given value of mprice
% Define the cost function for buying energy
Cb_i = @(Ec, price) price * Ec;
% Given total energy constraint
Ec_total = 2; % Given value of Ec_total
% Define the objective function
objective = @(X) Cg_i(X(1), a, b, c) + Cb_i(X(2), prices(1)) + Cb_i(X(3), prices(2)) + Cb_i(X(4), mprice);
% Initial guess for Ec_g, Ec1, Ec2
initial_guess = [Ec_total / 3, Ec_total / 3, Ec_total / 3, Ec_total / 3];
% initial_guess = [0, 0, Ec_total, 0];
% Linear equality constraint for the sum of Ec_g, Ec1, Ec2
Aeq = [1, 1, 1, 1];
beq = Ec_total;
% Options for fmincon
options = optimoptions(‘fmincon’, ‘Display’, ‘iter’);
% Define solver options
% Call fmincon with specified options
X = fmincon(objective, initial_guess, [], [], Aeq, beq, [0, 0, 0,0], [], [], options)
the matlab answer is X=[2 0 0 0]; that means generat 2 and no buy, that obviously is wrong because cost of buy with price 26 is 26*2=52 while generating cost is 112!i have a optimization problem that fmincon get the wrong answer, there are a demand power(2) that can be generate or buy with different price, the codes are as follow:
a = 0.005;
b = 6;
c = 100;
% Define the cost function for generating energy (quadratic cost function) for the current microgrid
Cg_i = @(Ec, a, b, c) a * Ec^2 + b * Ec + c;
% Given prices
prices = [33, 26];
mprice = 27; % Given value of mprice
% Define the cost function for buying energy
Cb_i = @(Ec, price) price * Ec;
% Given total energy constraint
Ec_total = 2; % Given value of Ec_total
% Define the objective function
objective = @(X) Cg_i(X(1), a, b, c) + Cb_i(X(2), prices(1)) + Cb_i(X(3), prices(2)) + Cb_i(X(4), mprice);
% Initial guess for Ec_g, Ec1, Ec2
initial_guess = [Ec_total / 3, Ec_total / 3, Ec_total / 3, Ec_total / 3];
% initial_guess = [0, 0, Ec_total, 0];
% Linear equality constraint for the sum of Ec_g, Ec1, Ec2
Aeq = [1, 1, 1, 1];
beq = Ec_total;
% Options for fmincon
options = optimoptions(‘fmincon’, ‘Display’, ‘iter’);
% Define solver options
% Call fmincon with specified options
X = fmincon(objective, initial_guess, [], [], Aeq, beq, [0, 0, 0,0], [], [], options)
the matlab answer is X=[2 0 0 0]; that means generat 2 and no buy, that obviously is wrong because cost of buy with price 26 is 26*2=52 while generating cost is 112! i have a optimization problem that fmincon get the wrong answer, there are a demand power(2) that can be generate or buy with different price, the codes are as follow:
a = 0.005;
b = 6;
c = 100;
% Define the cost function for generating energy (quadratic cost function) for the current microgrid
Cg_i = @(Ec, a, b, c) a * Ec^2 + b * Ec + c;
% Given prices
prices = [33, 26];
mprice = 27; % Given value of mprice
% Define the cost function for buying energy
Cb_i = @(Ec, price) price * Ec;
% Given total energy constraint
Ec_total = 2; % Given value of Ec_total
% Define the objective function
objective = @(X) Cg_i(X(1), a, b, c) + Cb_i(X(2), prices(1)) + Cb_i(X(3), prices(2)) + Cb_i(X(4), mprice);
% Initial guess for Ec_g, Ec1, Ec2
initial_guess = [Ec_total / 3, Ec_total / 3, Ec_total / 3, Ec_total / 3];
% initial_guess = [0, 0, Ec_total, 0];
% Linear equality constraint for the sum of Ec_g, Ec1, Ec2
Aeq = [1, 1, 1, 1];
beq = Ec_total;
% Options for fmincon
options = optimoptions(‘fmincon’, ‘Display’, ‘iter’);
% Define solver options
% Call fmincon with specified options
X = fmincon(objective, initial_guess, [], [], Aeq, beq, [0, 0, 0,0], [], [], options)
the matlab answer is X=[2 0 0 0]; that means generat 2 and no buy, that obviously is wrong because cost of buy with price 26 is 26*2=52 while generating cost is 112! fmincon MATLAB Answers — New Questions