2019年1月25日 星期五

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

'''
    例題 6-2 利用 高斯--喬登理則 求線性代數解
 * Based on Gauss-Jordan Method to
 * solve n x n system of linear algebraic  equations.
 * Implementation for Gauss-Jordan Elimination Method
 '''
n=3
#input the no. of equations
print("\nEnter the elements of the augmented-matrix row-wise:\n")
a=[[-1.0 ,  1.0 , 2.0 , 2.0],
      [3.0 , -1.0 , 1.0,  6.0],
      [-1.0 ,  3.0 , 4.0 , 4.0]]

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

for i in range(0 ,n):
    for j in range(0, n):
        if(i!=j):
            t=a[j][i]/a[i][i]
            for k in range(0 , n+1):
                a[j][k]=a[j][k]-(a[i][k]*t);

print("In Matrix form : \n");
for i  in range (0,n) :     # 
    for j in range (0, n+1) :
         print( round( a[i][j],4),"\t",end='')
    print("\n")

   
print("\n\nSolution is = ");
for i in range (0 , n ):
    print(round(a[i][3]/a[i][i],4)) 


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

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

In Matrix form : 

-1.0 0.0 0.0 -1.0

0.0 2.0 0.0 -2.0

0.0 0.0 -5.0 -10.0



Solution is = 
1.0
-1.0
2.0
>>> 

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...