2019年1月27日 星期日

範例6-5 利用 LU分解 矩陣

範例6-5 利用 LU分解 矩陣

 n=3
 2  1  -3
-1  3   2
 3  1  -3

n=4
8   1   3    2
2   9  -1  -2
1   3   2  -1
1   0   6    4


#/*********** LU Decomposition for solving linear equations **********/
#n=3
n=4
print("Enter the order of matrix ! ",n)

a=  [ [0.0 for i in range(n+1)]  for j in range(n+1) ]       #[n][n],
l=  [ [0.0 for i in range(n+1)]  for j in range(n+1) ]       #[n][n],
u=  [ [0.0 for i in range(n+1)]  for j in range(n+1) ]       #[n][n],
                                 
'''
a =[ [0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 , 0.0 ],
        [0.0 ,2.0 ,  1.0 , -3.0 , 0.0 , 0.0 , 0.0   , 0.0 , 0.0,   0.0  ] ,
        [0.0 ,-1.0 , 3.0 , 2.0,  0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,3.0 ,  1.0 , -3.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0  ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ]  ]


'''
a =[ [0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 , 0.0 ],
        [0.0 , 8.0 ,  1.0 ,  3.0 , 2.0 , 0.0 , 0.0   , 0.0 , 0.0,   0.0  ] ,
        [0.0 , 2.0 ,  9.0 ,  -1.0,  -2.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 , 1.0 ,  3.0 ,  2.0  ,  -1.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0] ,
        [0.0 , 1.0 ,  0.0 ,  6.0 , 4.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0  ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ]  ]

# //print the new matrix
print("Enter all coefficients of matrix :")
for i in range(1,n+1):
    for j in range(1,n+1):
        print( "a[",i,"][",j,"]=",round(a[i][j],4),'\t',end='')
    print("")

   
#//********** LU decomposition *****//
for k in range (1 , n+1):
    u[k][k]=1;
    for i in range (k, n+1):
        sum1=0.0
        for p in range (1, k):
            sum1+=l[i][p]*u[p][k]
        l[i][k]=a[i][k]-sum1

    for j in range (k+1 , n+1):
        sum1=0.0
        for p in range(1, k):
            sum1+=l[k][p]*u[p][j]
        u[k][j]=(a[k][j]-sum1)/l[k][k]
       
#//******** Displaying LU matrix**********//
print("\n\nL matrix is ")
for i in range(1,n+1):
    for j in range(1,n+1):
        print( "L[",i,"][",j,"]=",round(l[i][j],4),'\t',end='')
    print("")
       

print("\n\nU matrix is ")
for i in range(1,n+1):
    for j in range(1,n+1):
        print( "U[",i,"][",j,"]=",round(u[i][j],4),'\t',end='')
    print("")



========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-5-1.py ========
Enter the order of matrix !  4
Enter all coefficients of matrix :
a[ 1 ][ 1 ]= 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efficients of matrix :
a[ 1 ][ 1 ]= 2.0 a[ 1 ][ 2 ]= 1.0 a[ 1 ][ 3 ]= -3.0
a[ 2 ][ 1 ]= -1.0 a[ 2 ][ 2 ]= 3.0 a[ 2 ][ 3 ]= 2.0
a[ 3 ][ 1 ]= 3.0 a[ 3 ][ 2 ]= 1.0 a[ 3 ][ 3 ]= -3.0


L matrix is 
L[ 1 ][ 1 ]= 2.0 L[ 1 ][ 2 ]= 0.0 L[ 1 ][ 3 ]= 0.0
L[ 2 ][ 1 ]= -1.0 L[ 2 ][ 2 ]= 3.5 L[ 2 ][ 3 ]= 0.0
L[ 3 ][ 1 ]= 3.0 L[ 3 ][ 2 ]= -0.5 L[ 3 ][ 3 ]= 1.5714


U matrix is 
U[ 1 ][ 1 ]= 1 U[ 1 ][ 2 ]= 0.5 U[ 1 ][ 3 ]= -1.5
U[ 2 ][ 1 ]= 0.0 U[ 2 ][ 2 ]= 1 U[ 2 ][ 3 ]= 0.1429
U[ 3 ][ 1 ]= 0.0 U[ 3 ][ 2 ]= 0.0 U[ 3 ][ 3 ]= 1
>>> 

沒有留言:

張貼留言

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

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