我试图通过有限差分近似来解决以下问题:
, on and ;
;
。
我把当作我的问题。
编程根本不是我的强项,所以我在实现方面遇到了一些问题。这是我的代码:
## This program is to implement a Finite Difference method approximation
## to solve the Heat Equation, u_t = k * u_xx,
## in 1D w/out sources & on a finite interval 0 < x < L. The PDE
## is subject to B.C: u(0,t) = u(L,t) = 0,
## and the I.C: u(x,0) = f(x).
import numpy as np
import matplotlib.pyplot as plt
# definition of solution to u_t = k * u_xx
def u(x,t):
return u
# definition of initial condition function
def f(x):
return x^2
# parameters
L = 1
T = 10
N = 10
M = 10
s = 0.25
# uniform mesh
x_init = 0
x_end = L
dx = float(x_end - x_init) / N
# time discretization
t_init = 0
t_end = T
dt = float(t_end - t_init) / M
t = np.zeros(M+1)
t = np.arange(t_init, t_end, dt)
# Boundary Conditions
for m in xrange(0, M):
t[m] = m * dt
u(0, t[m]) = 0
u(N, t[m]) = 0
# Initial Conditions
for j in xrange(0, N):
x[j] = j * dx
u(x[j], 0) = f(x[j])
# finite difference scheme
for j in xrange(1, N-1):
u(x[j],t[m+1]) = u(x[j],t[m]) + s * ( u(x[j+1],t[m]) -
2 * u(x[j],t[m]) + u(x[j-1],t[m]) )
所以特别是,我想知道(1)我的函数定义是否可以?(2) 我的均匀网格和时间离散化好吗?(3) 我是否为参数选择了合理的值?(我知道对于较小的和,近似值更准确,但缺点是计算时间增加......)
(4) 我的循环如何(对于初始/边界条件)?我觉得我需要一个用于有限差分方案的嵌套循环。一个用于 j 值的 for 循环和另一个用于及时遍历每个值 m 的 for 循环。它是否正确?...另外,我不断收到错误消息: u(0, t[m]) = 0 "can't assign to function call." 那里有什么问题?
在此先感谢您的帮助!正如我所提到的,我的编程能力不是很强,所以我觉得做这件事就像一条离水的鱼。