示例#1
0
# 	Ap[i,i] = a2
# 	Ap[i+1,i] = a1 #Bottom
# 	Ap[i,i+1] = a1 #Right
# Ap[N-2,N-2] = a2
#==============================================================================

# Static plots at time t0
plot(real(psi), label = '$\Psi(x,t)$')
plot(abs(psi), label = '$|\Psi(x,t)|$')
plot(-abs(psi), label = '$-|\Psi(x,t)|$')
xlabel('$x$')
ylabel('$\Psi(x,t)$')
legend()
show()

# Animation
from visual import curve, rate

psi_c = curve()
psi_c.set_x(x-L/2)

#psi = banded(A,v,1,1)
while True:
	rate(30)
	psi_c.set_y(real(psi)*1e-9)
	psi_c.set_z(imag(psi)*1e-9)	
	for i in range(20):
		v = b1*psi[1:N] + b2*(psi[2:N+1] + psi[0:N-1])
		psi[1:N] = banded(A,v,1,1)

示例#2
0
def homework5_1():
    N = int(
        input(
            "Solving A Chain of Resistors. Please enter the number of unkown junction voltages.\n\n \tN = "
        ))
    A = np.zeros((N, N))
    V = 5  #voltage of upper rail
    u = 2  #number of non-zero values above diagonal of matrix A
    d = u  #number of non-zero values below diagonal of matrix A
    # populate A in a very ugly way!
    for i in range(2, N - 2):
        A[i, (i - 2)] = -1
        A[i, (i - 1)] = -1
        A[i, (i)] = 4
        A[i, (i + 1)] = -1
        A[i, (i + 2)] = -1
    A[0, 0] = 3
    A[0, 1] = -1
    A[0, 2] = -1
    A[1, 0] = -1
    A[1, 1] = 4
    A[1, 2] = -1
    A[1, 3] = -1
    A[N - 1, N - 1] = 3
    A[N - 1, N - 2] = -1
    A[N - 1, N - 3] = -1
    A[N - 2, N - 1] = -1
    A[N - 2, N - 2] = 4
    A[N - 2, N - 3] = -1
    A[N - 2, N - 4] = -1
    print(A)
    w = np.zeros(N)
    w[0] = V
    w[1] = V
    if (N <= 20):
        #solve Av=w
        v = np.linalg.solve(A, w)
        print(v)

    if (N > 20):
        #reorder matrix A into matrix diagonal ordered form
        Anew = np.zeros((1 + u + d, N))
        k = 0
        for i in range(N):
            for j in range(u + 1):
                Anew[(u + i - k - j), k] = A[i, k - j]
            for l in range(1, d + 1):
                if (i < N - l):
                    Anew[(u + i - k + l), k] = A[i + l, k]
            k += 1
        print(Anew)
        # solve Av=w
        v = banded.banded(Anew, w, u, d)
        print(v)
        #sanity check of all values being less than 5
        for i in range(N):
            if (v[i] > 5):
                print("greater than 5!!!!!")
                check = 1
            else:
                check = 0
        if (check == 0):
            print("All less than 5!!!!")
        for i in range(N):
            if (v[i] < 0):
                print("less than 0!!!!!")
                check = 1
            else:
                check = 0
        if (check == 0):
            print("All greater than 0!!!!")
示例#3
0
line = ax.plot(x,psi.real)
ax.set_xlabel('$x [m]$',fontsize = 20)
ax.set_ylabel('$\psi$',fontsize = 20)
'''
# BEGIN COMPUTING PSI AND ANIMATING THE RESULT

psi_saves = []  # Array to hold snapshots

t = 0
while t < Nt * h:
    line[0].set_ydata(psi.real)
    ax.set_title('Time = {0} s'.format(t))
    plt.draw()
    v = vvec(psi)  # Create v vector
    psi = banded(
        A, v, 1,
        1)  # Use Gaussian elimination on banded matrices to update psi
    if t < eps or abs(t - 1e-16) < eps or abs(t - 1e-15) < eps:
        psi_saves.append(psi)
    t += h

plt.figure()
plt.subplot(311)
plt.plot(x, psi_saves[0].real)
plt.ylabel('$\psi$', fontsize=20)
plt.title('$0$ s')
plt.subplot(312)
plt.plot(x, psi_saves[1].real)
plt.ylabel('$\psi$', fontsize=20)
plt.title('$10 ^{-16}$ s')
plt.subplot(313)
A = np.zeros((bands, N))

v = np.zeros(N)

v[0] = 5
v[1] = 5

A[0, :] = -1
A[1, :] = -1
A[2, :] = 4
A[2, 0] = 3
A[2, N - 1] = 3
A[3, :] = -1
A[4, :] = -1

V = np.round(b.banded(A, v, 2, 2), decimals=3)

i = np.linspace(1, 6, 6)

print('Voltage at each successive node starting from the first: ', V)

py.scatter(i, V)
py.title('Voltage at each node')

py.show()

#same solution using Gaussian Elimination and backsubsitution to Check

#Example 6.1

import numpy as np
示例#5
0
文件: 6.6.py 项目: akels/com-phy
U = 5

A = array([
	[3, -1, -1, 0, 0, 0],
	[-1, 4, -1, -1, 0, 0],
	[-1,-1, 4, -1, -1, 0],
	[0, -1,-1, 4, -1, -1],
	[0, 0, -1,-1, 4, -1],
	[0, 0, 0, -1, -1, 3]
],float)

v = array([U, U, 0, 0, 0, 0],float)

x = solve(A,v)
print(x)

# Banded example

N = 10000
Ab = empty((5,N),float)
vb = zeros(N,float)

vb[[0,1]] = U

Ab[:,:] = -1
Ab[2,:] = 4
Ab[2,0] = 3
Ab[2,N-1] = 3

x = banded(Ab,vb,2,2)
print(x)
示例#6
0
A[0, :] = a2
A[1, :] = a1
A[2:, ] = a2

arr = []

# setup vectors with solely x values
for i in range(len(x)):
    arr.append([x[i] - L / 2, 0, 0])

ksi_c = curve(pos=arr)

n = len(arr)


def update_points(ksi_c, y, z):
    for i in range(n):
        ksi_c.modify(i, y=y[i], z=z[i])


while True:
    rate(30)
    y = real(ksi) * 1e-9
    z = imag(ksi) * 1e-9
    # update at each point, keeping x
    update_points(ksi_c, y, z)
    for i in range(20):
        v = b1 * ksi[1:N] + b2 * (ksi[2:N + 1] + ksi[0:N - 1])
        ksi[1:N] = banded(A, v, 1, 1)
示例#7
0
A = makematrixA(a1,a2,x)

# initialize t
tstart = 0.0    # start time
tend = 1.0e-15   # end time
t = tstart
print_interval = 50
step = 0
print_num = 0
while t < tend:
    clf()
    # multiply matrix equation v = B * psi
    v = rhs(psi,N,b1,b2)

    # solve linear system A psi = v for psi
    psi=banded(A,v,1,1)
    
    # update animation with psi at current timestep    
    plot(x, abs(psi),'-b',x,psi.real,'-r',x,-abs(psi),'-b')
    plot(x,V(x)/eV/80.0,'g')
    ylim((-1,1))
    my_title="Time: %e" % (t)
    title(my_title)
    if step%print_interval==0:
		savefig('Lab09-Q2b-%i.png'%print_num)   
		print_num += 1   
    step+=1
    pause(0.01)
	
        
    t +=h
#Autor: Pablo Gullith
#Bibliotecas
import numpy as np
import banded as bd


def matriz(N):
    A = np.zeros([5, N], float)
    for j in range(N - 2):
        A[0, j + 2] = A[4, j] = -1
    for j in range(N - 1):
        A[1, j + 1] = A[3, j] = -1
    for j in range(N - 2):
        A[2, j + 1] = 4
    A[2, 0] = A[2, N - 1] = 3

    v = np.zeros(N, float)
    v[0] = v[1] = 5

    return A, v


A, v = matriz(6)
print("QUANDO N = 6:\n", bd.banded(A, v, 2, 2))

A, v = matriz(10000)
print("QUANDO N = 10000:\n", bd.banded(A, v, 2, 2))
    exp(complex(-(x * a - x0)**2 / (2 * sigma**2), kappa * x * a))
    for x in range(N + 1)
])

psi[0], psi[N] = 0, 0

# Main program to calculate psi at different time
for i in range(N):
    v_tmp = zeros(N + 1, complex)

    v_tmp[0] = b1 * psi[0, i] + b2 * (psi[1, i])
    for j in range(1, N):
        v_tmp[j] = b1 * psi[j, i] + b2 * (psi[j + 1, i] + psi[j - 1, i])
    v_tmp[N] = b1 * psi[0, i] + b2 * (psi[N, i])

    psi[:, i + 1] = banded.banded(A, v_tmp, 1, 1)

# (b)
# Animation using the method from Lab 08
for i in range(N + 1):

    clf()
    title("wavelength vs length")
    plot(arange(0, N + 1) * a, real(psi[:, i]))
    xlabel("length")
    ylabel("wavelength")
    ylim(-1, 1)
    show()
    pause(0.01)

# Screen shots at t = t1, t2, t3
# Test with 6 elements

N = 6
U = 5

A = array(
    [[3, -1, -1, 0, 0, 0], [-1, 4, -1, -1, 0, 0], [-1, -1, 4, -1, -1, 0],
     [0, -1, -1, 4, -1, -1], [0, 0, -1, -1, 4, -1], [0, 0, 0, -1, -1, 3]],
    float)

v = array([U, U, 0, 0, 0, 0], float)

x = solve(A, v)
print(x)

# Banded example

N = 10000
Ab = empty((5, N), float)
vb = zeros(N, float)

vb[[0, 1]] = U

Ab[:, :] = -1
Ab[2, :] = 4
Ab[2, 0] = 3
Ab[2, N - 1] = 3

x = banded(Ab, vb, 2, 2)
print(x)
示例#11
0
ax = plt.axes()
line = ax.plot(x,psi.real)
ax.set_xlabel('$x [m]$',fontsize = 20)
ax.set_ylabel('$\psi$',fontsize = 20)
'''
# BEGIN COMPUTING PSI AND ANIMATING THE RESULT 

psi_saves = [] # Array to hold snapshots

t = 0
while t < Nt*h:
    line[0].set_ydata(psi.real)
    ax.set_title('Time = {0} s'.format(t))
    plt.draw()
    v = vvec(psi) # Create v vector
    psi = banded(A,v,1,1) # Use Gaussian elimination on banded matrices to update psi
    if t < eps or abs(t-1e-16) < eps or abs(t-1e-15) < eps:
        psi_saves.append(psi)
    t+=h

plt.figure()
plt.subplot(311)
plt.plot(x,psi_saves[0].real)
plt.ylabel('$\psi$',fontsize = 20)
plt.title('$0$ s')
plt.subplot(312)
plt.plot(x,psi_saves[1].real)
plt.ylabel('$\psi$',fontsize = 20)
plt.title('$10 ^{-16}$ s')
plt.subplot(313)
plt.plot(x,psi_saves[2].real)
示例#12
0
N = 6
Vp = 5.0

# setup the arrays
A = empty([5,N], float)

A[0,:] = -1.0
A[1,:] = -1
A[2,:] = 4.0
A[3,:] = -1
A[4,:] = -1.0
A[2,0] = A[2, N-1] = 3.0

v = zeros(N, float)
v[0] = v[1] = Vp

# solve the equations
w = banded(A, v, 2, 2)

# make a plot
print(w)

plot(range(1, N+1), w)
plot(range(1, N+1), w, 'ko')
ylim(0,5)





示例#13
0
A    = A(len(xvalues),a1,a2)

#entries for calling banded function
up   = 1
down = 1

#start time
t    = 0
#end time
tend = 1e-15 #seconds
#an empty list to include si values
si_solutions = []

while t<tend+h:
    #solving the matrix equation using banded function
    si_new = banded(A,v(si_vals),up,down)
    #appending si values to a list
    si_solutions.append(si_new)
    #making the ew si to be the si used to calculate the newer si
    si_vals = si_new
    
    line[0].set_ydata(si_new)
    ax.set_title("time={0}".format(t))
    plt.ylabel("$\psi$")
    plt.draw()
    print t
    t += h



示例#14
0
N = 6
#set up banded matrix
A = zeros([5,N],float)
A[0,:] = -1
A[1,:] = -1
A[2,:] = 4
A[3,:] = -1
A[4,:] = -1
A[0,0] = 3
A[-1,-1] = 3

w = zeros(N,float)
w[0] = V
w[1] = V

v = banded(A,w,2,2)

print v

#c)
V = 5 #voltage
N= 1000
#set up banded matrix
A = zeros([5,N],float)
A[0,:] = -1
A[1,:] = -1
A[2,:] = 4
A[3,:] = -1
A[4,:] = -1
A[0,0] = 3
A[-1,-1] = 3
    A[0,i] = -k

    A[1,i] = alpha

    A[2,i] = -k

A[1,0] = alpha - k

A[1,N-1] = alpha - k


v = zeros(N,float)

v[0] = C

x = banded(A,v,1,1)

s = []
delta = 2
for i in range(len(x)):
	p = (i - len(x)/2)*delta
	s.append(vp.sphere(pos = vp.vector(p, 0, 0), radius = 0.3))
t0 = tm.time()
while True:
	t = tm.time()
	for i in range(len(s)):
		p = (i - len(s)/2)*delta + x[i]*sin(omega*(t - t0))
		s[i].pos = vp.vector(p, 0, 0)
	tm.sleep(0.05)

示例#16
0
v = b1 * psi[1:N] + b2 * (psi[2:N + 1] + psi[0:N - 1])

#now to solve the system Ax = v
#x will be time evolved value of psi, A is as given in Newman pg.440
from banded import banded

a1 = 1 + h * 1j * hbar / (2 * M * a**2)
a2 = -h * 1j * hbar / (4 * M * a**2)
A = np.empty([3, N], dtype=complex)
A[0, :] = a2
A[1, :] = a1
A[2, :] = a2
psi_t = np.zeros(N + 1, dtype=complex)  #psi at later time t
#Calculate 1 time step:
psi[1:N] = banded(
    A, v, 1,
    1)  #solve for x using Gauss elim and backsub for tridiagnonal matrix
#the above psi_t is the wavefunction on the domain x in [0,L] for t=h (h time step)

#Extend program to perform repeated steps and make an animation

steps = 2000
from pylab import figure, plot, xlabel, ylabel, title, clf, pause, draw

figure()
plot(x, psi.real)
xlabel('$x$ m')
ylabel('$\psi(x,t)$')
title('Wavefunction at time t = 0')

#instantiate emtpy vector for psi(x,t)
示例#17
0
from banded import banded
from pylab import plot,show

# Constants
N = 26
C = 1.0
m = 1.0
k = 6.0
omega = 2.0
alpha = 2*k-m*omega*omega

# Set up the initial values of the arrays
A = empty([3,N],float)
for i in range(N):
    A[0,i] = -k
    A[1,i] = alpha
    A[2,i] = -k
A[1,0] = alpha - k
A[1,N-1] = alpha - k

v = zeros(N,float)
v[0] = C

# Solve the equations
x = banded(A,v,1,1)

# Make a plot using both dots and lines
plot(x)
plot(x,"ko")
show()