部分旋转的高斯消元(列)

时间:2015-03-24 07:41:13

标签: python-3.x

我无法找出我犯的错误,有人能帮帮我吗?非常感谢!

import math

def GASSEM():
    a0 = [12,-2,1,0,0,0,0,0,0,0,13.97]
    a1 = [-2,12,-2,1,0,0,0,0,0,0,5.93]
    a2 = [1,-2,12,-2,1,0,0,0,0,0,-6.02]
    a3 = [0,1,-2,12,-2,1,0,0,0,0,8.32]
    a4 = [0,0,1,-2,12,-2,1,0,0,0,-23.75]
    a5 = [0,0,0,1,-2,12,-2,1,0,0,28.45]
    a6 = [0,0,0,0,1,-2,12,-2,1,0,-8.9]
    a7 = [0,0,0,0,0,1,-2,12,-2,1,-10.5]
    a8 = [0,0,0,0,0,0,1,-2,12,-2,10.34]
    a9 = [0,0,0,0,0,0,0,1,-2,12,-38.74]
    A = [a0,a1,a2,a3,a4,a5,a6,a7,a8,a9] # 10x11 matrix
    interchange=[0,0,0,0,0,0,0,0,0,0,0]

    for i in range (1,10):
        median = abs(A[i-1][i-1])
        for m in range (i,10): #pivoting
            if abs(A[m][i-1]) > median:
                median = abs(A[m][i-1])
                interchange = A[i-1]
                A[i-1] = A[m]
                A[m] = interchange
        for j in range(i,10): #creating upper triangle matrix
            A[j] = [A[j][k]-(A[j][i-1]/A[i-1][i-1])*A[i-1][k] for k in range(0,11)]
    for t in range (0,10): #print the upper triangle matrix
        print(A[t])

输出不是上三角矩阵,我在for循环中迷失了......

1 个答案:

答案 0 :(得分:1)

当我运行此代码时,输​​出为

[12, -2, 1, 0, 0, 0, 0, 0, 0, 0, 13.97]
[0.0, 11.666666666666666, -1.8333333333333333, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.258333333333333]
[0.0, 0.0, 11.628571428571428, -1.842857142857143, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -5.886428571428571]
[0.0, 0.0, -2.220446049250313e-16, 11.622235872235873, -1.8415233415233416, 1.0, 0.0, 0.0, 0.0, 0.0, 6.679281326781327]
[0.0, 0.0, -3.518258683818212e-17, 0.0, 11.622218698800275, -1.8415517150256329, 1.0, 0.0, 0.0, 0.0, -22.185475397706252]
[0.0, 0.0, 1.3530439218911067e-17, 0.0, 0.0, 11.62216239813737, -1.841549039580908, 1.0, 0.0, 0.0, 24.359991632712457]
[0.0, 0.0, 5.171101701700419e-18, 0.0, 0.0, 0.0, 11.622161705324444,   -1.84154850220678, 1.0, 0.0, -3.131238144426707]
[0.0, 0.0, -3.448243038110395e-19, 0.0, 0.0, 0.0, 0.0, 11.62216144141611, -1.8415485389982904, 1.0, -13.0921440313208]
[0.0, 0.0, -4.995725026226573e-19, 0.0, 0.0, 0.0, 0.0, 0.0, 11.622161418001749, -1.8415485322346454, 8.534950160892514]
[0.0, 0.0, -4.9488445836100553e-20, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 11.622161417603511, -36.26114362292296]

这实际上上三角形。 '非零'的绝对值。下三角形第三列中的条目均小于10e-15。鉴于其他值为1或更大,这些小数看起来像A[j][k] - (A[j][i-1]/A[i-1][i-1])*A[i-1][k]中的浮点减法误差,可以认为是0.没有更多的调查,我不知道为什么非零值是仅限于此专栏。

对于此数据,条件abs(A[m][i-1]) > median永远不会为真,因此不会测试if块代码。