0

I declared the vector Psi_init by just copying the value of Psi. Then, I don't touch the variable Psi_init any more. But when I print the value of Psi_init at the end of the code, the value of Psi_init is changed to the value of Psi after the for-loop. The value is actually changed on the funtion return of the function Runge-Kutta. I don't get why this actually happens.

import numpy as np
from matplotlib import pyplot as plt
from scipy import linalg


#----------------------------------------------------------------
#Function definitions


def f(t,Ham,Psi):
    return (-1j*Ham*Psi)


def RungeKutta(t,Ham,Psi,DeltaT):
    k1=f(t,Ham,Psi)
    k2=f(t+DeltaT/2,Ham,Psi+k1*DeltaT/2)
    k3=f(t+DeltaT/2,Ham,Psi+k2*DeltaT/2)
    k4=f(t+DeltaT,Ham,Psi+k3*DeltaT)
    return Psi+DeltaT/6*(k1+2*k2+2*k3+k4)




#-------------------------------------------------------------------
#Anfangsbedingungen
DeltaT=0.01
finalT=1.0
NumOfIt=int(finalT/DeltaT)+1

omega1=5.0
omega2=3.0

Psi=np.matrix([[1],[0]],dtype=np.complex)
Psi_init=Psi
Ham=np.matrix([[omega1,0],[0,omega2]])
Zustand=np.zeros((2,NumOfIt),dtype=np.complex)

time=np.linspace(0,finalT,NumOfIt,endpoint='true')

#---------------------------------------------------------------------
#Zeitentwicklung

for t in time:
    ii=int(t/DeltaT)
    Psi[0:2] =RungeKutta(t,Ham,Psi,DeltaT)[0:2]
    Zustand[0:2,ii:ii+1]=Psi[0:2]


print Psi_init
anonymous
  • 409
  • 1
  • 4
  • 14
  • Yeah, that should be an answer to my question. But why does then a simple Psi=2*Psi and then printing Psi_init not change Psi_init. Psi_init is only changed in the function return – anonymous Jul 26 '17 at 08:04
  • 2
    Of course, because `Psi = 2 * Psi` does not modify the original array, but creates a new one and re-assigns it to the name `Psi`. – bereal Jul 26 '17 at 08:09
  • ok, this makes sense! Thanks a lot" – anonymous Jul 26 '17 at 08:43

0 Answers0