linprog

5 Ansichten (letzte 30 Tage)
Nina
Nina am 13 Sep. 2011
Bearbeitet: John D'Errico am 3 Apr. 2025
Dear all, I am dealing with linprog function and have some difficulties with it. The function I need to optimize is: f=1.44-0.05x-0.04y, subject to x>=0, y>=0, y>= -x +10, y<=-x+12 , y<=(1/3)x.
Where do I include the constant value from the objective function 1.44?
Thanks a lot.
Nina
  1 Kommentar
Dimpal
Dimpal am 3 Apr. 2025
% Define the objective function coefficients (excluding the constant)
f_coeffs = [-0.05, -0.04];
% Define the inequality constraints (A*x <= b)
A = [1, 1; % y <= -x + 12 => x + y <= 12
-1, -1; % y >= -x + 10 => -x - y <= -10
-1, 3]; % y <= (1/3)x => -x + 3y <= 0
b = [12, -10, 0];
% Define the lower bounds for x and y (x >= 0, y >= 0)
lb = [0, 0];
% Solve the linear programming problem
[x, fval] = linprog(f_coeffs, A, b, [], [], lb);
% Calculate the actual objective function value (including the constant)
f_actual = 1.44 + fval;
% Display the results
fprintf('Optimal solution:\n');
fprintf('x = %.4f\n', x(1));
fprintf('y = %.4f\n', x(2));
fprintf('Objective function value = %.4f\n', f_actual);
% Plotting the feasible region and optimal solution
% 1. Define the line for the constraints
x_plot = 0:15; % Set the range for x
y1 = -x_plot + 12;
y2 = -x_plot + 10;
y3 = (1/3) * x_plot;
% 2. Plot the lines
figure;
plot(x_plot, y1, 'r-', 'DisplayName', 'y <= -x + 12');
hold on;
plot(x_plot, y2, 'b-', 'DisplayName', 'y >= -x + 10');
plot(x_plot, y3, 'g-', 'DisplayName', 'y <= (1/3)x');
% 3. Fill the feasible region
x_fill = [0, 6, 12, 10,0];
y_fill = [0, 2, 0, 0, 10];
fill(x_fill,y_fill,'y','FaceAlpha',0.3,'DisplayName','Feasible Region');
% 4. Plot the optimal point
plot(x(1), x(2), 'ko', 'MarkerSize', 8, 'MarkerFaceColor', 'k', 'DisplayName', 'Optimal Point');
% 5. Add labels and legend
xlabel('x');
ylabel('y');
title('Feasible Region and Optimal Solution');
legend('show');
grid on;
axis([0, 15, 0, 15]); % Adjust axis limits as needed
hold off;

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Andrei Bobrov
Andrei Bobrov am 13 Sep. 2011
Removed first variant (17:20 MDT[09:20 EDT])
Hi Nina! Adjustment for the right answer (ADD 13.09.2011 14:20 MDT [06:20EDT])
f = [ 0.05; 0.04];
A = [-1 -1; 1 1];
b = [-10; 12;];
Aeq = [-1/3 1];
beq = 0;
lb = [0; 0;];
[x,fval,exitflag,output,lambda] = linprog(f,A,b,Aeq,beq,lb)
ADD2 13.09.2011 (14:25 MDT [06:25EDT])
f = [ 0.05; 0.04;];
A = [-1 -1 ; 1 1 ; -1/3 1];
b = [-10; 12; 0];
lb = [0; 0;];
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],lb)
  1 Kommentar
Nina
Nina am 13 Sep. 2011
Verschoben: John D'Errico am 3 Apr. 2025
Thanks Andrei, but it's wrong :(
The right solution should be 7.5 for x and 2.5 for y.
Why do you put the whole equation on the left side (in matrix A) and don't leavi it for b?

Melden Sie sich an, um zu kommentieren.


Aurele Turnes
Aurele Turnes am 13 Nov. 2017
Bearbeitet: Aurele Turnes am 13 Nov. 2017
If you have R2017b, you can use the new problem-base approach. It will take care of the constant value for you: https://www.mathworks.com/help/optim/problem-based-lp-milp.html

John D'Errico
John D'Errico am 3 Apr. 2025
Bearbeitet: John D'Errico am 3 Apr. 2025
Think about it. Do you need that constant at all? (NO.)
LINPROG finds a location (in your case, in terms of x and y) that minimizes an objective. Does adding a constant change the location of that minimum? Of course not. Adding any constant to the objective is irrelevant to the location of the minimum. As such, don't worry about the constant. LINPROG does not care.
This is a common idea in any optimization problem. In terms of a constraint, yes, a constant term matters. But the objective can be arbitrarily shifted by any constant. If it bothers you that the final function value returned does not have that constant in it, then add it in at the end. And, finally, IF you really, really, desperately need that constant in there, use of a problem based solution will allow that.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by