Last active
March 8, 2022 08:39
-
-
Save DianSano/394e10b29758edab6186745b9eb72cf5 to your computer and use it in GitHub Desktop.
SPL_gauss_elim.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "SPL_gauss_elim.ipynb", | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyMgL7Zi7ePrYHZB4kg+iczh", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/DianSano/394e10b29758edab6186745b9eb72cf5/spl_gauss_elim.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "YtKggsj2oLeQ", | |
| "outputId": "87e396ba-6e0f-46e9-cf39-04fca1385776" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "[[1. 1. 1. ]\n", | |
| " [0. 2. 2. ]\n", | |
| " [0. 0. 2.33333333]]\n", | |
| "[4. 6. 2.33333333]\n", | |
| "Gaussian Elimination \n", | |
| "The solution of the system is:\n", | |
| "[1. 2. 1.]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from numpy import array, zeros, fabs, linalg\n", | |
| "\n", | |
| "a = array([[1, 1, 1],\n", | |
| " [1, -1, -1],\n", | |
| " [2, 8, 1]], float)\n", | |
| "#the b matrix constant terms of the equations \n", | |
| "b = array([4, -2, 19], float)\n", | |
| "\n", | |
| "n = len(b)\n", | |
| "x = zeros(n, float)\n", | |
| "\n", | |
| "#first loop specifys the fixed row\n", | |
| "for k in range(n-1):\n", | |
| " if fabs(a[k,k]) < 1.0e-12:\n", | |
| " # if statements checks if the pivot element is zero\n", | |
| " # and swap it with the appropriate row\n", | |
| " for i in range(k+1, n): \n", | |
| " if fabs(a[i,k]) > fabs(a[k,k]):\n", | |
| " a[[k,i]] = a[[i,k]] # Note a[[0,1]] is not the same to a[0,1]\n", | |
| " b[[k,i]] = b[[i,k]]\n", | |
| " break\n", | |
| "\n", | |
| " #applies the elimination below the fixed row\n", | |
| "\n", | |
| " for i in range(k+1,n):\n", | |
| " if a[i,k] == 0:continue\n", | |
| "\n", | |
| " factor = a[k,k]/a[i,k]\n", | |
| " for j in range(k,n):\n", | |
| " a[i,j] = a[k,j] - a[i,j]*factor\n", | |
| " #we also calculate the b vector of each row\n", | |
| " b[i] = b[k] - b[i]*factor\n", | |
| "print(a)\n", | |
| "print(b)\n", | |
| "\n", | |
| "\n", | |
| "x[n-1] = b[n-1] / a[n-1, n-1]\n", | |
| "for i in range(n-2, -1, -1):\n", | |
| " sum_ax = 0\n", | |
| " \n", | |
| " for j in range(i+1, n):\n", | |
| " sum_ax += a[i,j] * x[j]\n", | |
| " \n", | |
| " x[i] = (b[i] - sum_ax) / a[i,i]\n", | |
| "\n", | |
| "print(\"Gaussian Elimination \")\n", | |
| "print(\"The solution of the system is:\")\n", | |
| "print(x)\n" | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment