Jeremy Knight Math 639
<Text-field style="Heading 1" layout="Heading 1">Algorithm 7.2: GaussSiedel_iterate(n,A,b,x_0,tol,N)</Text-field> GaussSiedel_iterate := proc(n,A,b,x_0,tol,N) #n: scalar number of equations and unknown; #A: Coefficient Matrix #b: Constant Vector #XO: Initial approximation vector #tol: tolerance #N: Max iterations local i, j, k, m, tempx, T, X0, x_k, DX, euclid_norm; #option trace; #initialize a matrix of vectors to store the iterates X0:=Matrix(); X0(1,1):=x_0; k:=1; while k <= N do #initialize a temporary vector to store the approximation values during the loop tempx:=Vector(); x_k:=X0(1,k); for i from 1 to n do T:=0: for j from 1 to i-1 do T:=T+(A(i,j)*tempx(j)): end do: for j from i+1 to n do T:=T+(A(i,j)*x_k(j)): end do: tempx(i):=(1/(A(i,i)))*((-1*T)+b(i)): end do: X0(1,k+1):=tempx: Print(X0(1,k+1)); #Calculate the norm of ||x_(k+1)-x_k|| DX:=abs(X0(1,k) - X0(1,k+1)); euclid_norm:=DX(1); for m from 2 to n do if DX(m)>euclid_norm then euclid_norm:=DX(m); end if; end do; #Compare Euclidean Norm to tolerance if euclid_norm < tol then print("tolerance reached Iterations =",k); print(evalf(X0(1,k+1))); break; end if; if k>N then break; end if; k:=k+1; end do; if k=N+1 then print("tolerance not reached"); print(evalf(X0(1,k))); end if; end proc:
A:=Matrix([[10,-1,2,0],[-1,11,-1,3],[2,-1,10,-1],[0,3,-1,8]]): b:=Vector([6,25,-11,15]): x:=Vector([0,0,0,0]): GaussSiedel_iterate(4,A,b,x,.001,20); LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYlLUkqbXZlcmJhdGltR0YkNiNRQ1E/dG9sZXJhbmNlfGlycmVhY2hlZHxpckl0ZXJhdGlvbnN8aXI9NiJGJy1JI21vR0YkNi1RIixGJy8lLG1hdGh2YXJpYW50R1Enbm9ybWFsRicvJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHUSV0cnVlRicvJSlzdHJldGNoeUdGOC8lKnN5bW1ldHJpY0dGOC8lKGxhcmdlb3BHRjgvJS5tb3ZhYmxlbGltaXRzR0Y4LyUnYWNjZW50R0Y4LyUnbHNwYWNlR1EmMC4wZW1GJy8lJ3JzcGFjZUdRLDAuMzMzMzMzM2VtRictSSNtbkdGJDYkUSI1RidGMw== LSZJJ1ZlY3Rvckc2JCUqcHJvdGVjdGVkR0koX3N5c2xpYkc2IjYjSSdjb2x1bW5HRig2Iy9JJCVpZEdGKCIqJzRmRkM= JSFH TTdSMApJNlJUQUJMRV9TQVZFLzI0Mjc1OTA5NlgqJSlhbnl0aGluZ0c2IjYiW2dsISMlISEhIiUiJSQiKyFHIjQrNSEiKiQiK1U4LSs/RikkIStaNi4rNUYpJCIrTDUpKSoqKiohIzU2Ig==