Loop my code until c vector is zero or less
Show older comments
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
James Tursa
on 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
on 30 Jan 2018
Walter Roberson
on 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.
@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
on 30 Jan 2018
Walter Roberson
on 30 Jan 2018
Someone already formatted the code on your behalf.
Answers (1)
Soumya Saxena
on 2 Feb 2018
Edited: Soumya Saxena
on 2 Feb 2018
0 votes
Hi,
You may consider the following link:
Categories
Find more on Performance and Memory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!