2019年1月27日 星期日

例題6-4 分解矩陣為L 與 U 矩陣

例題6-4
分解矩陣
           [ -6 , 14 , 2 ]
A=    [ 4   , 4     9 ]
          [ -3  , 2  ,13   ]

為L 與 U  矩陣


#/*********** 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 , -6.0 , 14.0 , 2.0 , 0.0 , 0.0 , 0.0   , 0.0 , 0.0,   0.0  ] ,
        [0.0 , 4.0 ,   4.0 ,  9.0,  0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 , -3.0 ,  2.0 , 13.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-4.py ============
Enter the order of matrix !  3
Enter all coefficients of matrix :
a[ 1 ][ 1 ]= -6.0 a[ 1 ][ 2 ]= 14.0 a[ 1 ][ 3 ]= 2.0
a[ 2 ][ 1 ]= 4.0 a[ 2 ][ 2 ]= 4.0 a[ 2 ][ 3 ]= 9.0
a[ 3 ][ 1 ]= -3.0 a[ 3 ][ 2 ]= 2.0 a[ 3 ][ 3 ]= 13.0


L matrix is 
L[ 1 ][ 1 ]= -6.0 L[ 1 ][ 2 ]= 0.0 L[ 1 ][ 3 ]= 0.0
L[ 2 ][ 1 ]= 4.0 L[ 2 ][ 2 ]= 13.3333 L[ 2 ][ 3 ]= 0.0
L[ 3 ][ 1 ]= -3.0 L[ 3 ][ 2 ]= -5.0 L[ 3 ][ 3 ]= 15.875


U matrix is 
U[ 1 ][ 1 ]= 1 U[ 1 ][ 2 ]= -2.3333 U[ 1 ][ 3 ]= -0.3333
U[ 2 ][ 1 ]= 0.0 U[ 2 ][ 2 ]= 1 U[ 2 ][ 3 ]= 0.775
U[ 3 ][ 1 ]= 0.0 U[ 3 ][ 2 ]= 0.0 U[ 3 ][ 3 ]= 1
>>> 

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...