Why am I getting the error: Subscript indices must either be real positive integers or logicals.

Here is the code. It is for Gaussian elimination
% Impementation of algorithm 6.1 for Gaussian Elimination
% Input: n x n augmented matrix A , vector b
% Output: vector x as in Ax = b
function [x] = GaussElim(A,b)
% Step 0
[nr nc] = size(A);
if (nr ~= nc)
disp('Must be n x n matrix try again');
return
end
% Step 1
x = zeros(1,nr);
for i = 1 : nr - 1
if ( A(i,i) == 0 )
t = min (find ( A(i+1:nr,i) ~= 0) + i);
% Step 2
if (isempty(t))
disp ('Error: A matrix is singular try again');
return
end;
% Step 3
t = A(i,:);
te = b(i);
A(i,:) = A(t,:);
b(i) = b(t);
A(t,:) = t;
b(t) = te;
end;
% Step 4
for j = i+1 : nr
% Step 5
m = -A(j,i) / A(i,i);
A(j,i) = 0;
A(j, i+1:nr) = A(j, i+1:nr) + m * A(i, i+1:nr);
% Step 6
b(j) = b(j) + m * b(i);
end;
% Step 8
x(nr) = b(nr) / A(nr, nr);
% Step 9
for k = nr - 1 : -1 : 1
x(k) = (b(k) - sum (x(k+1:nr) .* A(k, k+1:nr))) / A(k,k);
end;
end;

Answers (1)

clear
A=rand(3,3)
b=[1 2 3]
[nr nc] = size(A);
if (nr ~= nc)
disp('Must be n x n matrix try again');
return
end % Step 1
x = zeros(1,nr);
for i = 1 : nr - 1
if ( A(i,i) == 0 )
t = min (find ( A(i+1:nr,i) ~= 0) + i);
% Step 2
if (isempty(t))
disp ('Error: A matrix is singular try again');
return
end; % Step 3
t = A(i,:);
te = b(i);
A(i,:) = A(t,:); b(i) = b(t);
A(t,:) = t; b(t) = te;
end
% Step 4
for j = i+1 : nr % Step
m = -A(j,i) / A(i,i);
A(j,i) = 0;
A(j, i+1:nr) = A(j, i+1:nr) + m * A(i, i+1:nr); % Step 6
b(j) = b(j) + m * b(i);
end
% Step 8
x(nr) = b(nr) / A(nr, nr); % Step 9
for k = nr - 1 : -1 : 1
x(k) = (b(k) - sum (x(k+1:nr) .* A(k, k+1:nr))) / A(k,k);
end
end

This question is closed.

Asked:

on 9 Jan 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!