示例#1
0

#Initialize values
M = 30
lamb = 1.5
err = 10
tol = 0.000001
it = 0
iter_max = 100
u_k1 = np.linspace(0.9, 0.95, (M + 1) * (M + 1))
F = G1(u_k1, M, lamb, 1, 1, 1, 1)

while err > tol and it < iter_max:
    J = jacobi(u_k1, M)
    a = np.linalg.solve(J, F)
    u_k1 -= a
    F = G1(u_k1, M, lamb, 1, 1, 1, 1)
    err = la.norm(F)
    it += 1
print("err", err, "it", it)

# Adding boundary points for plotting
I2 = np.ones(M + 1)
A = np.vstack((I2, u_k1.reshape((M + 1), (M + 1)), I2))
I = np.ones(M + 3)
I = I[:, np.newaxis]
U = np.hstack((I, A, I))

x, y = np.ogrid[0:1:(M + 3) * 1j, 0:1:(M + 3) * 1j]
plott(x, y, U)
    F[bc_indices] = G[bc_indices]

    return F * h**2


# Function for solving the bvp
def solve_bvp_neumann(bvp, M):
    A = fdm_neumann(bvp, M)
    F = rhs_neumann(bvp, M)
    U = spsolve(A, F)
    return U


def f(x, y):
    return 1 + 0 * x + 0 * y


def u(x, y):  # boundary values
    return 0 * x + 0 * y


def v(x, y):
    return np.array([y, -x])


M = 35
x, y = np.ogrid[0:1:(M + 1) * 1j, 0:1:(M + 1) * 1j]
ex = BVP(f, v, u, 0, 1, 0.01)
U = solve_bvp_neumann(BVP(f, v, u, 0, 1, 0.01), M)
plott(x, y, U.reshape((M + 1, M + 1)))
示例#3
0
def u_ex(x, y):
    return (2 * x ** 2 + y ** 2)


def v(x, y):
    return np.array([x, y * y])


ex_1 = BVP(f, v, u_ex, 0, 1, 1, u_ex)

# Number of subdivisions in each dimension
M = 5

# Define the grid using a sparse grid, and using the imaginary number 1j to include the endpoints
x, y = np.ogrid[0:1:(M + 1) * 1j, 0:1:(M + 1) * 1j]

# Evaluate u on the grid.
U_ext = ex_1.uexact(x, y).ravel()
U = solve_bvp(ex_1, M)

print("Numerical sol:", U)
print("Exact sol:", U_ext)

error = U_ext - U
print(error)
Eh = np.linalg.norm(error, ord=np.inf)
print('The error is {:.2e}'.format(Eh))

plott(x, y, U.reshape((M + 1, M + 1)))
plott(x, y, u_ex(x, y))
示例#4
0
for i in range(M + 1):
    for j in range(M + 1):
        if S[i, j] == 0:
            U_ext[i, j] = 0
U_ext = U_ext.ravel()
U = solve_bvp_circle(ex_1, M, S)

print("Numerical sol:", U)
print("Exact sol:", U_ext)

error1 = U - U_ext
print(error1)
Eh = np.linalg.norm(error1, ord=np.inf)
print('The error is {:.2e}'.format(Eh))

plott(x, y, U.reshape((M + 1, M + 1)))
plott(x, y, U_ext.reshape((M + 1, M + 1)))
plott(x, y, error1.reshape((M + 1, M + 1)))

# error analysis
M_list = [20, 38, 76, 150]
E = []
h_list = []
for M1 in M_list:
    x, y = np.ogrid[0:1:(M1 + 1) * 1j, 0:1:(M1 + 1) * 1j]
    S = shape_matrix(M1)
    U = solve_bvp_circle(ex_1, M1, S)
    U_ext = ex_1.uexact(x, y)
    for i in range(M1 + 1):
        for j in range(M1 + 1):
            if S[i, j] == 0:
示例#5
0
def v(x, y):
    return np.array([x, y])


# error analysis
M_list = [10, 20, 39, 76 , 150]
E = []
h_list = []

for M1 in M_list:
    x, y = np.ogrid[0:1:(M1 + 1) * 1j, 0:1:(M1 + 1) * 1j]
    ex = BVP(f1, v, u, 0, 1, 1, u)
    U = solve_bvp(ex, M1)
    U_ext = ex.uexact(x, y)
    if M1 == 150:
        plott(x, y, U.reshape((M1 + 1, M1 + 1)))
        plott(x, y, U_ext)
    U_ext = U_ext.ravel()
    error = U_ext - U
    E.append(np.linalg.norm(error, ord=np.inf))
    h_list.append(1 / M1)

print(E)
order = np.polyfit(np.log(h_list), np.log(E), 1)[0]
print("order", order)
h_list = np.array(h_list)

plt.figure()
plt.loglog(h_list, E, 'o-')
plt.loglog(h_list, h_list ** 2 * 1 / 12 * (np.pi ** 4 + (1 + 1 / 8) * np.pi ** 3))
plt.show()