How do i plot the function as a mesh in the constraint area only?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Samson David Puthenpeedika
le 21 Nov 2021
Réponse apportée : yanqi liu
le 22 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 commentaires
Réponse acceptée
yanqi liu
le 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

