2019年1月27日 星期日

例題 6-7 6-8 利用 L U 分解法 計算 行列式結果

例題 6-7  , 6-8 利用 L U 分解法 計算 行列式結果

'''
/*This is a program to display the
upper triangular matrix and lower triangular matrix
for any given matrix. The method used here is
LU decomposition method. This program works for matrices
of order equal to or below 10x10*/
'''
#n=3
n=4
print("\nENTER THE SIZE OF THE MATRIX(size<10)",n);
uppertriangle= [ [0.0 for i in range(n+1)]  for j in range(n+1) ]
lowertriangle= [ [0.0 for i in range(n+1)]  for j in range(n+1) ]
for i in range (0,n):
    lowertriangle[i][i]=1.0
'''
matrix=[[ 4.0 ,  -2.0 ,  1.0 ],
               [ 3.0 ,  0.0 ,  -5.0 ],
               [ 1.0 ,  -3.0 , -4.0]]
'''
matrix=[[2.00, 3.00 , 1.00 , 5.00],
               [6.00 , 13.00 ,5.00 ,19.00],
               [2.00 ,19.00 ,10.00, 23.00],
               [4.00 ,10.00 ,11.00, 31.00]]

for i in range(0 ,n):
    for j in range(0,n):
        print( "matrix[",i,"][",j,"]=",round(matrix[i][j],4),'\t',end='')
    print("")

#/*The algorithm used later is from the book Introduction to algorithms
#    by Thomas Cormen.For a better understaning one can
#    read this text under chapter Matrix Operations. */
for ctr1 in range(0 , n):
    uppertriangle[ctr1][ctr1]=matrix[ctr1][ctr1];
    for ctr2 in range(ctr1+1, n):
        uppertriangle[ctr1][ctr2]=matrix[ctr1][ctr2];
        lowertriangle[ctr2][ctr1]=matrix[ctr2][ctr1]/uppertriangle[ctr1][ctr1];

    for row in range (ctr1+1 , n):
        for column in range(ctr1+1 , n):
                    matrix[row][column]=matrix[row][column]-lowertriangle[row][ctr1]*uppertriangle[ctr1][column];

#+++++++++++++++++++++++++++++++++++   
print("UPPER TRIANGULAR MATRIX\n");
for i in range(0 ,n):
    for j in range(0,n):
        print( "uppertriangle[",i,"][",j,"]=",round(uppertriangle[i][j],4),'\t',end='')
    print("")
#+++++++++++++++++++++++++++++++++++   
print("LOWER TRIANGULAR MATRIX\n");
for i in range(0 ,n):
    for j in range(0,n):
        print( "lowertriangle[",i,"][",j,"]=",round(lowertriangle[i][j],4),'\t',end='')
    print("")






輸出畫面
======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-8-1.py ===========

ENTER THE SIZE OF THE MATRIX(size<10) 4
matrix[ 0 ][ 0 ]= 2.0 matrix[ 0 ][ 1 ]= 3.0 matrix[ 0 ][ 2 ]= 1.0 matrix[ 0 ][ 3 ]= 5.0
matrix[ 1 ][ 0 ]= 6.0 matrix[ 1 ][ 1 ]= 13.0 matrix[ 1 ][ 2 ]= 5.0 matrix[ 1 ][ 3 ]= 19.0
matrix[ 2 ][ 0 ]= 2.0 matrix[ 2 ][ 1 ]= 19.0 matrix[ 2 ][ 2 ]= 10.0 matrix[ 2 ][ 3 ]= 23.0
matrix[ 3 ][ 0 ]= 4.0 matrix[ 3 ][ 1 ]= 10.0 matrix[ 3 ][ 2 ]= 11.0 matrix[ 3 ][ 3 ]= 31.0
UPPER TRIANGULAR MATRIX

uppertriangle[ 0 ][ 0 ]= 2.0 uppertriangle[ 0 ][ 1 ]= 3.0 uppertriangle[ 0 ][ 2 ]= 1.0 uppertriangle[ 0 ][ 3 ]= 5.0
uppertriangle[ 1 ][ 0 ]= 0.0 uppertriangle[ 1 ][ 1 ]= 4.0 uppertriangle[ 1 ][ 2 ]= 2.0 uppertriangle[ 1 ][ 3 ]= 4.0
uppertriangle[ 2 ][ 0 ]= 0.0 uppertriangle[ 2 ][ 1 ]= 0.0 uppertriangle[ 2 ][ 2 ]= 1.0 uppertriangle[ 2 ][ 3 ]= 2.0
uppertriangle[ 3 ][ 0 ]= 0.0 uppertriangle[ 3 ][ 1 ]= 0.0 uppertriangle[ 3 ][ 2 ]= 0.0 uppertriangle[ 3 ][ 3 ]= 3.0
LOWER TRIANGULAR MATRIX

lowertriangle[ 0 ][ 0 ]= 1.0 lowertriangle[ 0 ][ 1 ]= 0.0 lowertriangle[ 0 ][ 2 ]= 0.0 lowertriangle[ 0 ][ 3 ]= 0.0
lowertriangle[ 1 ][ 0 ]= 3.0 lowertriangle[ 1 ][ 1 ]= 1.0 lowertriangle[ 1 ][ 2 ]= 0.0 lowertriangle[ 1 ][ 3 ]= 0.0
lowertriangle[ 2 ][ 0 ]= 1.0 lowertriangle[ 2 ][ 1 ]= 4.0 lowertriangle[ 2 ][ 2 ]= 1.0 lowertriangle[ 2 ][ 3 ]= 0.0
lowertriangle[ 3 ][ 0 ]= 2.0 lowertriangle[ 3 ][ 1 ]= 1.0 lowertriangle[ 3 ][ 2 ]= 7.0 lowertriangle[ 3 ][ 3 ]= 1.0
>>> 

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...