Error using ==> rdivide Matrix dimensions must agree HELP!
Show older comments
Hey guys, I am implementing a function to help start the simplex method. But when i input my matrices and vectors I get the same message of 'Error using etc' coming up again and again. I searched this forum before and tried to change the divisor with .\ but still no luck.
The function is below, any help would be great! I highlighted the trouble line:
function [obj,x,y] = revised_simplex(c,A,b,eps,B)
[m,n] = size(A);
%%%%%%%%%%%%%%%%%%%
% Step 1:- We are given an initial basis B
N = setdiff(1:n,B);
% B = find(x0);
% N = find(ones(n,1) - abs(sign(x0)));
*xB = A(:,B).\b;*
% xB = x0(B);
iter = 0;
while 1 == 1,
iter = iter + 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Step 2 :- Solve A_B^Ty = c_B and compute s_N = c_N - A_N^Ty
% Declare optimality if s_N <= 0
% Else find the entering non-basic variable x_{N(k)}
y = A(:,B)'\c(B);
sN = c(N) - A(:,N)'*y;
[sNmax,k] = max(sN);
if sNmax <= eps,
fprintf('We are done\n');
fprintf('Number of iterations is %d\n',iter);
x = zeros(n,1);
x(B) = xB;
fprintf('Optimal objective value is %f\n',c'*x);
obj = c'*x;
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Step 3 :- Solve A_Bd = a_{N(k)}
% Find theta = Min_{i=1,...,m|d_i > 0} xB(i)/d(i)
% Let theta = xB(l)/d(l)
% x_{B(l)} is the leaving basic variable
% Also check for unboundedness if d <= 0
d = A(:,B)\A(:,N(k));
zz = find(d > eps)';
if (isempty(zz))
error('System is unbounded\n');
end
[theta,ii] = min(xB(zz)./d(zz));
l= zz(ii(1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Step 4:- Update B and N
% Also x(B(i)) = x(B(i)) - theta*d(i), i=1,...,m and i not equal to l
% x(B(l)) = theta
temp = B(l);
B(l) = N(k);
N(k) = temp;
xB = xB - theta*d;
xB(l) = theta;
end; % while
I know this looks messy, if anyone can tell me how to input the code into this editor on mathswork to make it look nicer i would be grateful.
3 Comments
Walter Roberson
on 26 Apr 2013
I gather that the line with the problem is
xB = A(:,B).\b
Please show us
size(A), size(B), size(b)
Jan
on 26 Apr 2013
@Bradley: Please post complete copies of the full error message instead of the shortened "Error using etc". Then we do not waste time with guessing, which line is failing.
Answers (1)
Jan
on 26 Apr 2013
The standard procedure to solve such problems is the debugger. Type this in the command window:
dbstop if error
Then start the program again and wait until Matlab stops at the error. Then inspect the current line and the used variables. Copy the parts of the failing command to the command window to investigate the source of the problems.
Debugging is even more efficient than asking the forum for help - I have to admit this inspite of my vain participüation in this forum.
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!