Simplex Method - 18BCN7125
Simplex Method - 18BCN7125
( Slot - L39+L40 )
SIMPLEX METHOD
K.Vamsi krishna
18BCN7125
Q.
A firm that produces two products has limitations on its machine and labour
time.There are 240 hours of machine time and 300 hours of labour time
available duringthe production period. A unit of the first product requires 4
hours of machine timeand 3 hours of labour time. The second product
takes 6 and 8 hours of machineand labour time, respectively. A unit of
product 1 and product 2 generates profits of10.00and12.00, respectively.
The economic problem is to find the levels of products1 and 2 that
maximize profit and allocate economic resources efficiently.
Mathematical Formulation:
Maximize subject to -
10x1+ 12x2
4x1+ 6x2 ≤ 240
3x1+ 8x2 ≤ 300
x1 ≤ 0 and x2 ≥ 0.
Code:
clc
clear all
%objf=input('Enter the objective function matrix');
%stc=input('Enter the basis matrix');
objf=[10 12 0 0];
stc=[240 4 3 1 0; 300 6 8 0 1];
delj=zeros(size(objf));
minr=zeros(size(stc(:,1)));
cb=zeros(size(stc(:,1)));
bv=zeros(size(stc(:,1)));
ite=1;
%Filling the Basic variable Coloumn
for i=1:length(bv)
bv(i)=length(objf)-length(bv)+i;
end
disp('Iteration No. =');
disp(ite);
disp('Basic Variables =');
disp(bv);
disp('cb =');
disp(cb);
%cj;
%zma=cb.*xb;
disp('Basis Matrix =
') disp(stc)
fprintf('reg no 18bcn7004');
bool=1;
while(bool>0)%Run The loop till all the delj values are positive
%Computing Z
z=sum(transpose(cb)*stc(:,1))
bool=0;
for i=1:length(objf)
%Computing delj
delj(i)=sum(transpose(cb)*stc(:,i+1))-objf(i);
if(delj(i)<0)
bool=bool+1;
end
end
min=0;
pos=0;
%Findingthe minimum value indelj
for i=1:length(delj)
if(min>delj(i)
)
min = delj(i);
pos=i;
end
end
pos=pos+1;
disp('Entering Coloumn= ');
disp(pos-1);
xb=stc(:,1)
xj=stc(:,pos)
minr=xb;
%computing minr coloumn
for i=1:length(minr)
minr(i)=xb(i)/xj(i);
end
minr
min=minr(1);
x=-1;
%Determining the entering row
for i=1:length(minr)
if(min<0)
min=minr(i);
pos1=i;
continue;
end
if(min>=minr(i)&&minr(i)>=0)
min=minr(i);
pos1=i;
x=0;
end
end
disp('Minimun Value= ');
disp(min)
if(x==-1&&minr(length(minr))<0)
bool=1;
break;
end
disp('Entering Row= ');
disp(pos1)
disp('Pivot element = ');
disp(stc(pos1,pos))
%Evaluating the basis matrix
for i=1:length(stc(:,1))
if(pos1==i)
k=stc(pos1,pos);
for j=1:length(stc)
stc(i,j)=stc(i,j)/k
;
en
d
en
for i=1:length(stc(:,1))
if(i~=pos1)
k=stc(i,pos);
for j=1:length(stc)
stc(i,j)=stc(i,j)-(k*stc(pos1,j));
en
d
en
d
ite=ite+1;
en
disp('Iteration No. =');
disp(ite);
bv(pos1)=pos-1;
a=objf(1,pos-1);
cb(pos1,1)=a;
disp('Basic Variables');
disp(bv);
disp('cb =');
disp(cb);
disp('Basis Matrix=
') disp(stc)
bool=0;
for i=1:length(objf)
delj(i)=sum(transpose(cb)*stc(:,i+1))-objf(i);
if(delj(i)<0)
bool=bool+1;
en
d
en
disp('delj= ');
disp(delj);
%break;
end
if(bool==0)
z=sum(transpose(cb)*stc(:,1));
disp('Solution Found Max Z = ');
disp(z);
xb=stc(:,1);
for i=1:length(bv)
if(bv(i)<=length(objf)-length(bv))
fprintf('X%d = %.2f ',bv(i),xb(i));
end
end
else
disp('Unbounded Solution');
disp(z);
end
Output:
Iteration No. =
1
Basic Variables =
3
4
cb =
0
0
Basis Matrix =
240 4 3 1 0
300 6 8 0 1
reg no 18bcn7004
z =
Entering Coloumn=
2
xb =
240
300
xj =
3
8
minr =
80.0000
37.5000
Minimun Value=
37.5000
Entering Row=
2
Pivot element =
8
Iteration No. =
2
Basic Variables
3
2
cb =
0
1
2
Basis Matrix=
127.5000 1.7500 0 1.0000 -0.3750
37.5000 0.7500 1.0000 0 0.1250
delj
=
-1.0000 0 0 1.5000
z =
45
0
Entering Coloumn=
1
xb =
127.5000
37.5000
xj =
1.7500
0.7500
minr =
72.8571
50.0000
Minimun Value=
50
Entering Row=
2
Pivot element =
0.7500
Iteration No. =
3
Basic Variables
3
1
cb =
0
10
Basis Matrix=
40.0000 0 -2.3333 1.0000 -0.6667
50.0000 1.0000 1.3333 0 0.1667
delj=
0 1.3333 0 1.6667
X1 = 50.00
Q.
A manufacturer produces two products A and B. Both the products are
pro-cessed on two different machines. The available capacity of first
machine is 12hours and that of second machine is 9 hours per day. Each
unit of product Arequires 3 hours on both machines and each unit of
product B requires 2 hours on first machine and 1 hour on second machine.
Each unit of product A is soldat Rs 7 profit and B at a profit of Rs 4. Find
the production level per day formaximum profit.
Mathematical Formulation:
Maximize subject to -
7x1+ 4x2
3x1+ 2x2 ≤ 12
3x1+ 1x2 ≤ 9
x1 ≤ 0 and x2 ≥ 0.
Code:
clc
clear all
%objf=input('Enter the objective function matrix');
%stc=input('Enter the basis matrix');
objf=[7 4 0 0];
stc=[12 3 3 1 0; 9 2 1 0 1];
delj=zeros(size(objf));
minr=zeros(size(stc(:,1)));
cb=zeros(size(stc(:,1)));
bv=zeros(size(stc(:,1)));
ite=1;
%Filling the Basic variable Coloumn
for i=1:length(bv)
bv(i)=length(objf)-length(bv)+i;
end
disp('Iteration No. =');
disp(ite);
disp('Basic Variables =');
disp(bv);
disp('cb =');
disp(cb);
%cj;
%zma=cb.*xb;
disp('Basis Matrix =
') disp(stc)
fprintf('reg no 18bcn7004');
bool=1;
while(bool>0)%Run The loop till all the delj values are positive
%Computing Z
z=sum(transpose(cb)*stc(:,1))
bool=0;
for i=1:length(objf)
%Computing delj
delj(i)=sum(transpose(cb)*stc(:,i+1))-objf(i);
if(delj(i)<0)
bool=bool+1;
end
end
min=0;
pos=0;
%Findingthe minimum value indelj
for i=1:length(delj)
if(min>delj(i))
min = delj(i);
pos=i;
end
end
pos=pos+1;
disp('Entering Coloumn= ');
disp(pos-1);
xb=stc(:,1)
xj=stc(:,pos)
minr=xb;
%computing minr coloumn
for
i=1:length(minr)
minr(i)=xb(i)/xj(i)
end
minr
min=minr(1);
x=-1;
%Determining the entering row
for i=1:length(minr)
if(min<0)
min=minr(i);
pos1=i;
continue;
end
if(min>=minr(i)&&minr(i)>=0)
min=minr(i);
pos1=i;
x=0;
end
end
disp('Minimun Value= ');
disp(min)
if(x==-1&&minr(length(minr))<0)
bool=1;
break;
end
disp('Entering Row= ');
disp(pos1)
disp('Pivot element = ');
disp(stc(pos1,pos))
%Evaluating the basis matrix
for i=1:length(stc(:,1))
if(pos1==i)
k=stc(pos1,pos);
for j=1:length(stc)
stc(i,j)=stc(i,j)/k;
end
end
end
for i=1:length(stc(:,1))
if(i~=pos1)
k=stc(i,pos);
for j=1:length(stc)
stc(i,j)=stc(i,j)-(k*stc(pos1,j));
end
end
end
ite=ite+1;
disp('Iteration No. =');
disp(ite);
bv(pos1)=pos-1;
a=objf(1,pos-
cb(pos1,1)=a;
disp('Basic Variables');
disp(bv);
disp('cb =');
disp(cb);
disp('Basis Matrix=
') disp(stc)
bool=0;
for i=1:length(objf)
delj(i)=sum(transpose(cb)*stc(:,i+1))-objf(i);
if(delj(i)<0)
bool=bool+1;
end
end
disp('delj=
');
disp(delj);
%break;
end
if(bool==0)
z=sum(transpose(cb)*stc(:,1));
disp('Solution Found Max Z = ');
disp(z);
xb=stc(:,1);
for i=1:length(bv)
if(bv(i)<=length(objf)-length(bv))
fprintf('X%d = %.2f ',bv(i),xb(i));
end
end
else
disp('Unbounded Solution');
disp(z);
end
Output:
Iteration No. =
1
Basic Variables =
3
4
cb =
0
0
Basis Matrix =
12 3 3 1 0
9 2 1 0 1
reg no 18bcn7004
z =
Entering Coloumn=
1
xb =
12
9
xj =
3
2
minr =
4.0000
4.5000
Minimun Value=
4
Entering Row=
1
Pivot element =
3
Iteration No. =
2
Basic Variables
1
4
cb =
7
0
Basis Matrix=
4.0000 1.0000 1.0000 0.3333 0
1.0000 0 -1.0000 -0.6667 1.0000
delj=
0 3.0000 2.3333 0
Solution Found Max Z =
28
X1 = 4.00
Q.
Maximum Selling in each city:A businessman can travel
to city A, city B,or city C. It is 122 miles to city
A, 237 miles to city B, and 307 miles to cityC. He
can travel up to 3000 miles. Dining and other
expenses are $95 in cityA, $130 in city B, and $180
in city C. Her expense account allows her to
spend$2000. A trip to city A will generate $800 in
sales, while a trip to city B willgenerate $1300 and
a trip to city C will generate $1800. How many trips
shouldhe make to each city to maximize sales? Write
the initial simplex tableau.
Code:
clc
clear all
%objf=input('Enter the objective function matrix');
%stc=input('Enter the basis matrix');
objf=[800 1300 1800 0 0];
stc=[3000 122 237 307 1 0 ;2000 95 130 180 0 1];
delj=zeros(size(objf));
minr=zeros(size(stc(:,1)));
cb=zeros(size(stc(:,1)));
bv=zeros(size(stc(:,1)));
ite=1;
%Filling the Basic variable Coloumn
for i=1:length(bv)
bv(i)=length(objf)-length(bv)+i;
end
disp('Iteration No. =');
disp(ite);
disp('Basic Variables =');
disp(bv);
disp('cb =');
disp(cb);
%cj;
%zma=cb.*xb;
disp('Basis Matrix =
') disp(stc)
fprintf('reg no 18bcn7004');
bool=1;
while(bool>0)%Run The loop till all the delj values are positive
%Computing Z
z=sum(transpose(cb)*stc(:,1))
bool=0;
for i=1:length(objf)
%Computing delj
delj(i)=sum(transpose(cb)*stc(:,i+1))-objf(i);
if(delj(i)<0)
bool=bool+1;
end
end
min=0;
pos=0;
%Findingthe minimum value indelj
for i=1:length(delj)
if(min>delj(i))
min = delj(i);
pos=i;
end
end
pos=pos+1;
disp('Entering Coloumn= ');
disp(pos-1);
xb=stc(:,1)
xj=stc(:,pos)
minr=xb;
%computing minr coloumn
for i=1:length(minr)
minr(i)=xb(i)/xj(i);
end
minr
min=minr(1);
x=-1;
%Determining the entering row
for i=1:length(minr)
if(min<0)
min=minr(i);
pos1=i;
continue;
end
if(min>=minr(i)&&minr(i)>=0)
min=minr(i);
pos1=i;
x=0;
end
end
disp('Minimun Value= ');
disp(min)
if(x==-1&&minr(length(minr))<0)
bool=1;
break;
end
disp('Entering Row= ');
disp(pos1)
disp('Pivot element = ');
disp(stc(pos1,pos))
%Evaluating the basis matrix
for i=1:length(stc(:,1))
if(pos1==i)
k=stc(pos1,pos);
for j=1:length(stc)
stc(i,j)=stc(i,j)/k;
end
end
end
for i=1:length(stc(:,1))
if(i~=pos1)
k=stc(i,pos);
for j=1:length(stc)
stc(i,j)=stc(i,j)-(k*stc(pos1,j));
en
d
en
end
ite=ite+1;
disp('Iteration No.
='); disp(ite);
bv(pos1)=pos-1;
a=objf(1,pos-1);
cb(pos1,1)=a;
disp('Basic Variables');
disp(bv);
disp('cb =');
disp(cb);
disp('Basis Matrix=
') disp(stc)
bool=0;
for i=1:length(objf)
delj(i)=sum(transpose(cb)*stc(:,i+1))-objf(i);
if(delj(i)<0)
bool=bool+1;
end
end
disp('delj= ');
disp(delj);
%break;
end
if(bool==0)
z=sum(transpose(cb)*stc(:,1));
disp('Solution Found Max Z = ');
disp(z);
xb=stc(:,1);
for i=1:length(bv)
if(bv(i)<=length(objf)-length(bv))
fprintf('X%d = %.2f ',bv(i),xb(i));
end
end
else
disp('Unbounded Solution');
disp(z);
end
Output:
Iteration No. =
1
Basic Variables =
4
5
cb =
0
0
Basis Matrix =
3000 122 237 307 1 0
2000 95 130 180 0 1
reg no 18bcn7004
z =
Entering Coloumn=
3
xb =
3000
2000
xj =
307
180
minr =
9.7720
11.1111
Minimun Value=
9.7720
Entering Row=
1
Pivot element =
307
Iteration No. =
2
Basic Variables
3
5
cb =
1800
0
Basis Matrix=
9.7720 0.3974 0.7720 1.0000 0.0033 0
241.0423 23.4691 -8.9577 0 -0.5863 1.0000
delj=
-84.6906 89.5765 0 5.8632 0
z =
1.7590e+04
Entering Coloumn=
1
xb =
9.7720
241.0423
xj =
0.3974
23.4691
minr =
24.5902
10.2706
Minimun Value=
10.2706
Entering Row=
2
Pivot element =
23.4691
Iteration No. =
3
Basic Variables
3
1
cb =
1800
800
Basis Matrix=
5.6905 0 0.9237 1.0000 0.0132 -0.0169
10.2706 1.0000 -0.3817 0 -0.0250 0.0426
delj=
0 57.2519 0 3.7474 3.6086
X3 = 5.69 X1 = 10.27
THE END