MATLAB Answers

# Linear programming code not showing the solution

37 views (last 30 days)
Hmm! on 27 Feb 2021
Commented: Hmm! on 28 Feb 2021
I extracted the following code from an online pdf that solves basic feasible solution of linear programming problems.
function vert = feassol(A, b)
% Basic feasible solutions vert to the system of constraints
% Ax = b, x >= 0.
% They are stored in columns of the matrix vert.
[m, n] = size(A);
warning off
b = b(:);
vert = [];
if (n >= m)
t = nchoosek(1:n,m);
nv = nchoosek(n,m);
for i = 1:nv
y = zeros(n,1);
x = A(:,t(i,:))\b;
if all(x >= 0 & (x ~= inf & x ~= -inf))
y(t (i, :)) = x;
end
end
else
error('Nuber of equations is greater than th neumber of variables.')
end
if ~isempty(vert)
vert = delcols(vert);
else
vert = [];
end
end
To test the code, the author used the system
A = [1 1 1 0; 0 1 0 1];
b = [6; 3];
and obtain the results
vert = feassol(A, b)
vert =
0 0 3 6
0 3 3 0
6 3 0 0
3 0 0 3
But whenever I run the code, I get
>> vert = feassol(A,b)
vert =
[]
What am I not doing right with the code? Any help will be much appreciated. Thanks in advance!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (2)

Walter Roberson on 27 Feb 2021
Where do you assign something nonempty to vert?
Why do you calculate y since you never use it?
##### 9 CommentsShowHide 8 older comments
Hmm! on 28 Feb 2021
Sorry. It is another written function that I had to called. I have just figure it out.

Sign in to comment.

Matt J on 27 Feb 2021
Edited: Matt J on 27 Feb 2021
As long as your feasible set is bounded, you can use this FEX submission instead,
A = [1 1 1 0; 0 1 0 1];
b = [6; 3];
[args{1:4}]=addBounds([],[],A,b,[0;0;0;0]);
vert=lcon2vert(args{:}).'
for which I get the result,
vert =
6.0000 3.0000 -0.0000 -0.0000
0.0000 3.0000 3.0000 -0.0000
-0.0000 0 3.0000 6.0000
3.0000 0 0 3.0000
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!