How do i plot the function as a mesh in the constraint area only?
6 views (last 30 days)
Show older comments
Samson David Puthenpeedika
on 21 Nov 2021
Given is the following conditions:
Using linprog()
Maximize: f= 30*X1 + 10*X2;
subject to : 6*X1 + 3*X2 <= 40;
3*X1 - X2<=0 ;
X1 + 0.25*X2 <= 4;
X1>=0
X2>=0
Display the plot
-the function as a mesh in the constraint area only (using mesh())
below is my approach ...please correct me.
f = [-30 -10];
A=[6 3;3 -1;1 0.25];
b=[40 0 4];
lb = zeros(2,1);
ub = [];
Aeq = [];
beq = [];
tic
options = optimoptions('linprog','Algorithm','dual-simplex','Display','iter');
[sol,fval,exitflag,output] = linprog(f,A,b,Aeq,beq,lb,ub,options);
toc
[X1 X2]=meshgrid(0:0.2:50,0:0.2:50);
cons1= 6*X1 + 3*X2 <= 40;
cons2= 3*X1 - X2<=0 ;
cons3= X1 + 0.25*X2 <= 4;
X1(~cons1)= NaN;
X1(~cons2)= NaN;
X1(~cons3)= NaN;
X2(~cons1)= NaN;
X2(~cons2)= NaN;
X2(~cons3)= NaN;
f= 30*X1 + 10*X2;
mesh(X1,X2,f);
0 Comments
Accepted Answer
yanqi liu
on 22 Nov 2021
sir,which is the question?
clc; clear all; close all;
f = [-30 -10];
A=[6 3;3 -1;1 0.25];
b=[40 0 4];
lb = zeros(2,1);
ub = [];
Aeq = [];
beq = [];
tic
options = optimoptions('linprog','Algorithm','dual-simplex','Display','iter');
[sol,fval,exitflag,output] = linprog(f,A,b,Aeq,beq,lb,ub,options);
toc
[X1, X2]=meshgrid(0:0.1:20,0:0.1:20);
cons1= 6*X1 + 3*X2 <= 40;
cons2= 3*X1 - X2<=0 ;
cons3= X1 + 0.25*X2 <= 4;
X1(~(cons1&cons2&cons1))= NaN;
X2(~(cons1&cons2&cons1))= NaN;
f= 30*X1 + 10*X2;
mesh(X1,X2,-f);
xlabel('X1')
ylabel('X2')
zlabel('f')
hold on;
plot3(sol(1),sol(2),fval,'ro','MarkerFaceColor', 'r');
0 Comments
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
