我正在尝试设计一种用于有限差分法的算法,但我有点困惑。所讨论的常微分方程是y''-5y'+10y = 10x,其中y(0)=0,y(1)=100。因此,我需要一种方法来获得系数,该系数将从关系中乘以"y_i“:

然后将得到的系数存储到一个矩阵中,这将是我将通过高斯-乔丹求解的系统的矩阵。问题归结为如何获得这些系数并将它们移动到矩阵中。我想过手动计算系数,然后输入矩阵,但我需要对大小为0.1,0.001和0.001的步长执行此操作,所以这在这里确实不是一个可行的选择。
发布于 2019-04-07 10:01:06
让我们假设ODE的更一般情况
c1 * y''(x) + c2 * y'(x) + c3 * y(x) + c4 * x = 0在边界条件下
y(0) = lo
y(1) = hi您希望为步长为h = 1 / n (其中n + 1是样本数)的x ∈ [0, 1]解决此问题。我们想要解决yi = y(h * i)的问题。yi的范围从i ∈ [0, n]开始。为此,我们想要解决一个线性系统
A y = b每个内部yi都会施加一个线性约束。因此,我们在A和n - 1列中有对应于未知yi的n - 1行。
要设置A和b,我们只需在未知的yi上滑动一个窗口(我假设从零开始建立索引)。
A = 0 //the zero matrix
b = 0 //the zero vector
for i from 1 to n - 1
//we are going to create the constraint for yi and store it in row i-1
//coefficient for yi+1
coeff = c1 / h^2 + c2 / h
if i + 1 < n
A(i - 1, i) = coeff
else
b(i - 1) -= coeff * hi //we already know yi+1
//coefficient for yi
coeff = -2 * c1 / h^2 - c2 / h + c3
A(i - 1, i - 1) = coeff
//coefficient for yi-1
coeff = c1 / h^2
if i - 1 > 0
A(i - 1, i - 2) = coeff
else
b(i - 1) -= coeff * lo //we already know yi-1
//coefficient for x
b(i - 1) -= c4 * i * h
nexthttps://stackoverflow.com/questions/55553393
复制相似问题