Пример #1
0
for i,j in enumerate(n):
    

    h   = 1/(j+1)
    xx  = np.linspace(0,1,j)
    
    # The forcing function
    fx = 100*np.exp(-10*xx)

    # Analytic solution
    ux = 1 - (1 - np.exp(-10))*xx - np.exp(-10*xx)

    # Thomas algorithm setup
    a = -1*np.ones([j,1])
    b = 2*np.ones([j,1])
    c = -1*np.ones([j,1])

    d = (h**2)*fx.reshape(len(fx),1)
    
    # Thomas algorithm for matrix inverse
    th_ux = Functions.Thomas_algorithm_solution(a,b,c,d)

    plt.subplot(len(n),1,i+1)
    plt.plot(xx,th_ux)
    plt.plot(xx,ux)
    plt.title('n={}'.format(j))
    plt.ylabel('u(x)')
    plt.xlabel('x')
    plt.legend(['Analytic','Thomas Algorithm'],loc='upper right')
    plt.grid()
Пример #2
0
    a = np.concatenate([a1, a2])
    A = toeplitz(a, a)

    # Normalize to get orthonormal matrix for Jacobi
    V = A.copy()
    for j in range(i):
        V[:, j] = V[:, j] / np.linalg.norm(V[:, j])

    ##############

    lmbda_an = np.zeros(i)
    for j in range(i):
        lmbda_an[j] = A[j, j] + 2 * A[0, 1] * np.cos(j * np.pi / i)

    time_start_B = time.clock()
    eigB = Functions.bisect_eig(A, n_pol=i)
    eigB = Functions.sort_eig_val(eigB)
    time_elapsed_B = (time.clock() - time_start_B)
    B.append(time_elapsed_B)

    time_start_Nu = time.clock()
    eigNP, eigNPv = np.linalg.eig(A)
    eigNP = Functions.sort_eig_val(eigNP)
    time_elapsed_Nu = (time.clock() - time_start_Nu)
    Nu.append(time_elapsed_Nu)

    time_start_J = time.clock()
    eigJ, eigJv = Functions.jacobi(A, V, n=i, conv=10**-8)
    eigJ, eigJv = Functions.sort_eig(eigJ, eigJv)
    eigJ = Functions.sort_eig_val(eigJ)
    time_elapsed_J = (time.clock() - time_start_J)
Пример #3
0
for i,j in enumerate(n):
    

    h   = 1/(j+1)
    xx  = np.linspace(0,1,j)
    
    # The forcing function
    fx = 100*np.exp(-10*xx)

    # Analytic solution
    ux = 1 - (1 - np.exp(-10))*xx - np.exp(-10*xx)

    # Gauss algorithm setup
    a = -1*np.ones([j,1])
    b = 2*np.ones([j,1])

    d = (h**2)*fx.reshape(len(fx),1)
    
    # Gaus elimination for matrix inverse
    #th_ux = Functions.gaus_elim(b,a,d)
    # special case
    th_ux = Functions.special_case(d)
    
    plt.subplot(len(n),1,i+1)
    plt.plot(xx,th_ux)
    plt.plot(xx,ux)
    plt.title('n={}'.format(j))
    plt.ylabel('u(x)')
    plt.xlabel('x')
    plt.legend(['Analytic','Gauss elimination'],loc='upper right')
    plt.grid()
Пример #4
0
    
    h   = 1/(j+1)
    xx  = np.linspace(0,1,j)
    
    # The forcing function
    fx = 100*np.exp(-10*xx)

    # Analytic/closed form solution
    ux = 1 - (1 - np.exp(-10))*xx - np.exp(-10*xx)

    # Special case setup
    d = (h**2)*fx.reshape(len(fx),1)
    
    time_start_th = time.clock()
    # special case
    th_ux = Functions.special_case(d)
    time_elapsed_th = (time.clock() - time_start_th)
    th.append(time_elapsed_th)
    
    # Tridiagonal matrix setup
    a1 = np.array([2,-1])
    a2 = np.zeros([j-2])
    a  = np.concatenate([a1,a2])
    
    # Define the Toeplitz matrix        
    A    = toeplitz(a, a)
    
    time_start_lu = time.clock()
    # LU inverse
    lu_ux = Functions.LU_decomp_inverse(A,d).T
    time_elapsed_lu = (time.clock() - time_start_lu)
        rho = (j + 1 * h)
        V[j] = rho**2
        d = 2.0 / h**2 + V[j]
        e = -1.0 / h**2
        A[j, j] = d
        if j > 0:
            A[j, j - 1] = e
        if j < i - 1:
            A[j, j + 1] = e

    ##############
    W = A.copy()
    for j in range(i):
        W[:, j] = W[:, j] / np.linalg.norm(W[:, j])

    eigJ, eigJv = Functions.jacobi(A, W, n=i, conv=10**-8)
    eigJ, eigJv = Functions.sort_eig(eigJ, eigJv)
    eigJ = Functions.sort_eig_val(eigJ)

    eigNP, eigNPv = np.linalg.eig(A)
    eigNP = Functions.sort_eig_val(eigNP)

    #plt.figure()
    ax = plt.figure().gca()
    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.title('N={}'.format(i))
    plt.scatter(np.arange(4), lmbd_an[0:4], marker='x')
    plt.scatter(np.arange(4), eigJ[i - 4:][::-1], marker='v')
    plt.scatter(np.arange(4), eigNP[i - 4:][::-1], marker='o')
    plt.legend(['Analytical', 'Jacobi', 'Numpy'])
    plt.ylabel('Eigenvalue')
Пример #6
0
# The forcing function
fx = 100*np.exp(-10*xx)

# Analytic solution
ux = 1 - (1 - np.exp(-10))*xx - np.exp(-10*xx)

# Tridiagonal matrix setup
a1 = np.array([2,-1])
a2 = np.zeros([n-2])
a  = np.concatenate([a1,a2])
    
# Define the Toeplitz matrix        
A    = toeplitz(a, a)

d = (h**2)*fx.reshape(len(fx),1)

x = np.random.randn(n).reshape(n,1)

v = Functions.Jacobi_it(A,x,d,eps=0.001)    

plt.figure()
plt.plot(xx,ux)
plt.plot(xx,v)
plt.ylabel('u(x)')
plt.xlabel('x')
plt.legend(['Analytic','Jacobi'],loc='upper right')
plt.grid()