Linear programming code not showing the solution

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!

답변 (2개)

Walter Roberson
Walter Roberson 2021년 2월 27일

0 개 추천

Where do you assign something nonempty to vert?
Why do you calculate y since you never use it?

댓글 수: 9

Hmm!
Hmm! 2021년 2월 27일
편집: Hmm! 2021년 2월 27일
You are right. But as I said the author just used the code to illustate the example I gave. The author never made mention of y. The question is a LP program in the form Ax = b, x>=0
Could you help fix this for me?
>> vert = feassol(A,b)
Unrecognized function or variable 'y'.
Error in feassol (line 27)
vert(:, end+1) = y;
%this error showed up after I pasted vert(:, end+1)=y; after "vert = [];"
%where do I paste vert(:, end+1) = y;?
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;
vert(:, end+1) = y;
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
note that you test x>=0 but you also test about negative infinity. A nonnegative value cannot be negative infinity.
Also if you got that error message then you passed in an array with more rows than columns.
Another heckle...
>> feassol
Unrecognized function or variable 'delcols'.
Error in feassol (line 27)
vert = delcols(vert);
% How to do with delcols?
I do not know. You called decols in the code you supplied; no documentation has been supplied as to its purpose or its implementation.
But if I were to guess... I would guess it probably isn't needed.
Sorry. It is another written function that I had to called. I have just figure it out.

댓글을 달려면 로그인하십시오.

Matt J
Matt J 2021년 2월 27일
편집: Matt J 2021년 2월 27일
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

카테고리

도움말 센터File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품

질문:

2021년 2월 27일

댓글:

2021년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by