I am attempting to write code for Gaussian elimination with pivoting. I need my code to be able to find the maximum pivot in the column and swap the rows to ensure that the first element is always the largest absolute value. However, my code consistently produces an error that I cannot identify the solution for. Could someone please assist me?
GaussEliminationwithPivoting[A_, b_] :=
Module[{n, x, maxIndex, maxValue, factor}, n = Length[b];
x = Table[0, {n}];
(*Elimination phase*)
For[i = 1, i <= n,
i++,(*Find the maximum pivot in the current column.*)maxIndex = i;
maxValue = Abs[A[[i, i]]];
For[j = i + 1, j <= n, j++,
If[Abs[A[[j, i]]] > maxValue, maxIndex = j;
maxValue = Abs[A[[j, i]]];];];
(*Swap the rows to ensure that the pivot is the largest absolute \
value.*)If[maxIndex != i, A[[{i, maxIndex}]] = A[[{maxIndex, i}]];
b[[{i, maxIndex}]] = b[[{maxIndex, i}]];];
(*Gauss Elimination*)
For[j = i + 1, j <= n, j++, factor = A[[j, i]]/A[[i, i]];
A[[j]] -= factor*A[[i]];
b[[j]] -= factor*b[[i]];];];
(*Phase of backward substitution.*)
For[i = n, i >= 1, i--, x[[i]] = b[[i]];
For[j = i + 1, j <= n, j++, x[[i]] -= A[[i, j]]*x[[j]];];
x[[i]] /= A[[i, i]];];
Return[x];
]
(*Example*)
A = {{2, 1, -1}, {-3, -1, 2}, {-2, 1, 2}};
b = {8, -11, -3};
solution = eliminacaoGaussianaPivotamento[A, b]
Thats the error:
During evaluation of In[115]:= Set::setps: {{2,1,-1},{-3,-1,2},{-2,1,2}} in the part assignment is not a symbol.
During evaluation of In[115]:= Set::setps: {8,-11,-3} in the part assignment is not a symbol.
During evaluation of In[115]:= Set::setps: {{2,1,-1},{-3,-1,2},{-2,1,2}} in the part assignment is not a symbol.
During evaluation of In[115]:= General::stop: Further output of Set::setps will be suppressed during this calculation.
Out[118]= {-(3/4), 8, -(3/2)}