Hi Community, I got a problem with my gauss elimination and I'm unable to fix it myself.

2 views (last 30 days)
Hi Community, I got an error in the following code. May you guys help me with this code, as its important for me to get it run until tomorrow and it seems like I'm unable to fix it myself.
I get a 'NaN' error for
A = [0, 1, 2, 5, 1, -3; 1, 0, 3, -1, 1, -2; 2, -1, 4, 3, 1, 0; -1, 3, 3, 4, 2, -2]
b = [4; -7; -7; 10]
and it says 'Index in position 1 exceeds array bounds (must not exceed 3)' for
A = [0, 0, -2, 0, 7; 1, 2, -5, 3, 6; 2, 4, -5, 6, -5}
b = [12; 14; -1]
The code is as follows
function [ C ] = gaussalgorithmus( A, b )
count_ze = 1;
count_sp = 1;
eins_ze = -1;
null_ze = -1;
merker = -1;
[z, s] = size (A);
S
C = [A, b];
for k1 = 1 : s
for k2 = 1 : z
if C(k1, k2) == 1 && k1 >= count_ze
eins_ze = k1;
end
end
if eins_ze ~= -1
C([count_ze, eins_ze], :) = C([eins_ze, count_ze], :);
elseif (eins_ze == -1) && (rund(C(count_ze,count_sp)) ~= 0)
lambda = 1/rund(C(count_ze,count_sp));
C(count_ze, :) = C(count_ze, :) * lambda;
elseif (eins_ze == -1) && (rund(C(count_ze,count_sp)) == 0)
for k3 = count_ze : z
if rund( C(k3, count_sp)) ~= 0
null_ze = k3;
end
end
if null_ze ~= -1
C([count_ze, null_ze], :) = C([null_ze, count_ze], :);
C(count_ze, :) = C(count_ze, :) * 1/rund(C(count_ze, count_sp));
else
merker = 1;
end
end
for k4 = 1 : z
if k4 ~= count_ze && rund(C(k4,k1)) ~= 0 && merker ~= 1 && ...
rund(C(count_ze, count_sp)) ~= 0
C = vielfachesaddieren(C, k4, count_ze, -1*(rund(C(k4,k1)) / ...
rund(C(count_ze,count_sp))));
end
end
if merker ~= 1
C(count_ze, :) = C(count_ze, :) * (1 / rund( C(count_ze, count_sp)));
end
count_ze = count_ze + 1;
count_sp = count_sp + 1;
eins_ze = -1;
if null_ze ~= -1
count_ze = count_ze - 1;
end
if merker == 1
count_ze = count_ze - 1;
merker = -1;
end
for k5 = 1 : z
for k6 = 1 : s + 1
if abs( C(k5, k6)) < 10e-10
C(k5, k6) = 0;
end
end
end
if count_ze == z + 1
break
end
end
end
function [ numb ] = rund( number )
if abs(number) < 10e-13
numb = 0;
else
numb = number;
end
end
function [ A ] = vielfachesaddieren(A, j ,i, faktor)
A(i,:) = A(i,:) * faktor;
A(j,:) = A(j,:) + A(i,:);
end
  1 Comment
Paul
Paul on 2 Nov 2021
Edited: Paul on 2 Nov 2021
Suggest you carefully go through the code and recheck all of the indexing operations. For example, in this line
[z, s] = size (A);
z is the number of rows of A and s is the number of columns. Then later on k1 loops from 1 to s (the number of columns), and k2 loops from 1 to z (the number of rows). But then in this line
if C(k1, k2) == 1 && k1 >= count_ze
k1 is the row index and k2 is the column index, which is backwards. I think there may be other errors like this as well.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!