数值分析作业(第二章):代码+手写计算

时间:2024-10-03 07:11:47

《数值计算方法》丁丽娟-数值实验作业-第二章(MATLAB)

作业P58: 1 ,2,3,6,8(1), 12, 13
数值实验P61: 2, 3

数值实验(第二章)

代码仓库:https://github.com/sylvanding/bit-numerical-analysis-hw

P61. 2

试用MATLAB软件编程实现追赶法求解三对角方程组的算法,并考虑如下梯形电阻电路问题:

其中电路中的各个电流 { i 1 , i 2 , … , i 8 } \{i_1, i_2, \dots, i_8\} {i1,i2,,i8}满足下列线性方程组:
2 i 1 − 2 i 2 = V / R − 2 i 1 + 5 i 2 − 2 i 3 = 0 − 2 i 2 + 5 i 3 − 2 i 4 = 0 − 2 i 3 + 5 i 4 − 2 i 5 = 0 − 2 i 4 + 5 i 5 − 2 i 6 = 0 − 2 i 5 + 5 i 6 − 2 i 7 = 0 − 2 i 6 + 5 i 7 − 2 i 8 = 0 − 2 i 7 + 5 i 8 = 0 \begin{aligned} 2 i_1-2 i_2 &=V / R \\ -2 i_1+5 i_2-2 i_3&=0 \\ -2 i_2+5 i_3-2 i_4&=0 \\ -2 i_3+5 i_4-2 i_5&=0 \\ -2 i_4+5 i_5-2 i_6&=0 \\ -2 i_5+5 i_6-2 i_7&=0 \\ -2 i_6+5 i_7-2 i_8&=0 \\ -2 i_7+5 i_8&=0 \end{aligned} 2i12i22i1+5i22i32i2+5i32i42i3+5i42i52i4+5i52i62i5+5i62i72i6+5i72i82i7+5i8=V/R=0=0=0=0=0=0=0
V = 220 V , R = 27 Ω V=220\text{V}, R=27\Omega V=220V,R=27Ω,求各段电路的电流量。

实验内容、步骤及结果

chap-2\thomas_algorithm.m:

function x = thomas_algorithm(a, b, c, d)
    % a: sub-diagonal (length n-1)
    % b: main diagonal (length n)
    % c: super-diagonal (length n-1)
    % d: right-hand side (length n)
    
    n = length(b);
    
    % Forward elimination
    for i = 2:n
        w = a(i-1) / b(i-1);
        b(i) = b(i) - w * c(i-1);
        d(i) = d(i) - w * d(i-1);
    end
    
    % Back substitution
    x = zeros(n, 1);
    x(n) = d(n) / b(n);
    for i = n-1:-1:1
        x(i) = (d(i) - c(i) * x(i+1)) / b(i);
    end
end

chap-2\Q2.m:

% Define input parameters
a = -2;
a = repmat(a, 1, 7);
b = [2, 5, 5, 5, 5, 5, 5, 5];
c = a;
V = 220;
R = 27;
d = [V/R, 0, 0, 0, 0, 0, 0, 0];

% Call the function
x = thomas_algorithm(a, b, c, d);

% Display the output
disp(x);
实验结果分析
>> Q2
    8.1478
    4.0737
    2.0365
    1.0175
    0.5073
    0.2506
    0.1194
    0.0477

所求电阻输出如上。

P61. 3

方程组的性态和矩阵的条件数的实验。设有线性方程组 A x = b Ax=b Ax=b,其中系数矩阵 A = ( a i j ) n × n A=(a_{ij})_{n\times n} A=(aij)n×n的元素分别为 a i j = x i j − 1 ( x i = 1 + 0.1 i , i , j = 1 , 2 , ⋯   , n ) a_{i j}=x_i^{j-1}\left(x_i=1+0.1 i, i, j=1,2, \cdots, n\right) aij=xij1(xi=1+0.1i,i,j=1,2,,n) a i j = 1 i + j − 1 ( i , j = 1 , 2 , ⋯   , n ) a_{i j}=\frac{1}{i+j-1}(i, j=1,2, \cdots, n) aij=i+j11(i,j=1,2,,n),右端向量 b = ( ∑ j = 1 n a 1 j , ∑ j = 1 n a 2 j , ⋯   , ∑ j = 1 n a n j ) T \boldsymbol{b}=\left(\sum_{j=1}^n a_{1 j}, \sum_{j=1}^n a_{2 j}, \cdots, \sum_{j=1}^n a_{n j}\right)^{\mathrm{T}} b=(j=1na1j,j=1na2j,,j=1nanj)T. 利用MATLAB中的库函数,

  1. n = 5 , 10 , 20 n=5,10,20 n=5,10,20,分别求出系数矩阵的2-条件数,判别它们是否是病态阵?随着 n n n的增大,矩阵性态的变化如何?
  2. 分别取 n = 5 , 10 , 20 n=5,10,20 n=5,10,20,解上述两个线性方程组 A x = b Ax=b Ax