Loop my code until c vector is zero or less

3 vues (au cours des 30 derniers jours)
Jordan Whitehurst
Jordan Whitehurst le 30 Jan 2018
Modifié(e) : Soumya Saxena le 2 Fév 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 commentaires
Jordan Whitehurst
Jordan Whitehurst le 30 Jan 2018
@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 le 30 Jan 2018
Someone already formatted the code on your behalf.

Connectez-vous pour commenter.

Réponses (1)

Soumya Saxena
Soumya Saxena le 2 Fév 2018
Modifié(e) : Soumya Saxena le 2 Fév 2018

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by