matlab & numeric analysis basics_01

本文介绍了在MATLAB中使用Gauss消去法和列主元素消去法解线性方程组的基本原理和步骤。Gauss消去法通过初行变换将增广矩阵变为阶梯形矩阵,然后通过回代求解。而列主元素消去法通过选择列的最大元素来减少舍入误差,实现更稳定的解法。文章详细阐述了两种方法的MATLAB实现,并强调了列主元素消去法在避免舍入误差方面的优势。
摘要由CSDN通过智能技术生成

解线性代数方程组的直接法的Matlab算法

写在前面:刚刚开始使用这个平台,第一篇记录,还不是很习惯,如有问题,还望指出。本身是学生,专业不是学计算机,方向上属于金融。

在数值分析课程中,解线性代数方程组主要有迭代法和直接法。直接法具体有LU分解,Gauss消去法,主元素消去法等。这篇主要说一下Gauss消去法和列主元素消去法的matlab实现。

其实,对于解方程而言,matlab是可以直接进行方程组的解的,即利用矩阵除的命令。

假设系数矩阵是A,右端项矩阵是b,原方程组可以写成Ax = b,直接输入x = A\b命令可以得到线性代数方程组的解。当原方程组具有唯一解时输出唯一解,输出的x是唯一解;若原方程组“无解”,即超定方程组时,这个命令返回方程组最小二乘意义下的最优解;若原方程组有无穷多解时(欠定方程组),返回无穷多解的其中一组解。

Gauss消去法解线性方程组

Gauss消去法是直接法解线性代数方程组的一种原理较为容易的方法。在数值分析的入门课程中大都有所描述,具体原理如下:

step1:取方程组的系数矩阵A和右端项,组成增广矩阵[A b],对增广矩阵进行初行变换使之变成阶梯形矩阵。
例如,以三阶系数矩阵为例:

[ a 11 a 12 a 13 b 1 a 21 a 22 a 23 b 2 a 31 a 32 a 33 b 3 ] ⇒ 初 等 行 变 换 [ a 11 ′ a 12 ′ a 13 ′ b 1 ′ a 22 ′ a 23 ′ b 2 ′ a 33 ′ b 3 ′ ] \begin{bmatrix} a_{11}&a_{12}&a_{13}&b_1\\ a_{21}&a_{22}&a_{23}&b_2\\a_{31}&a_{32}&a_{33}&b_3\end{bmatrix}\xRightarrow{初等行变换}\begin{bmatrix} a'_{11}&a'_{12}&a'_{13}&b'_1\\ &a'_{22}&a'_{23}&b'_2\\&&a'_{33}&b'_3\end{bmatrix} a11a21a31a12a22a32a13a23a33b1b2b3 a11a12a22a13a23a33b1b2b3

step2:通过阶梯形矩阵的最后一行可以算出最后一个未知数的值。

  x 3 = b 3 ′ / a 33 ′ \ x_{3} = b'_3 / a'_{33}  x3=b3/a33

step3:最后一个值回代,可以算出每一个未知数的值。

  x 2 = ( b 2 ′ − a 23 ′ ∗ x 3 ) / a 22 ′ \ x_{2} = (b'_2 - a'_{23}*x_3) / a'_{22}  x2=(b2a23x3)/a22 其中,   x 3 \ x_3  x3的值已知。
  x 1 = ( b 1 ′ − a 13 ′ ∗ x 3 − a 12 ′ ∗ x 2 ) / a 11 ′ \ x_{1} = (b'_1 - a'_{13}*x_3 - a'_{12}*x_2) / a'_{11}  x1=(b1a13x3a12x2)/a11 其中   x 2 \ x_2  x2   x 3 \ x_3  x3的值已经解出。

Gauss消去法核心在于化成上三角形矩阵(阶梯形矩阵)和回代。

具体的Matlab算法实现:

function S = SolveEquationG(A, b)
% create the augmented matrix combined coefficient matrix and right items
B = [A b];
% the num of dimensions of the coefficient matrix (the number of rows)
n = size(A, 1);
% transfer the coefficient matrix to an upper triangular matrix
for k = 1 : n-1
    if B(k, k) ~= 0 %only when this element is not zero, the equations system could be solved in this method.
        for i = k+1 : n
            B(i, k) = B(i, k)/B(k, k);
            for j = k+1 : n+1
                B(i, j) = B(i, j) - B(i, k) * B(k, j);
            end
        end
    else break
    end
end
% reprocess the matrix and solve out the solution vector.
S = rand(n, 1);% create a vector to store the solutions.
S(n, 1) = B(n, n+1)/B(n, n); % calculate the 'xn'
%calculate rest elements of the solution vector.
for t1 = n-1:-1:1
    for t2 = n:-1:t1+1
        B(t1, n+1) = B(t1, n+1) - S(t2, 1)*B(t1, t2);
    end
    S(t1, 1) = B(t1, n+1)/ B(t1, t1);
end

列主元素消去法解方程组

Gauss消去法确实提供了一个解线性代数方程组的一个逻辑较为简单的方法,但是Gauss消去法却有着弊端——有可能会产生较大的舍入误差。如果消元过程中,第k个系数矩阵第k行第k列的元素是一个极小的数(eg.   0.4 ∗ 1 0 − 11 \ 0.4 * 10^{-11}  0.41011) 。这时,这一行下一个元素的消元运算就有可能被“吃掉”,从而使最后计算出来的解向量产生一个较大的舍入误差。

为了避免舍入误差,需要对Gauss消去法进行改进,便引出列主元素消去法。(事实上,使用主元素消去法也可以达到同样的效果,但是显然算法上,列主元素消去法更容易实现)

列主元素消去法的算法过程大致和Gauss消去法一致,核心思想都是先处理系数矩阵与右端项列向量组成的增广矩阵,将增广矩阵化成上三角形矩阵(阶梯型矩阵)。

改进在于,每一列元素消元的过程中,将最大的元素换到第k行,第k列,然后消去下面的几行的第k个元素。

以进行第二列的消元过程为例:

[ a 11 a 12 . . . a 1 n b 1 a 22 . . . a 2 n b 2 a 32 . . . a 3 n b 3 . . . . . . . . . . . . a m 2 . . . a m n b m ] ⇒ e x c h a n g e   a i 2   w i t h   a 22 a s s u m e   a i 2   i s   t h e   b i g g e s t   e l e m e n t   o f   c o l u m n   2 [ a 11 a 12 . . . a 1 n b 1 a i 2 . . . a i n b i a 32 . . . a 3 n b 3 . . . . . . . . . . . . a 22 . . . a 2 n b 2 . . . . . . . . . . . . a m 2 . . . a m n b m ] ⇒ e l i m i n a t e   t h e   e l e m e n t s   o f   c o l u m n   2 [ a 11 a 12 . . . a 1 n b 1 a i 2 . . . a i n b 2 a 33 ′ . . . a 3 n ′ b 3 ′ a 43 ′ . . . a 4 n ′ b 4 ′ . . . . . . . . . . . . a m 3 ′ . . . a m n ′ b m ′ ] \begin{bmatrix} a_{11}&a_{12}&...&&a_{1n}&b_1\\&a_{22}&...&&a_{2n}&b_2\\&a_{32}&...&&a_{3n}&b_3\\&...&...&&...&...\\&a_{m2}&...&&a_{mn}&b_m\end{bmatrix}\xRightarrow[exchange \: a_{i2}\:with\:a_{22}]{assume \: a_{i2}\:is \:the\:biggest\:element \: of \:column \: 2}\begin{bmatrix} a_{11}&a_{12}&...&&a_{1n}&b_1\\&a_{i2}&...&&a_{in}&b_{i}\\&a_{32}&...&&a_{3n}&b_3\\&...&...&&...&...\\&a_{22}&...&&a_{2n}&b_2\\&...&...&&...&...\\&a_{m2}&...&&a_{mn}&b_m\end{bmatrix}\xRightarrow{eliminate \: the \: elements \: of \: column \: 2}\begin{bmatrix} a_{11}&a_{12}&...&&a_{1n}&b_1\\&a_{i2}&...&&a_{in}&b_2\\&&a'_{33}&...&a'_{3n}&b'_3\\&&a'_{43}&...&a'_{4n}&b'_4\\&&...&...&...&...\\&&a'_{m3}&...&a'_{mn}&b'_{m}\end{bmatrix} a11a12a22a32...am2...............a1na2na3n...amnb1b2b3...bmassumeai2isthebiggestelementofcolumn2 exchangeai2witha22a11a12ai2a32...a22...am2.....................a1naina3n...a2n...amnb1bib3...b2...bmeliminatetheelementsofcolumn2 a11a12ai2......a33a43...am3............a1naina3na4n...amnb1b2b3b4...bm

按照这个过程进行消去可以有效减少最后算出的方程组解矩阵的舍入误差。

其余的过程类似于未改进的Gauss消去法的过程。
具体算法如下:

function S = SolveEquationM(A, b)
% create the augmented matrix combined the coefficiet matrix and right items
B = [A b];
% the num of dimensions of the coefficient matrix;
n = size(A, 1);
% transfer the coefficient matrix to an upper triangular matrix
for k = 1 : n
    IM = B(:, k);
% select the max value of this column as main element
    [max_IM, index] = max(IM, [], 1);
% exchange the value of rows
    B([k, index], :) = B([index k], :);
    if B(k, k) ~= 0
        for i = k+1 : n
            B(i, k) = B(i, k)/B(k, k);
            for j = k+1 : n+1
                B(i, j) = B(i, j) - B(i, k) * B(k, j);
            end
        end
    else break
    end
end
% reprocess the matrix (same as Gauss-elimination)
S = rand(n,1);
S(n, 1) = B(n, n+1)/B(n, n);
for t1 = n-1 : -1 : 1
    for t2 = n : -1 : t1 + 1
        B(t1, n+1) = B(t1, n+1) - S(t2, 1) * B(t1, t2);
    end
    S(t1, 1) = B(t1, n+1)/ B(t1, t1);
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值