有限差分法(Python):薛定inger方程

时间:2018-06-28 15:16:07

标签: python-3.x physics

我遇到的问题是在“位置”功能内。其他一切似乎都正常,位置和概率的期望值都很好!问题是当我进入动量/能量时,它需要dR / dx和dI / dx(波动函数的复数和实数导数。)可以使用如下的有限差分法来计算:

d [f] / dx = f [i + 1] -f [i-1] / 2 * h

我尝试执行此操作,但是有一些麻烦,它的标题为“有限差分法”。如果要运行动量和能量函数,它们应该返回正弦波。 (出于以后可能会想到的原因。)我们将不胜感激!

示例代码:

import numpy as np
import integral as itg
import matplotlib.pyplot as plt
import ode as ode

def sch_eqn(id, R, I, t):
    """INPUTS
    ===================================================================
    id: a core requirement for the leapfrog method.
    R: Real part of psi.
    I: Imaginary part of psi.
    t:time-interval.
    OUTPUTS
    ===================================================================
    dpsi/dt.
    """ 
    b = 0.5/(h*h)
    if (id == 0):
        tmp = -b*I
        dydt = (2*b + V)*I
    else:
        tmp = b*R
        dydt = (-2*b - V)*R

dydt[1:-1] += tmp[:-2] + tmp[2:]
dydt[0] += tmp[-1] + tmp[1]
dydt[-1] += tmp[-2] +tmp[0]
return dydt


def gaussian(s, x0, x): 
"""INPUTS
   ======================================================================
   s: width of gaussian
   x0: where gaussian is centered around initially.
   x: actual changing variable (distance) throughout time.
   OUTPUTS
   ======================================================================
   Normalized Gaussian of SHO."""
    c = 1.0/np.sqrt(s*np.sqrt(np.pi))
    return c*np.exp(-((x-x0)/s)**2/2)

def initialize(a, b, N):
"""Given a specific potential (V) it provides a mesh given it's inital value 
   and 
   boundary conditions.

    INPUTS
   ======================================================================
   a: the start of our mesh.
   b: the last piece of our mesh.
   N: seperations between a and b.
   OUTPUTS
   ======================================================================
   """
    x = np.linspace(a, b, N+1)  
    V = 0.5*x*x                 
    s, x0  = 0.5, -5.0          
    R, I = gaussian(s, x0, x), np.zeros(N+1)    
    return x[1]-x[0], x,V,R,I  

h, x, V, R, I = initialize(-10.,10.,500)
Normalization = gaussian(.5,-3,x)

def position():
    Rrec,Irec = R,I
    t = 0
    dt = h*h*0.25
    xrec = x
    hrec = h
    t_list = []
    integral_list = []

    while t < 10000:
        Rrec, Irec = ode.leapfrog(sch_eqn,Rrec,Irec,t,dt)
        def finitedifference(f,x,h,n):
            df = np.zeros_like(x)
            for i in range(1,n):
                df[i] = (f[i+1]-f[i-1])/(2*h)
                #end_points
                df[0] = (f[1]-f[0])/h
                df[-1] = (f[-1]-f[-2])/h
            return df
        dRdx = finitedifference(Rrec,xrec,hrec,500)
        dIdx = finitedifference(Irec,xrec,hrec,500)
        """probability"""
        #f = R*R + I*I
        """momentum expectation value (Should be multiplied by h_bar)"""
        f = Rrec*dIdx-Irec*dRdx
        """position expecation value"""
        #f = (Rrec**2+Irec**2)*x
        """Energy expectation value(Should be multiplied by h_bar/2*m)"""
        #f = dRdx**2 + dIdx**2 
        integral = itg.simpson(f,h)
        t_list.append(t)
        integral_list.append(integral)
        t += 1
    plt.figure()
    plt.plot(t_list,integral_list)
    plt.xlabel("time")
    plt.ylabel("expecation value")
    plt.show()


position()

0 个答案:

没有答案