1
$\begingroup$

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)}
$\endgroup$
3

1 Answer 1

1
$\begingroup$

If I make the following smallest possible change to your code:

GaussEliminationwithPivoting[AA_, bb_] :=
  Module[{A=AA,b=bb,n, x, maxIndex, maxValue, factor}, n = Length[b];
    ...
  ]
A = {{2, 1, -1}, {-3, -1, 2}, {-2, 1, 2}};
b = {8, -11, -3};
GaussEliminationwithPivoting[A, b]

it then displays, without error or warning

{2,3,-1}

What that has done is to make local copies of the matrix and vector that you are passing into your function/module. One of the rules inside Mathematica is that you cannot assign values inside a function to things you have passed into the function. If you need to change the values of things passed in then you must create a local variable inside the function and then you can do whatever you like with the local variable.

So my change passes in the same matrix and vector into the function that you do and then creates a local copy named A of your AA and a local copy named b of your bb and then your code proceeds unchanged after that. (I named things this way so you and I didn't need to go through every line of your code and change all you A and b variable names) You might need to be a little careful trying to understand the names A and AA and b and bb, but hopefully this detailed explanation has made it clear.

Please test this very carefully. Does this change make it work exactly correctly? Or at least if you find any more problems, are they unrelated to the change I just made?

$\endgroup$
1
  • $\begingroup$ Thank you so much !! It`s working perfectly now ! $\endgroup$ Sep 17, 2023 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.