You are on page 1of 2

1/5/2021 Untitled8 - Jupyter Notebook

NAMA : Faiz Aqsa Indrawan

NIM: 180401110

Quiz Analisa Numerik / 05-01-2021


In [4]: import numpy as np
A = np.array([(10,2,-1),
(-3,-6,2),
(1,1,-5)])
B = np.array([(27),
(-61.5),
(-21.5)])
x = np.linalg.solve(A,B)
print(x)

[ 0.97808765 12.06374502 6.90836653]

localhost:8888/notebooks/Untitled8.ipynb?kernel_name=python3 1/2
1/5/2021 Untitled8 - Jupyter Notebook

In [5]: # Gauss method


# to solve [A] . {x} = {b}
import numpy as np
# forward elimination
def forward(A, b, n):
for row in range (0, n-1):
for i in range (row+1, n):
factor = A[i,row] / A[row,row]
for j in range (row,n):
A[i,j] = A[i,j] - factor * A[row,j]
b[i] = b[i] - factor * b[row]
return A,b
def back(a, b, n):
x = np.zeros((n,1)) # aid of numpy
x[n-1] = b[n-1] / a[n-1, n-1]
for row in range (n-2, -1, -1):
sum = b[row]
for j in range (row+1, n):
sum = sum - a[row,j] * x[j]
x[row] = sum / a[row,row]
return x
def Gauss(A, b):
n = A.shape[0] # aid of numpy
A, b = forward(A, b, n)
return back(A, b, n)
# input matrices
A = np.array([[3,-0.1,-0.2], # numpy rather than list
[0.1,7,-0.3],
[0.3,-0.2,10]])
b = np.array([7.85,-19.3,71.4])
# [A] . {x} = {b}
# x is solved using Gauss(A,b)
print('[A] = \n',A)
print()
print('{b} = \n',b)
print()
x = Gauss(A,b)
print('{x} = \n',x)

File "<ipython-input-5-66ac0ded5c93>", line 7


for i in range (row+1, n):
^
IndentationError: expected an indented block

In [ ]:

localhost:8888/notebooks/Untitled8.ipynb?kernel_name=python3 2/2

You might also like