Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TeddyFirman authored May 31, 2021
0 parents commit 42e15b2
Showing 1 changed file with 173 additions and 0 deletions.
173 changes: 173 additions & 0 deletions Gauss Method.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number of unknowns: 4\n",
"Masukkan augmented matrix coefficients: \n",
"matrix[0][0] = 1\n",
"matrix[0][1] = -1\n",
"matrix[0][2] = 1\n",
"matrix[0][3] = -2\n",
"matrix[0][4] = 8\n",
"matrix[1][0] = 2\n",
"matrix[1][1] = -1\n",
"matrix[1][2] = 2\n",
"matrix[1][3] = -1\n",
"matrix[1][4] = 5\n",
"matrix[2][0] = -1\n",
"matrix[2][1] = 1\n",
"matrix[2][2] = 2\n",
"matrix[2][3] = -1\n",
"matrix[2][4] = 4\n",
"matrix[3][0] = 1\n",
"matrix[3][1] = 2\n",
"matrix[3][2] = 4\n",
"matrix[3][3] = 1\n",
"matrix[3][4] = 5\n",
"\n",
"Ketik Y untuk mengetahui hasilnya: Y\n",
"\n",
"Gauss Elimination\n",
"inisiasi matrix:\n",
" [[ 1. -1. 1. -2. 8.]\n",
" [ 2. -1. 2. -1. 5.]\n",
" [-1. 1. 2. -1. 4.]\n",
" [ 1. 2. 4. 1. 5.]]\n",
"\n",
"Transformation 1: \n",
"baris2 = row2 - 2.0 * baris1\n",
"baris3 = row3 - -1.0 * baris1\n",
"baris4 = row4 - 1.0 * baris1\n",
"Hasil:\n",
" [[ 1. -1. 1. -2. 8.]\n",
" [ 0. 1. 0. 3. -11.]\n",
" [ 0. 0. 3. -3. 12.]\n",
" [ 0. 3. 3. 3. -3.]]\n",
"\n",
"Transformation 2: \n",
"baris3 = row3 - 0.0 * baris2\n",
"baris4 = row4 - 3.0 * baris2\n",
"Hasil:\n",
" [[ 1. -1. 1. -2. 8.]\n",
" [ 0. 1. 0. 3. -11.]\n",
" [ 0. 0. 3. -3. 12.]\n",
" [ 0. 0. 3. -6. 30.]]\n",
"\n",
"Transformation 3: \n",
"baris4 = row4 - 1.0 * baris3\n",
"Hasil:\n",
" [[ 1. -1. 1. -2. 8.]\n",
" [ 0. 1. 0. 3. -11.]\n",
" [ 0. 0. 3. -3. 12.]\n",
" [ 0. 0. 0. -3. 18.]]\n",
"\n",
"Solusi: \n",
"x1 = 5.00\n",
"x2 = 7.00\n",
"x3 = -2.00\n",
"x4 = -6.00\n"
]
}
],
"source": [
"import numpy as np\n",
"import sys\n",
"\n",
"class Gauss:\n",
"\n",
" #Inisiasi Matrix\n",
" def start(self): #self variabel yg mewakili turunan dari objek itu sendiri\n",
" self.n = int(input(\"Enter the number of unknowns: \")) #memasukkan baris\n",
" self.A = np.zeros((self.n,self.n+1)) #pembuatan matriks #np zeros adalah fungsi mengembalikan larik baru dengan bentuk dan tipe tertentu, dengan nol #pembuatan kolom=baris+ 1\n",
" self.R = np.zeros((self.n,self.n+1)) #Matriks perkalian untuk kolom\n",
" self.x = np.zeros(self.n) #Solution array-nya\n",
"\n",
" print(\"Masukkan augmented matrix coefficients: \")\n",
" for i in range(self.n): #pengulangan baris\n",
" for j in range(self.n+1): #pengulangan kolom\n",
" self.A[i][j] = input(\"matrix[\"+ str(i) +\"][\"+ str(j) +\"] = \")\n",
" \n",
" #Gauss Elimination\n",
" def gaussProg(self):\n",
" print(f\"\\nGauss Elimination\\ninisiasi matrix:\\n {self.A}\")\n",
" for i in range(self.n-1): #loop kolom\n",
" if self.A[i][i] == 0:\n",
" sys.exit(\"Dibagi dengan 0(proses tidak dilanjutkan...)\") #kondisi system ketika membagi dengan 0\n",
" print(f\"\\nTransformation {i+1}: \")\n",
"\n",
" #Menghitung perkalian untuk kolom saat ini\n",
" for j in range(i+1, self.n):\n",
" self.R[j][i] = self.A[j][i] / self.A[i][i]\n",
" print(f\"baris{j+1} = row{j+1} - {self.R[j][i]} * baris{i+1}\")\n",
"\n",
" #Terapkan transformasi ke submatrix yang tersisa\n",
" for k in range(self.n+1):\n",
" for j in range(self.n):\n",
" self.A[j][k] = self.A[j][k] - self.R[j][i] * self.A[i][k]\n",
"\n",
" print(f\"Hasil:\\n {self.A}\")\n",
" \n",
" #Substitusi kembali\n",
" def Subs(self):\n",
" self.x[self.n-1] = self.A[self.n-1][self.n] / self.A[self.n-1][self.n-1]\n",
" for i in range(self.n-2,-1,-1):\n",
" self.x[i] = self.A[i][self.n]\n",
" for j in range(i+1, self.n):\n",
" self.x[i] = self.x[i] - self.A[i][j] * self.x[j]\n",
" self.x[i] = self.x[i] / self.A[i][i]\n",
"\n",
" #Tampilan/pemecahan\n",
" def display(self):\n",
" print(\"\\nSolusi: \")\n",
" for i in range(self.n):\n",
" print(\"x%d = %0.2f\" %(i+1, self.x[i]))\n",
"\n",
"g = Gauss() #manggil class\n",
"g.start() #mulai jalankan class\n",
"choice = input(\"\\nKetik Y untuk mengetahui hasilnya: \")\n",
"if choice == 'Y':\n",
" g.gaussProg() #jalankan fungsi gauss\n",
"else:\n",
" print(\"Anda menghentikan program\")\n",
" quit()\n",
"g.Subs() #jalankan fungsi subtitusi kembali\n",
"g.display() #jalankan fungsi tampilan"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 42e15b2

Please sign in to comment.