2019年1月27日 星期日

例題 6-6 利用 LU 分解法 求解聯立方程式

例題 6-6 利用 LU 分解法 求解聯立方程式

 n=4
       [1.0   1.0   0.0   1.0  ]           [  2.0 ]
 a = [2.0   1.0   -1.0  1.0  ]    b=  [  1.0 ]
       [4.0   -1.0   -2.0  2.0]           [  0.0 ]
       [3.0  -1.0  -1.0   2.0 ]           [ -3.0 ]

 n=3

      [-1    1   2  ]               [ 2 ]
a=  [ 3   -1   1  ]        b = [ 6 ]
      [-1    3   4  ]               [ 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],

b= [0.0 for i in range(n+1)];                                         
x= [0.0 for i in range(n+1)];
z= [0.0 for i in range(n+1)];                                         

a =[ [0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 , 0.0 ],
        [0.0 ,-1.0 ,  1.0 , 2.0 , 2.0 , 0.0 , 0.0   , 0.0 , 0.0,   0.0  ] ,
        [0.0 ,3.0 , -1.0 , 1.0,  6.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,-1.0 ,  3.0 , 4.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 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ]  ]

b=[0.0 , 2.0 , 6.0 , 4.0]
'''
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 ,  0.0 ,  0.0 , 0.0 , 0.0 , 0.0   , 0.0 , 0.0,   0.0  ] ,
        [0.0 , 1.0 ,  1.5 ,  0.0,  0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 , 0.0 ,  -3.0 , 0.5 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0] ,
        [0.0 , 2.0 ,   -2.0 , 1.0 , 1.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0  ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ] ,
        [0.0 ,0.0 ,   0.0 , 0.0 , 0.0  , 0.0 , 0.0 ,   0.0 , 0.0,   0.0 ]  ]

b=[0.0 , 3.0 , 4.5 , -6.6 , 0.8]
'''

# //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("")


print("Enter elements of b matrix")
for i in range(1,n+1):
    print( "b[",i,"]=",round(b[i],4),'\t',end='')

   
#//********** 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\nLU 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("LU 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("")

#//***** FINDING Z; LZ=b*********//
for i in range (1, n+1):
    #//forward subtitution method
    sum1=0;
    for p in range (1,i):
        sum1+=l[i][p]*z[p];
    z[i]=(b[i]-sum1)/l[i][i];

#//********** FINDING X; UX=Z***********//
for i in range (n, 0 , -1):
    sum1=0
    for p in range (n , i , -1):
        sum1+=u[i][p]*x[p]
    x[i]=(z[i]-sum1)/u[i][i]

#//*********** DISPLAYING SOLUTION**************//
print("Set of solution is")
for i in range(1,n+1):
    print( "x[",i,"]=",round(x[i],4),'\t',end='')





========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-5-2.py ===========
Enter the order of matrix !  4
Enter all coefficients of matrix :
a[ 1 ][ 1 ]= 2.0 a[ 1 ][ 2 ]= 0.0 a[ 1 ][ 3 ]= 0.0 a[ 1 ][ 4 ]= 0.0
a[ 2 ][ 1 ]= 1.0 a[ 2 ][ 2 ]= 1.5 a[ 2 ][ 3 ]= 0.0 a[ 2 ][ 4 ]= 0.0
a[ 3 ][ 1 ]= 0.0 a[ 3 ][ 2 ]= -3.0 a[ 3 ][ 3 ]= 0.5 a[ 3 ][ 4 ]= 0.0
a[ 4 ][ 1 ]= 2.0 a[ 4 ][ 2 ]= -2.0 a[ 4 ][ 3 ]= 1.0 a[ 4 ][ 4 ]= 1.0
Enter elements of b matrix
b[ 1 ]= 3.0 b[ 2 ]= 4.5 b[ 3 ]= -6.6 b[ 4 ]= 0.8

LU matrix is 
L[ 1 ][ 1 ]= 2.0 L[ 1 ][ 2 ]= 0.0 L[ 1 ][ 3 ]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= 0.0
L[ 2 ][ 1 ]= 3.0 L[ 2 ][ 2 ]= 2.0 L[ 2 ][ 3 ]= 0.0
L[ 3 ][ 1 ]= -1.0 L[ 3 ][ 2 ]= 2.0 L[ 3 ][ 3 ]= -5.0
LU matrix is 
U[ 1 ][ 1 ]= 1 U[ 1 ][ 2 ]= -1.0 U[ 1 ][ 3 ]= -2.0
U[ 2 ][ 1 ]= 0.0 U[ 2 ][ 2 ]= 1 U[ 2 ][ 3 ]= 3.5
U[ 3 ][ 1 ]= 0.0 U[ 3 ][ 2 ]= 0.0 U[ 3 ][ 3 ]= 1
Set of solution is
x[ 1 ]= 1.0 x[ 2 ]= -1.0 x[ 3 ]= 2.0
>>>

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...