-1

I am writing a program that solves a system of equations using the Gauss method, but I get an error related to recursion, but there is no recursion in my code, how can I fix it?

n := 12;
k := 2;
A := Matrix([[1, 2, 1], [4, 5, 6], [7, 8, n]]);
B := Vector([2, n, n^2])
Ao = Matrix([A, B]);

n1 := 3; s := 0;
for k from 1 to n1-1 do;
for j from k to n1 do;
s := s+abs(A[j, k]);
end do;
if s = 0 then print("Error") end if;
for j from k + 1 to 3 do;
m := Ao[j, k]/Ao[k, k];
for p from k+1 to 4 do;
Ao[j, p] := -m*Ao[k, p]+Ao[j, p];
end do;end do;end do;
if Ao[n1,n1] = 0 then print("Error") else x[n] := (Ao[n1,n1+1])/(Ao[n1,n1]);
end if;
for i from n1 by -1 to 1 do;
sums := 0;
for j from i + 1 to n1 do;
sums := sums + Ao[i, j]*x[j];end do;
x[i] := (Ao[i, n1+1]-sums)/Ao[i, i];
end do;
print("x1" = x[1], "x2" = x[2], "x3" = x[3]);
Utis
  • 21
  • 5

1 Answers1

0

You have the line,

Ao = Matrix([A, B]);

which creates an equation but which does not make an assignment.

The correct syntax for assignment would be,

Ao := Matrix([A, B]);

With Ao unassigned (as you have it) the following line is a recursive asignment to the uninitialized indexed reference Ao[j,p]

Ao[j, p] := -m*Ao[k, p]+Ao[j, p];

If you correct the line to make an actual assigment then the code computes a result that matched the solution from,

LinearAlgebra:-LinearSolve(A,B);
acer
  • 6,671
  • 15
  • 15