How to plot objective function with optimal solution

A=[1 3;-1 2];
Aeq=[2 1];
beq=4;
b=[5;-1];
f=[-1 -5];
lb=[0;0];
[x,Z]=linprog(f,A,b,Aeq,beq,lb);
fprintf('Optimal Solution:\n');
fprintf('x1 = %.4f\n', x(1));
fprintf('x2 = %.4f\n', x(2));
fprintf('Optimal Objective Function Value (Z) = %.4f\n', -Z);
plotting command?

Answers (2)

Perhaps as follows?
A=[1 3;-1 2];
Aeq=[2 1];
beq=4;
b=[5;-1];
f=[-1 -5];
lb=[0;0];
[x,Z]=linprog(f,A,b,Aeq,beq,lb);
Optimal solution found.
fprintf('Optimal Solution:\n');
Optimal Solution:
fprintf('x1 = %.4f\n', x(1));
x1 = 1.8000
fprintf('x2 = %.4f\n', x(2));
x2 = 0.4000
fprintf('Optimal Objective Function Value (Z) = %.4f\n', -Z);
Optimal Objective Function Value (Z) = 3.8000
fsurf(@(x1,x2) f(1)*x1+f(2)*x2,'EdgeColor','none','FaceAlpha',0.3); hold on
scatter3(x(1),x(2),Z,'r','filled','SizeData',80); hold off; xlabel x1; ylabel x2; zlabel Z
legend Objective Optimum
lb = [lower_bound_x1, lower_bound_x2]; % Replace with your actual lower bounds
% Define the range for x1 and x2. Adjust the range according to your constraints.
x1_range = linspace(lb(1), b(1), 100);
x2_range = linspace(lb(2), b(2), 100);
% Create a meshgrid for the ranges
[X1, X2] = meshgrid(x1_range, x2_range);
% Evaluate the objective function over the grid
Z = -X1 - 5*X2; % Since your objective function is f = [-1; -5]
% Plot the objective function
figure;
contour(X1, X2, Z, 50); % Creates a contour plot with 50 levels
hold on;
% Mark the optimal solution on the plot
plot(x(1), x(2), 'r*', 'MarkerSize', 10);
% Optionally, you can add labels and title
xlabel('x1');
ylabel('x2');
title('Objective Function with Optimal Solution');
colorbar; % To show the objective function value scale on the side
hold off;
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

1 Comment

How exactly does plotting a contour plot make sense for a linear programming problem?
And your code does not work, as the point does not lie on the plane -
A=[1 3;-1 2];
Aeq=[2 1];
beq=4;
b=[5;-1];
f=[-1 -5];
lb=[0;0];
[x,Z]=linprog(f,A,b,Aeq,beq,lb);
Optimal solution found.
fprintf('Optimal Solution:\n');
Optimal Solution:
fprintf('x1 = %.4f\n', x(1));
x1 = 1.8000
fprintf('x2 = %.4f\n', x(2));
x2 = 0.4000
fprintf('Optimal Objective Function Value (Z) = %.4f\n', -Z);
Optimal Objective Function Value (Z) = 3.8000
% Define the range for x1 and x2. Adjust the range according to your constraints.
x1_range = linspace(lb(1), b(1), 100);
x2_range = linspace(lb(2), b(2), 100);
% Create a meshgrid for the ranges
[X1, X2] = meshgrid(x1_range, x2_range);
% Evaluate the objective function over the grid
Z = -X1 - 5*X2; % Since your objective function is f = [-1; -5]
% Plot the objective function
figure;
contour(X1, X2, Z, 50); % Creates a contour plot with 50 levels
hold on;
view(3)
% Mark the optimal solution on the plot
plot(x(1), x(2), 'r*', 'MarkerSize', 10);
% Optionally, you can add labels and title
xlabel('x1');
ylabel('x2');
title('Objective Function with Optimal Solution');
colorbar; % To show the objective function value scale on the side
hold off;

Sign in to comment.

Products

Release

R2023b

Tags

Asked:

on 11 Jan 2024

Commented:

on 11 Jan 2024

Community Treasure Hunt

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

Start Hunting!