数值分析之直接法解线性方程组:高斯消去法、高斯乔丹消去法

本文介绍了两种线性代数中常用的方程组求解方法——高斯消元法和高斯-乔丹消元法,并提供了MATLAB或Octave中的实现代码。这两种方法均可用于解决线性方程组问题,适用于非奇异矩阵。

高斯消去法(Gauss消去法)和高斯-乔丹消去法(Gauss-Jordan消去法)都很简单。代码如下:


高斯消去法:

function [x] = Gaussian_elim(a, b)
% a is a coefficient matrix, b is a column vector storing values of a*x,
% and here the return value x should be a column vector storing values of x.
% We should suppose that matrix a is not a singular matrix.

X = [a b];
n = size(X,1);     % the number of rows
x = zeros(n,1);    % x stores the values of x

for i = 1:n
    [c,p] = max(abs(X(i:end,i)), [], 1);    % find the index p of the max element in column i
                                            % notice that (abs(X(i:end,i)) is a new matrix)
    X([i,p+i-1],:) = X([p+i-1,i],:);        % swap column i and column p+i
    for j = i+1:n                           % to eliminate the ith element in rows i to n
        m = X(j,i) / X(i,i);                % get the multiplier
        X(j,:) = X(j,:) - m*X(i,:);         % eliminate the first non-zero element in row j
    end
end

% 将a,b分开
a = X(:,1:size(a,2));
b = X(:,size(X,2));

%回代求解x1,x2,...xn
x(n) = b(n,1)/a(n,n);
for i = n-1:-1:1
    x(i) = (b(i) - a(i,i+1:size(a,2)) * x(i+1:end,:)) / a(i,i);
end

end

高斯-乔丹消去法:

function [x] = GaussianJordan_elim(a, b)
% a is a coefficient matrix, b is a column vector storing values of a*x,
% and here the return value x should be a column vector storing values of x.
% We should suppose that matrix a is not a singular matrix.

X = [a b];
n = size(X,1);     % the number of rows
x = zeros(n,1);    % x stores the values of x

for i = 1:n
    [c,p] = max(abs(X(i:end,i)), [], 1);    % find the index p of the max element in column i
                                            % notice that (abs(X(i:end,i)) is a new matrix)
    X([i,p+i-1],:) = X([p+i-1,i],:);        % swap column i and column p+i
    for j = 1:n                             % to eliminate the ith element in rows i to n
        if j ~= i
            m = X(j,i) / X(i,i);            % get the multiplier
            X(j,:) = X(j,:) - m*X(i,:);     % eliminate the first non-zero element in row j
        else
            X(j,:) = X(j,:) / X(j,i);       % set the first non-zero element to 1
        end
    end
end

%得到 x1,x2,...xn
x = X(:,size(X,2));

end


实验一 误差分析 一、实验目的及要求 1.了误差分析对数值计算的重要性。 2.掌握避免或减小误差的基本方法。 二、实验设备 安装有C、C++或MATLAB的计算机。 三、实验原理 误差是指观测值真值之差,偏差是指观测值平均值之差。根据不同的算法,得到的结果的精度是不一样的。 四、实验内容及步骤 求方程ax2+bx+c=0的根,其中a=1,b= -(5×108+1),c=5×108 采用如下两种计算方案,在计算机上编程计算,将计算结果记录下来,并分析产生误差的原因。 ////////////////////////////// 实验二 Lagrange插值 一、实验目的及要求 1.掌握利用Lagrange插值法及Newton插值法求函数值并编程实现。 2.程序具有一定的通用性,程序运行时先输入节点的个数n,然后输入各节点的值( ),最后输入要求的自变量x的值,输出对应的函数值。 二、实验设备和实验环境 安装有C、C++或MATLAB的计算机。 三、算法描述 1. 插值的基本原理(求插值问题的基本思路) 构造一个函数y=f(x)通过全部节点,即 (i=0、1、… n) 再用f(x)计算插值,即 2. 拉格朗日(Lagrange)多项式插值 Lagrange插值多项式: 3.牛顿(Newton)插值公式 //////////////////////////////////// 实验三 高斯消去法方程组 一、实验目的及要求 1.掌握求线性方程组高斯消去法---列选主元在计算机上的算法实现。 2.程序具有一定的通用性,程序运行时先输入一个数n表示方程含有的未知数个数,然后输入每个线性方程的系数和常数,求出线性方程组。 二、实验设备和实验环境 安装有C、C++或MATLAB的计算机。 三、算法描述 1.高斯消去法基本思路 设有方程组 ,设 是可逆矩阵。高斯消去法的基本思想就是将矩阵的初等行变换作用于方程组的增广矩阵 ,将其中的 变换成一个上三角矩阵,然后求这个三角形方程组。 2. 利用列选主元高斯消去法线性方程组
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值