0

I'm trying to use an if statement to check when the approximated values converge, but it keeps returning that it converges after 2 iterations, which it shouldn't since removing the if-break statement and setting it to 1000 iterations gets very different values.

def Jacobi2D(U,xrange,yrange,order):
    Up=sym.zeros(len(xrange),len(yrange))
    for k in range(1,order):
        if k>=2 and U==Up:
            print('Approximation converges after',k,'iterations')
            break
        Up=U #Save the U of the previous iteration for use in the formula
        for i in range(1,len(xrange)-1):
            for j in range(1,len(yrange)-1):
                U[i,j]=Up[i,j]+(1/4)*(Up[i+1,j]+Up[i-1,j]+Up[i,j+1]+Up[i,j-1]-4*Up[i,j])
                continue
            continue
        continue
    return U

The function takes U as a matrix with dimensions (len(xrange),len(yrange)) and 'order' is how many iterations you want the approximation to go through. Does anyone know why the condition is being triggered before redefining Up=U? Any help would be appreciated, thank you!

Scott
  • 1
  • 1
  • Just FYI, you don't need a [`continue`](https://docs.python.org/3/reference/simple_stmts.html#continue) statement at the end of every `for` loop. – MattDMo Oct 26 '20 at 18:54

1 Answers1

0

Your condition will break when k >= 2 and when U == Up. On the second iteration, k = 2 and you set Up = U, so U == Up. Therefore, your loop will always break on the second iteration as it satisfies the conditions.

I might suggest a different condition for convergence: track the difference between the previous approximation and this iteration's approximations. Once the difference is smaller than some target precision (say epsilon = 0.0000001), break and return.

EDIT: The problem has to do with the Up = U pointing both variables to the same object so that changes in either object change the other object as well. You can resolve this by replacing Up = U with Up = U.copy().

Jace
  • 177
  • 6
  • But I redefine U in the 2 for loops within the k loop, so why is it not waiting to redefine Up till after the if statement? – Scott Oct 26 '20 at 19:28
  • Maybe it has to do with the `==` here? I just ran a test comparing a `Up` of all zeros to a `U` of all 1s with `U == Up` and it is returning `true`. – Jace Oct 26 '20 at 19:36
  • Nevermind. It has to do with references to the same object. Your `U` is pointing to the same object as your `Up`, so editing `U` edits `Up` as well. You should use `Up = U.copy()`. Editing my answer to reflect that. – Jace Oct 26 '20 at 19:38