2019年1月25日 星期五

例題 6-3 利用 高斯--喬登理則 求線性代數解


#/********** Gauss Jordan method for inverse matrix ****************/
'''
例題 6-3 利用 高斯--喬登理則 求線性代數解
/* Based on Gauss-Jordan Method to
 * solve n x n system of linear algebraic  equations.
 * Implementation for Gauss-Jordan Elimination Method
 '''
print("\nEnter the no. of equations\n")
n=3
print(n)
#input the no. of equations

x= [0.0 for i in range(n+1)];                                             # //declare an array to store the elements of augmented-matrix
print("\nEnter the elements of the augmented-matrix row-wise:\n")
a=[  [0.0 ,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  ] ,
        [0.0 ,3.0 , -1.0 , 1.0,  6.0  , 0.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 ] ,      ]

#陣列中 從1 開始

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



#/************** partial pivoting **************/
for i in range (n, 1, -1):
    if(a[i-1][1]<a[i][1]):
        for j in range (1, n*2+1):
            d=a[i][j]
            a[i][j]=a[i-1][j]
            a[i-1][j]=d


print("pivoted output: ")
for i  in range (1,n+1) :     # //print the new matrix
    for j in range (1, n+2) :
         print( round(a[i][j],4),"\t",end='')
    print("\n")
 
#/********** reducing to diagonal  matrix ***********/
for i in range (1,n+1):
    for j in range (1, n*2+ 1):
        if(j!=i):
            d=a[j][i]/a[i][i];
            for k in range (1,n*2+1):
                a[j][k]-=a[i][k]*d;
 #/************** reducing to unit matrix *************/
for i in range (1,n+1):
    d=a[i][i];
    for j in range (1,n*2+1):
            a[i][j]=a[i][j]/d

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

for i  in range (1,8) :     # //print the new matrix
    for j  in range (1,11) :     # //print the new matrix
        print(round(a[i][j],4),"\t",end='')
    print()
 



======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-3-2.py ==========

Enter the no. of equations

3

Enter the elements of the augmented-matrix row-wise:

-1.0 1.0 2.0 2.0

3.0 -1.0 1.0 6.0

-1.0 3.0 4.0 4.0

pivoted output: 
3.0 -1.0 1.0 6.0

-1.0 1.0 2.0 2.0

-1.0 3.0 4.0 4.0

your solutions: 
1.0

-1.0

2.0

1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 -1.0 0.0 0.0 0.0 0.0 0.0 0.0
-0.0 -0.0 1.0 2.0 -0.0 -0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
>>>

n=3

a=[[ 2.0 ,  -1.5 , 3.0,  1.0],
      [-1.0 ,  0.0,  2.0,  3.0],

      [4.0 , -4.5 , 5.0 , 1.0]]


======= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-2-1.py ===========

Enter the elements of the augmented-matrix row-wise:

2.0 -1.5 3.0 1.0

-1.0 0.0 2.0 3.0

4.0 -4.5 5.0 1.0

In Matrix form : 

2.0 0.0 0.0 -2.0

0.0 -0.75 0.0 0.0

0.0 0.0 -8.0 -8.0



Solution is = 
-1.0
-0.0
1.0

>>> 



沒有留言:

張貼留言

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

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