0% found this document useful (0 votes)
74 views1 page

Jacobi Seidel - Ipynb

This document contains Python code that implements the Jacobi and Gauss-Seidel iterative methods to solve a system of linear equations. It defines functions for Gauss-Jacobi and Gauss-Seidel that take as input the coefficient matrix A, right hand side vector b, initial guess x0, tolerance value tol, and convergence check flag. The functions perform the iterations, track the error, and return the solution vector x, number of iterations k, and error vector. The code provides an example of applying the methods to solve the system Ax=b, where A and b are defined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views1 page

Jacobi Seidel - Ipynb

This document contains Python code that implements the Jacobi and Gauss-Seidel iterative methods to solve a system of linear equations. It defines functions for Gauss-Jacobi and Gauss-Seidel that take as input the coefficient matrix A, right hand side vector b, initial guess x0, tolerance value tol, and convergence check flag. The functions perform the iterations, track the error, and return the solution vector x, number of iterations k, and error vector. The code provides an example of applying the methods to solve the system Ax=b, where A and b are defined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":

{"name":"Jacobi_Seidel.ipynb","provenance":[],"collapsed_sections":
[],"authorship_tag":"ABX9TyNmj18nqzzIPJVJ833crfTD"},"kernelspec":
{"name":"python3","display_name":"Python 3"},"language_info":
{"name":"python"}},"cells":[{"cell_type":"code","metadata":{"colab":
{"base_uri":"https://localhost:8080/"},"id":"clS_crNm8yHJ","executionInfo":
{"status":"ok","timestamp":1620735950554,"user_tz":180,"elapsed":21023,"user":
{"displayName":"Vitor Alves
Pires","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOWmozOiUOct2-
zIat3NeoYXwq3fkCGl72YJVgiQ=s64","userId":"04757212216159719223"}},"outputId":"ebaa0
0ee-0e4b-49a2-850a-7a4368394dfd"},"source":["import numpy as np\n","import math as
m\n","\n","# Gauss-Jacobi\n","def gauss_jacobi(A,b,x0,tol,flag):\n"," n =
np.shape(A)[0];\n"," D = np.diag(np.diag(A));\n"," aux_C =
np.linalg.solve(D,A);\n"," C = np.eye(n) - aux_C;\n"," g =
np.linalg.solve(D,b);\n"," kmax = 10000; k = 0;\n","\n"," error =
np.zeros(kmax);\n"," if flag:\n"," statment = (np.linalg.norm(b-
A.dot(x0))>tol and k<kmax);\n"," else:\n"," statment =
(k<kmax);\n","\n"," while statment:\n"," error[k] = np.linalg.norm(b-
A.dot(x0));\n"," k = k+1;\n"," x0 = C.dot(x0)+g;\n"," if
flag:\n"," statment = (np.linalg.norm(b-A.dot(x0))>tol and k<kmax);\n","
else:\n"," statment = (k<kmax);\n"," \n"," if (k == kmax and
flag):\n"," print('\\nErro: o método não converge.\\n')\n"," \n"," x =
x0;\n"," return x,k,error\n","\n","# Gauss-Seidel\n","def
gauss_seidel(A,b,x0,tol,flag):\n"," L = np.tril(A); R = np.triu(A,1);\n"," C
= -np.linalg.solve(L,R);\n"," g = np.linalg.solve(L,b);\n"," kmax = 10000; k
= 0;\n","\n"," error = np.zeros(kmax); \n","\n"," if flag: \n","
statment = (np.linalg.norm(b-A.dot(x0))>tol and k<kmax);\n"," else:\n","
statment = (k<kmax);\n"," \n"," while statment:\n"," error[k] =
np.linalg.norm(b-A.dot(x0));\n"," k = k+1;\n"," x0 =
C.dot(x0)+g;\n"," if flag:\n"," statment = (np.linalg.norm(b-
A.dot(x0))>tol and k<kmax);\n"," else:\n"," statment =
(k<kmax);\n","\n"," if (k == kmax and flag):\n"," print('\\nErro: o
método não converge.\\n')\n"," \n"," x = x0;\n"," return
x,k,error\n","\n","# Exemplo\n","A = np.array([[ -8, 1, 1], \n","
[ 1, -5, 1], \n"," [1, 1, -4]], dtype='double')\n","print(A)\n","b =
np.array([1,16,7], dtype='double')\n","print(b)\n","\n","# Função
Python\n","print('\\nSolução Python:\\n')\n","x =
np.linalg.solve(A,b);\n","print(x);\n","\n","input();\n","\n","# Métodos
Iterativos\n","n = np.shape(A)[0];\n","x0 = np.zeros(n); tol =
0.00000001;\n","\n","print('\\nSolução Gauss-
Jacobi:\\n')\n","(x_jacobi,k_jacobi,erro_jacobi) =
gauss_jacobi(A,b,x0,tol,1);\n","print(x_jacobi);\n","print('%d iterações usadas' %
(k_jacobi))\n","\n","input();\n","\n","print('\\nSolução Gauss-
Siedel:\\n')\n","(x_siedel,k_siedel,erro_siedel) =
gauss_seidel(A,b,x0,tol,1);\n","print(x_siedel);\n","print('%d iterações usadas' %
(k_siedel))"],"execution_count":null,"outputs":[{"output_type":"stream","text":
["[[-8. 1. 1.]\n"," [ 1. -5. 1.]\n"," [ 1. 1. -4.]]\n","[ 1. 16.
7.]\n","\n","Solução Python:\n","\n","[-1. -4. -3.]\n","\n","\n","Solução Gauss-
Jacobi:\n","\n","\n","Erro: o método não converge.\n","\n","[-0.98559082
-3.98034375 -2.97666992]\n","5 iterações usadas\n","\n","\n","Solução Gauss-
Siedel:\n","\n","\n","Erro: o método não converge.\n","\n","[-0.99947652
-3.99959092 -2.99976686]\n","5 iterações usadas\n"],"name":"stdout"}]}]}

You might also like