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?
1 件のコメント
Dyuman Joshi
2024 年 1 月 11 日
It's not fully clear to me what you want to plot.
回答 (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);
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);
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
Hassaan
2024 年 1 月 11 日
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 件のコメント
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);
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);
% 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;
カテゴリ
ヘルプ センター および File Exchange で Get Started with Problem-Based Optimization and Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

