Loop my code until c vector is zero or less

조회 수: 2 (최근 30일)
Jordan Whitehurst
Jordan Whitehurst 2018년 1월 30일
편집: Soumya Saxena 2018년 2월 2일
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
Jordan Whitehurst
Jordan Whitehurst 2018년 1월 30일
@Jan Simon,
I genuinely thought James tursa was referring to my MATLAB code and didn’t even think to check the text box I’m typing in :S an error on my bit, if the code needs to be put in this format, once I return home, I will re-post the original problem using the { } code button :)
Walter Roberson
Walter Roberson 2018년 1월 30일
Someone already formatted the code on your behalf.

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

답변 (1개)

Soumya Saxena
Soumya Saxena 2018년 2월 2일
편집: Soumya Saxena 2018년 2월 2일

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by