Loop my code until c vector is zero or less

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 Kommentare

James Tursa
James Tursa am 30 Jan. 2018
Learn how to format your code by highlighting it and then pressing the "{ } Code" button. I have done it for you this time.
Jordan Whitehurst
Jordan Whitehurst am 30 Jan. 2018
Hi James Tursa,
I have been looking for the {} code button, the only thing I could find was under the tab at the top under ‘publish’, it created a random if statement with intervals 1:10 for x and disp(x) ?!?! Thank u for your response as well
Walter Roberson
Walter Roberson am 30 Jan. 2018
when you are entering you code or text here, on the Answers system, look just above the entry box. You will see something that looks like
The button in the center, the '{} Code' button, is the one to use. Select your code that you have entered here and click on that button to have it formatted as code.
Jan
Jan am 30 Jan. 2018
Bearbeitet: Jan am 30 Jan. 2018
@Jordan: While you have typed you message, you must have seen this:
This means, that the searched button was just 2 cm away from the point where you typed that you are searching for it.
You are not the first person who had this problem. Do you have any useful suggestion how code formatting could be made more easy and obvious for the beginners? The members of the forum remind the newcomers several times each day to format code and it would be a relief if this can be reduced.
Jordan Whitehurst
Jordan Whitehurst am 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 am 30 Jan. 2018
Someone already formatted the code on your behalf.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 30 Jan. 2018

Bearbeitet:

am 2 Feb. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by