MATLAB 代码逐行解析:高斯消元法求解线性方程组及误差分析
首先清空命令窗口中的所有变量和之前的输出。
clc;
clear;
然后定义了一个4×4的矩阵 A 和一个4×1的向量 b。
A = [10 7 8 7; 7 5 6 5; 8 6 10 9; 7 5 9 10];
b =[32;23;33;31];
接着,定义了一个略微改变的矩阵 An,以及通过调用 Gauss 函数求解线性方程组 Ax=b 和 Anx=b 得到的解 x 和 xn。
An =[10 7 8.1 7.2; 7.08 5.04 6 5; 8 5.98 9.89 9; 6.99 5 9 9.98];
x = Gauss(A,b)
xn = Gauss(An,b)
然后输出矩阵 A 的行列式和特征值,并输出矩阵 A 的 2-范数。
disp('The det and eigvalues of A are:')
det(A),eig(A)
disp('The 2-norm of A is:')
cond(A,2)
接下来计算向量 dx 的值和 2-范数,以及相对误差 norm(dx,2)/norm(x,2) 和相对扰动 norm(An-A,2)/norm(A,2)。
disp('The value of dx is:')
dx =xn-x
disp('The 2-norm of dx is:')
norm(dx,2)
disp('The relative error of x is:')
norm(dx,2)/norm(x,2)
disp('The relative turbulance of A is:')
norm(An-A,2)/norm(A,2)
最终输出结果。
原文地址: https://www.cveoy.top/t/topic/n5Ql 著作权归作者所有。请勿转载和采集!