Loop my code until c vector is zero or less

3 views (last 30 days)
Jordan Whitehurst
Jordan Whitehurst on 30 Jan 2018
Edited: Soumya Saxena on 2 Feb 2018
here's my code (if the quality is crap, my only excuse is I don't like coding)
A = [1 0.5 2; 0 1 0.5; 2 1 1];
% | 1 .5 2 |
% | 0 1 .5 | --> What the matrix looks like
% | 2 1 1 |
c = [20 15 12];
emptyZ = zeros(1,3); % The zeros under the slack varibles of length m
Fullc = [0 c emptyZ]; % The string of values in the Obj row
b = [7; 10; 12;];
I = eye(3); % Identity matrix of size m when user inputs value
Q = [b A I; Fullc]
L = size(Q);
[val, idx] = max(Q(:));
[row, col] = ind2sub(size(Q),idx);
j = col;
bQ = Q(:,1);
a = bQ./Q(:,j);
a(a == 0) = NaN;
[valP, idxP] = min(a);
i = idxP; % This is our row i
v = ones(L(1),1); % Creates vector of ones
v(i) = 1./Q(i,j); % The pivot element in vector v is replaced by 1/Q
P1 = diag(v); % Sets all values in P1 in the diagonal of zeros and ones
P2 = eye(L(1)); % Opens identity matrix by length of rows
P2(:,i) = -Q(:,j); % Replaces the ith column of P2 by the negative column j in Q
P2(i,i) = 1; % Sets pivot element co-ordinates to 1
Q = P2*P1*Q % Complete pivot step
I'm trying to create a code that will do the simplex method without using linprog... :/ I have a piece of code which allows the user to enter the values in the appropriate arrays so I didn't need to put that in this, I used some random data to show my A, b and c matrices. I think all I need is my code to loop until the c vector has zero or less in that array. Any help would be greatly appreciated, many thanks
  6 Comments
Walter Roberson
Walter Roberson on 30 Jan 2018
Someone already formatted the code on your behalf.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by