Write a Matlab function that will return the row canonical form for any given row echelon matrix; your function should return an appropriate error if the input matrix is NOT in echelon form.
function [ val mat ] = simplex_min(Q,R )
Q =[7 9 31 1 6 1 0; 4 3 8 1 7 0 10; 9 9 8 1 2 7 1]
R = [0 0 0]
[ na ma] = size(Q);
[ nc mc] = size(R);
if nc ~= 1
disp(' check the given objective ')
return
end
if ma-1 ~= mc
disp('Check the given objective function')
return
end
P = [ Q(:,1:ma-1) eye(na) Q(:,ma) ];
P(na+1,:) = zeros(1,na+ma);
P(na+1,1:mc) = -R;% Indicator row.
while sum(P(na+1,1:na+ma-1) > zeros(1,na+ma-1)) ~= 0
xw = P(1:na , 1:na + ma - 1);
[ v1 i1 ] = max(xw);
[ v2 j ] = max(v1);
i = i1(1,j);
Y = P(1:na,na+ma)./P(1:na,j);
a1 = sign(Y);
a1 = a1 + ones(na,1);
y1 = Y.*a1/2;
[ v3 i ] = min(y1);% finding lowest non -ve no
if v3 == 0
ys = sort(y1);
k = 1;
while ys(k,1) <= 0
k = k + 1;
end
b = ys(k,1);
[ i j1 ] = find( y1 == b );
end
P = elimination(P,i,j);
ele = find(sign(P(na+1,1:na+ma-1))== -1);
[ ne me ] = size(ele);
if me == 0
break
else
j = ele(1,1);% fixing pivot column
Y = P(1:na,na+ma)./P(1:na,j);
a1 = sign(Y);
a1 = a1 + ones(na,1);
y1 = Y.*a1/2;
[ v3 i ] = min(y1);
if v3 == 0
ys = sort(y1);
k = 1;
while ys(k,1) <= 0
k = k + 1;
end
b = ys(k,1);
[ i j1 ] = find( y1 == b );
P = elimination(X,i,j);
end
end
for k = 1:na+ma-1
un = sign(P(:,k));
if un == - ones(na+1,1)
disp(' It is not bounded')
return
end
end
end
opt = P( na+1, ma+na);
sol = P(1:na , 1:ma-1);
for k = 1: ma-1
t = roots( [sol(:,k);0] );
[ nt mt ] = size(t);
if t == zeros(nt,1)
mat(1,k) = X(na - nt +1, na+ma);
else
mat(1,k) = 0;
end
end
disp('Co-efficient matrix ')
mat
disp(' optimum value ')
opt
function P = elimination(P,i,j)
[ nX mX ] = size( P);
a = P(i,j);
P(i,:) = P(i,:)/a;
for k = 1:nX
if k == i
continue
end
P(k,:) = P(k,:) - P(i,:)*P(k,j);
end
Get Answers For Free
Most questions answered within 1 hours.