Tugas 1 - Sistem Linier#

menggambar persamaan bidang dalam ruang 3D#

Dalam penyelesaian sistem persamaan linier dengan menggunakan metode eliminasi Gauss, kita menggunakan contoh persamaan berikut

\[\begin{split} A = \begin{bmatrix} 8x_1+9x_2+x_3=19\\ x_1+2x_2+3x_3=9 \\ 2x_1+x_2+x_3=5\\ \end{bmatrix} \end{split}\]

contoh dengan python

import numpy as np
A=np.array([[8,9,1,19],[1,2,3,9],[2,1,1,5]])
print(A)
[[ 8  9  1 19]
 [ 1  2  3  9]
 [ 2  1  1  5]]

Mendefinisikan fungsi operasi baris eliminasi GAUSS#

  1. RowSwap: Fungsi ini bertugas untuk menukar posisi dua baris dalam sebuah matriks. Dalam proses eliminasi Gauss, ini digunakan untuk mengatur baris sehingga elemen diagonal utama tidak nol.

  2. RowScale: Fungsi ini mengalikan satu baris dalam matriks dengan skalar tertentu. Hal ini berguna dalam proses eliminasi Gauss untuk membuat elemen diagonal utama menjadi satu.

  3. RowAdd: Fungsi ini menambahkan hasil perkalian satu baris dengan skalar ke baris lain dalam matriks. Ini digunakan dalam eliminasi Gauss untuk mengubah elemen di bawah diagonal utama menjadi nol.

def gauss_elimination(matrix):
    """
    Perform Gaussian Elimination on the given matrix.
    """
    n = len(matrix)


    for i in range(n):

        max_row = i
        for k in range(i + 1, n):
            if abs(matrix[k][i]) > abs(matrix[max_row][i]):
                max_row = k
        matrix[i], matrix[max_row] = matrix[max_row], matrix[i]


        for j in range(i + 1, n):
            factor = matrix[j][i] / matrix[i][i]
            for k in range(i, n + 1):
                matrix[j][k] -= factor * matrix[i][k]


    x = [0] * n
    for i in range(n - 1, -1, -1):
        x[i] = matrix[i][n] / matrix[i][i]
        for j in range(i):
            matrix[j][n] -= matrix[j][i] * x[i]

    return x


matrix = [
    [8,9,1,19],
    [1,2,3,9],
    [2,1,1,5]
]

solution = gauss_elimination(matrix)
print("Solution:", solution)
Solution: [1.0, 1.0, 2.0]
import numpy as np

def RowSwap(A,k,l):
# =============================================================================
#     A is a NumPy array.  RowSwap will return duplicate array with rows
#     k and l swapped.
# =============================================================================
    m = A.shape[0]  # m is number of rows in A
    n = A.shape[1]  # n is number of columns in A

    B = np.copy(A).astype('float64')

    for j in range(n):
        temp = B[k][j]
        B[k][j] = B[l][j]
        B[l][j] = temp

    return B

def RowScale(A,k,scale):
# =============================================================================
#     A is a NumPy array.  RowScale will return duplicate array with the
#     entries of row k multiplied by scale.
# =============================================================================
    m = A.shape[0]  # m is number of rows in A
    n = A.shape[1]  # n is number of columns in A

    B = np.copy(A).astype('float64')

    for j in range(n):
        B[k][j] *= scale

    return B

def RowAdd(A,k,l,scale):
# =============================================================================
#     A is a numpy array.  RowAdd will return duplicate array with row
#     l modifed.  The new values will be the old values of row l added to
#     the values of row k, multiplied by scale.
# =============================================================================
    m = A.shape[0]  # m is number of rows in A
    n = A.shape[1]  # n is number of columns in A

    B = np.copy(A).astype('float64')

    for j in range(n):
        B[l][j] += B[k][j]*scale

    return B
import numpy as np
a=np.array([[8,9,1,19],[1,2,3,9],[2,1,1,5]])
print(a)
[[ 8  9  1 19]
 [ 1  2  3  9]
 [ 2  1  1  5]]
a1= RowAdd(a,2,0,-4)
print(a1)
[[ 0.  5. -3. -1.]
 [ 1.  2.  3.  9.]
 [ 2.  1.  1.  5.]]
a2= RowAdd(a1,1,2,-2)
print(a2)
[[  0.   5.  -3.  -1.]
 [  1.   2.   3.   9.]
 [  0.  -3.  -5. -13.]]
a3 = RowSwap(a2,0,1)
print(a3)
[[  1.   2.   3.   9.]
 [  0.   5.  -3.  -1.]
 [  0.  -3.  -5. -13.]]
a4 = RowScale(a3,1,3)
print(a4)
[[  1.   2.   3.   9.]
 [  0.  15.  -9.  -3.]
 [  0.  -3.  -5. -13.]]
a5 = RowScale(a4,2,5)
print(a5)
[[  1.   2.   3.   9.]
 [  0.  15.  -9.  -3.]
 [  0. -15. -25. -65.]]
a6 = RowAdd(a5,1,2,1)
print(a6)
[[  1.   2.   3.   9.]
 [  0.  15.  -9.  -3.]
 [  0.   0. -34. -68.]]
a7 = RowScale(a6,1,1/3)
print(a7)
[[  1.   2.   3.   9.]
 [  0.   5.  -3.  -1.]
 [  0.   0. -34. -68.]]
x3 = 2
print('x3:', x3)
x2 = 5 / ((3*x3) -1)
print("x2 :",round(x2))
x1 = 1 + (2*x2) + (3*x3)
print("x1 :",x1/9)
x3: 2
x2 : 1
x1 : 1.0
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Koefisien untuk masing-masing persamaan bidang
a1, b1, c1, r1 = 1, 2, 3, 9
a2, b2, c2, r2 = 0, 1, 13, 27
a3, b3, c3, r3 = 0, 0, 1, 2

# Buat data x, y
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)

# Buat grid x, y
X, Y = np.meshgrid(x, y)

# Hitung nilai z sesuai dengan persamaan bidang
Z1 = (r1 - a1*X - b1*Y) / c1
Z2 = (r2 - a2*X - b2*Y) / c2
Z3 = (r3 - a3*X - b3*Y) / c3

# Plot persamaan bidang dalam tiga dimensi
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot bidang
ax.plot_surface(X, Y, Z1, alpha=0.5, rstride=100, cstride=100, color='r')
ax.plot_surface(X, Y, Z2, alpha=0.5, rstride=100, cstride=100, color='g')
ax.plot_surface(X, Y, Z3, alpha=0.5, rstride=100, cstride=100, color='b')

# Titik perpotongan
A = np.array([[a1, b1, c1], [a2, b2, c2], [a3, b3, c3]])
B = np.array([r1, r2, r3])
intersect = np.linalg.solve(A, B)
ax.scatter(intersect[0], intersect[1], intersect[2], color='black', s=100, label='Titik Perpotongan')

# Label sumbu
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Tampilkan legenda
ax.legend()

# Tampilkan plot
plt.show()
_images/55665a78f4229f4edc5d12f9b61bc750947475742031f567f46a5c50842baed5.png