0
I have a question about assigning values to variables. I created the following code for solving equation systems by the Gauss-Jacobi iterative method:
A = np.array([[2., -1.], [1., 2.]]) # Matriz de coeficientes
B = np.array([[1.], [3.]]) # Vetor de constantes
X = np.array([[0.], [0.]]) # Estimativa inicial
EA = 10**(-2) # Erro absoluto da solução do problema
erro = 1
while (erro > EA):
Xp = tuple(X)
print("\nO valor de Xp é:", Xp) # Aqui Xp está apresentando um valor
for i in range(len(A)):
s = 0
for j in range(len(A)):
if (i != j):
s += A[i,j]*Xp[j]
print("\nO valor de s é:", s)
X[i] = (B[i] - s)/A[i,i]
print("\nO valor de Xp é:", Xp) # Aqui Xp está apresentando outro valor, sendo que eu não alterei ele
print("\nO valor de X é:", X)
erro = np.amax(np.absolute(X - np.asarray(Xp)))
Initially, I set Xp to display the same value as X. Then I change only the value of X.
Whenever I run this code, the value of Xp is changing automatically, whenever I change the value of X. I tried to make Xp a tuple so that it doesn’t automatically change its value, but I didn’t succeed. What is the explanation for this and how to correct?
Throughout the code, I put comments indicating where my problem is occurring.
I believe you created a reference and not a copy. Use
Xp = tuple(np.copy(X))
, check the result and– Paulo Marques
Solved, thank you very much!
– Thiago Rodrigues
I’m glad you solved...
– Paulo Marques