Pertemuan 3

From ccitonlinewiki
Jump to: navigation, search

Belajar python

Eliminasi Gauss- Jordan

Misalkan ax + by + cz = q\ dx + ey + fz = r\ gx + hy + iz = s\ [[File:]]


Python

import numpy as np from numpy import array

  1. Mendefinisikan Matrix [A]

A=array([[4, -2, 1], [-2, 4, -2], [1, -2, 4]], float)

  1. Mendefinisikan Matrix [B]

b = array ([[11], [-16], [ 17]], float)

c=array([[4, -2, 1, 11], [-2, 4, -2, -16], [1, -2, 4, 17]], float)

print ('Matrix dari persamaan adalah : ') print (c)

  1. define rows column

n=len(b) #number of rows and colomn, due too the matrix that is square matrix print ('n adalah ' + str(n))

for k in range (0,n-1):

   for i in  range (k+1,n):
       if A[i,k] != 0.0 :            #perintah jika sebuah baris yang akan di eliminasi, ada bagiannya yang tidak 0 maka akan di substract dengan pivot
           lam = A[i,k]/A[k,k]
           A[i, k+1:n] = A[i, k+1:n] - lam*A[k, k+1:n]
           b[i]=b[i] - lam*b[k]

x=np.zeros((3), float) for k in range (n-1,-1,-1):

   x[k]=(b[k]-np.dot(A[k,k+1:n], x[k+1:n]))/A[k,k]
   print (x[k])