-2
Well, I need to solve some linear systems through the Jacobi method using Python 3.8 programming, I made the code (which I’ll leave below), but I can’t make a print file that stores my results in an organized way. My goals are:
- Create a folder and in it a file with the Jacobi function
- From another file 'call' this function to solve all linear systems that will be there
- Print these results in an organized way, like the figure below
import numpy as np
n = 3
A = np.array([(10.0, 3, -2), (2.0, 8, -1), (1.0, 1, 5)])
b = np.array([(57.0), (20.0), (-4.0)])
Toler = 1.0000e-05
IterMax = 50
x = np.zeros((n, 1))
v = np.zeros((n, 1))
Iter = 0
CondErro = 0
for i in range(n):
r = (1/A[i, i])
for j in range(n):
if i != j:
A[i, j] *= r
b[i] *= r
x[i] = b[i]
Iter = 0
while True:
Iter += 1
for i in range(n):
soma = 0
for j in range(n):
if i != j:
soma += A[i, j] * x[j]
v[i] = b[i] - soma
NormaNum = 0
NormaDen = 0
for i in range(n):
t = abs(v[i]-x[i])
if t > NormaNum:
NormaNum = t
if abs(v[i]) > NormaDen:
NormaDen = abs(v[i])
x[i] = v[i]
NormaRel = (NormaNum / NormaDen)
print(f'{Iter}{x}{NormaRel}')
if NormaRel <= Toler or Iter >= IterMax:
break
if NormaRel <= Toler:
CondErro = 0
else:
CondErro = 1
It would not be simpler just to display in the ordinary way and when you want to redirect the default output to a file?
– anonimo