三对角线性方程组(tridiagonal systems of equations)

  三对角线性方程组,对于熟悉数值分析的同学来说,并不陌生,它经常出现在微分方程的数值求解和三次样条函数的插值问题中。三对角线性方程组可描述为以下方程组:

\[a_{i}x_{i-1}+b_{i}x_{i}+c_{i}x_{i+1}=d_{i}
\]

其中\(1\leq i \leq n, a_{1}=0, c_{n}=0.\) 以上方程组写成矩阵形式为\(Ax=d\),即:

\[{\begin{bmatrix}
{b_{1}}&{c_{1}}&{}&{}&{0}\\
{a_{2}}&{b_{2}}&{c_{2}}&{}&{}\\
{}&{a_{3}}&{b_{3}}&\ddots &{}\\
{}&{}&\ddots &\ddots &{c_{n-1}}\\
{0}&&&{a_{n}}&{b_{n}}\\
\end{bmatrix}}
{\begin{bmatrix}{x_{1}}\\{x_{2}}\\{x_{3}}\\\vdots \\{x_{n}}\\\end{bmatrix}}={\begin{bmatrix}{d_{1}}\\{d_{2}}\\{d_{3}}\\\vdots \\{d_{n}}\\\end{bmatrix}}
\]

  三对角线性方程组的求解采用追赶法或者Thomas算法,它是Gauss消去法在三对角线性方程组这种特殊情形下的应用,因此,主要思想还是Gauss消去法,只是会更加简单些。我们将在下面的算法详述中给出该算法的具体求解过程。

  当然,该算法并不总是稳定的,但当系数矩阵\(A\)为严格对角占优矩阵(Strictly D iagonally Dominant, SDD)或对称正定矩阵(Symmetric Positive Definite, SPD)时,该算法稳定。对于不熟悉SDD或者SPD的读者,也不必担心,我们还会在我们的博客中介绍这类矩阵。现在,我们只要记住,该算法对于部分系数矩阵\(A\)是可以求解的。

算法详述

  追赶法或者Thomas算法的具体步骤如下:

  1. 创建新系数\(c_{i}^{*}\)和\(d_{i}^{*}\)来代替原先的\(a_{i},b_{i},c_{i}\),公式如下:

\[c^{*}_i = \left\{
\begin{array}{lr}
\frac{c_1}{b_1} & ; i = 1\\
\frac{c_i}{b_i - a_i c^{*}_{i-1}} & ; i = 2,3,...,n-1
\end{array}
\right.\\
d^{*}_i = \left\{
\begin{array}{lr}
\frac{d_1}{b_1} & ; i = 1\\
\frac{d_i- a_i d^{*}_{i-1}}{b_i - a_i c^{*}_{i-1}} & ; i = 2,3,...,n-1
\end{array}
\right.
\]

  1. 改写原先的方程组\(Ax=d\)如下:

\[\begin{bmatrix}
1 & c^{*}_1 & 0 & 0 & ... & 0 \\
0 & 1 & c^{*}_2 & 0 & ... & 0 \\
0 & 0 & 1 & c^{*}_3 & 0 & 0 \\
. & . & & & & . \\
. & . & & & & . \\
. & . & & & & c^{*}_{n-1} \\
0 & 0 & 0 & 0 & 0 & 1 \\
\end{bmatrix} \begin{bmatrix}
x_1 \\
x_2 \\
x_3 \\
.\\
.\\
.\\
x_k \\
\end{bmatrix} = \begin{bmatrix}
d^{*}_1 \\
d^{*}_2 \\
d^{*}_3 \\
.\\
.\\
.\\
d^{*}_n \\
\end{bmatrix}
\]

  1. 计算解向量\(x\),如下:

\[x_n = d^{*}_n, \qquad x_i = d^{*}_i - c^{*}_i x_{i+1}, \qquad i = n-1, n-2, ... ,2,1
\]

  以上算法得到的解向量\(x\)即为原方程\(Ax=d\)的解。

  下面,我们来证明该算法的正确性,只需要证明该算法保持原方程组的形式不变。

  首先,当\(i=1\)时,

\[1*x_{1}+c_{1}^{*}x_{2}=d_{1}^{*} \Leftrightarrow 1*x_{1}+\frac{c_{1}}{b_{1}}x_{2}=\frac{d_{1}}{b_{1}}\Leftrightarrow b_{1}*x_{1}+c_{1}x_{2}=d_{1}
\]

  当\(i>1\)时,

\[1*x_{i}+c_{i}^{*}x_{i+1}=d_{i}^{*} \Leftrightarrow 1*x_{i}+\frac{c_{i}}{b_{i} - a_{i} c^{*}_{i-1}}x_{i+1}=\frac{d_{i}- a_{i} d^{*}_{i-1}}{b_{i} - a_{i} c^{*}_{i-1}} \Leftrightarrow
(b_{i} - a_{i} c^{*}_{i-1})x_{i}+c_{i}x_{i+1}=d_{i}- a_{i} d^{*}_{i-1}\]

结合\(a_{i}x_{i-1}+b_{i}x_{i}+c_{i}x_{i+1}=d_{i}\),只需要证明\(x_{i-1}+c_{i-1}^{*}x_{i}=d_{i-1}^{*}\),而这已经在该算法的第(3)步的中的计算\(x_{i-1}\)中给出。证明完毕。

Python实现

  我们将要求解的线性方程组如下:

\[{\begin{bmatrix}
4&1&{0}&{0}&{0}\\
{1}&{4}&{1}&{0}&{0}\\
{0}&{1}&{4}&{1}&{0}\\
{0}&{0}&{1}&{4}&{1}\\
{0}&{0}&{0}&{1}&{4}\\
\end{bmatrix}}
{\begin{bmatrix}{x_{1}}\\{x_{2}}\\{x_{3}}\\{x_{4}} \\{x_{5}}\\\end{bmatrix}}={\begin{bmatrix}{1\\0.5\\ -1\\3\\2}\\\end{bmatrix}}
\]

  接下来,我们将用Python来实现该算法,函数为TDMA,输入参数为列表a,b,c,d, 输出为解向量x,代码如下:

# use Thomas Method to solve tridiagonal linear equation
# algorithm reference: https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm import numpy as np # parameter: a,b,c,d are list-like of same length
# tridiagonal linear equation: Ax=d
# b: main diagonal of matrix A
# a: main diagonal below of matrix A
# c: main diagonal upper of matrix A
# d: Ax=d
# return: x(type=list), the solution of Ax=d
def TDMA(a,b,c,d): try:
n = len(d) # order of tridiagonal square matrix # use a,b,c to create matrix A, which is not necessary in the algorithm
A = np.array([[0]*n]*n, dtype='float64') for i in range(n):
A[i,i] = b[i]
if i > 0:
A[i, i-1] = a[i]
if i < n-1:
A[i, i+1] = c[i] # new list of modified coefficients
c_1 = [0]*n
d_1 = [0]*n for i in range(n):
if not i:
c_1[i] = c[i]/b[i]
d_1[i] = d[i] / b[i]
else:
c_1[i] = c[i]/(b[i]-c_1[i-1]*a[i])
d_1[i] = (d[i]-d_1[i-1]*a[i])/(b[i]-c_1[i-1] * a[i]) # x: solution of Ax=d
x = [0]*n for i in range(n-1, -1, -1):
if i == n-1:
x[i] = d_1[i]
else:
x[i] = d_1[i]-c_1[i]*x[i+1] x = [round(_, 4) for _ in x] return x except Exception as e:
return e def main(): a = [0, 1, 1, 1, 1]
b = [4, 4, 4, 4, 4]
c = [1, 1, 1, 1, 0]
d = [1, 0.5, -1, 3, 2] '''
a = [0, 2, 1, 3]
b = [1, 1, 2, 1]
c = [2, 3, 0.5, 0]
d = [2, -1, 1, 3]
''' x = TDMA(a, b, c, d)
print('The solution is %s'%x) main()

运行该程序,输出结果为:

The solution is [0.2, 0.2, -0.5, 0.8, 0.3]

  本算法的Github地址为: https://github.com/percent4/Numeric_Analysis/blob/master/TDMA.py .

  最后再次声明,追赶法或者Thomas算法并不是对所有的三对角矩阵都是有效的,只是部分三对角矩阵可行。

参考文献

  1. https://www.quantstart.com/articles/Tridiagonal-Matrix-Solver-via-Thomas-Algorithm
  2. https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
  3. https://wenku.baidu.com/view/336bafa3daef5ef7ba0d3ccc.html

三对角线性方程组(tridiagonal systems of equations)的求解的更多相关文章

  1. 三对角矩阵(Tridiagonal Matrices)的求法:Thomas Algorithm(TDMA)

    转载http://www.cnblogs.com/xpvincent/archive/2013/01/25/2877411.html 做三次样条曲线时,需要解三对角矩阵(Tridiagonal Mat ...

  2. Sherman-Morrison公式及其应用

    Sherman-Morrison公式   Sherman-Morrison公式以 Jack Sherman 和 Winifred J. Morrison命名,在线性代数中,是求解逆矩阵的一种方法.本篇 ...

  3. Guass列主元、平方根法、追赶法求解方程组的C++实现

    一,要解决的问题 选用合适的算法,求解三种线性方程组:一般线性方程组,对称正定方程组,三对角线性方程组. 方程略. 二,数值方法 1,使用Guass列主元消去法求解一般线性方程组. Guass列主元是 ...

  4. 《Fluid Engine Development》 学习笔记1-求解线性方程组

    我个人对基于物理的动画很感兴趣,最近在尝试阅读<Fluid Engine Development>,由于内容涉及太多的数学问题,而单纯学习数学又过于枯燥,难以坚持学习(我中途放弃好多次了) ...

  5. <<Numerical Analysis>>笔记

    2ed,  by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...

  6. <Numerical Analysis>(by Timothy Sauer) Notes

    2ed,  by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...

  7. Maple拥有优秀的符号计算和数值计算能力

    https://www.maplesoft.com/products/maple/ Maple高级应用和经典实例: https://wenku.baidu.com/view/f246962107221 ...

  8. [转]BLAS简介

    BLAS(Basic Linear Algebra Subprograms)是一组线性代数计算中通用的基本运算操作函数集合[1] .BLAS Technical (BLAST) Forum负责规范BL ...

  9. MP算法和OMP算法及其思想

    主要介绍MP(Matching Pursuits)算法和OMP(Orthogonal Matching Pursuit)算法[1],这两个算法尽管在90年代初就提出来了,但作为经典的算法,国内文献(可 ...

随机推荐

  1. jQuery库介绍

    一.jQuery是什么 jQuery是一个轻量级,兼容多浏览器的JavaScript库 jQuery能够简化JavaScript编程 二.jQuery的优势 1.一款轻量级的JavaScript框架 ...

  2. python模块:random

    """Random variable generators. integers -------- uniform within range sequences ----- ...

  3. springmvc接收数组方式总结

    1.接受正常的数组 如param1=aaa&param1=bbb&param1=3 对于这种,在实体参数中,使用String param1[] 这种参数既可以获取数组的值 2.接受数组 ...

  4. 32 bit 与 64 bit 程序(2)比较

    32 bit  与 64 bit 程序(2)区别 由于操作系统内存分配的不同,导致软件开发过程中,需要编译不同版本的软件. 几个重要概念:  (1)这里所说的的32位与64位程序,是指经过编译器编译后 ...

  5. SELECT版FTP

    功能: 1.使用SELECT或SELECTORS模块实现并发简单版FTP 2.允许多用户并发上传下载文件环境: python 3.5特性: select 实现并发效果运行: get 文件名 #从服务器 ...

  6. Singleton Summary

    Java Singleton: Singleton pattern restricts the instantiation of a class and ensures that only one i ...

  7. Redis 基础、高级特性与性能调优

    本文将从Redis的基本特性入手,通过讲述Redis的数据结构和主要命令对Redis的基本能力进行直观介绍.之后概览Redis提供的高级能力,并在部署.维护.性能调优等多个方面进行更深入的介绍和指导. ...

  8. 关于 SQL 注入的问题

    拼串 (Statement)方式 1.编译次数多,效率比较低:会出现SQL注入问题(数据安全问题):先传参数再编译. 2.Sql文对应的字符串不一样,需要再次编译.Sql文对应的字符串一样,不会再编译 ...

  9. Android开发工程师文集-提示框,菜单,数据存储,组件篇

    提示框,菜单,数据存储,组件篇 Toast Toast.makeText(context, text, 时间).show(); setDuration();//设置时间 setGravity();// ...

  10. MySQL长度、大小写验证问题[开发篇]

    实际情况是这样的,从外部读取数据,然后存入数据库,要求不能重复存入数据. 由于以前的系统里没有加唯一键,所以,就只有手动判断相同记录是否存在了. case1.由于其中某个值存在为空情况,而存入时该值变 ...