A simple implementation of simplex algorithm using linprog() in MATLAB

clear all;
clc;
m=input('Are there equality constraints?\n If yes type 1. Else type 2\n');
if m==1
    Aeq=input('Enter equality constraints coefficient in matrix form\n');
    beq=input('Enter the bi of equality constraints as column vector\n');
else
    Aeq=[];
    beq=[];
end
f=input('Enter the coefficient of Objective function as column vector\n ');
A=input('Enter the contraint coefficients in matrix form\n');
b=input('Enter bi as column vector\n');
n=length(f);
lb=zeros(n,1);
[x,fval,exitflag,output,lambda] = linprog(f,A,b,Aeq,beq,lb);
disp(x);
disp(fval);

Example Input/Output
maximize z=2x1+3x2+4x3
subject to 
3x1+2x2+x3≤10
2x1+2x2+x3≤15
x1,x2,x3≥0

Input:
f=[-2;-3;-4];%linprog()minimizes the objective function
A=[3 2 1;2 2 1];
b=[10;15];
output:
Optimization terminated.
    x1=0.0000
    x2=0.0000
   x3=10.0000


 z= -40.0000
Hence, Maximum value of z= 40.



Comments