Why am I getting the error: Subscript indices must either be real positive integers or logicals.
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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)
Azzi Abdelmalek
on 9 Jan 2013
Edited: Azzi Abdelmalek
on 9 Jan 2013
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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!