{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "metadata": { "id": "361PFs2xFbvk" }, "source": [ "# Importando a biblioteca NumPy\n", "import numpy as np\n", "import sys" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "2rkWTcX7FeX1", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a6e08efa-a4a1-415e-e521-ad758e588372" }, "source": [ "# Lendo o número de incógnitas\n", "n = int(input('Indique o número de variáveis do seu problema: '))" ], "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Indique o número de variáveis do seu problema: 3\n" ] } ] }, { "cell_type": "code", "metadata": { "id": "de9YJU4EFfrb" }, "source": [ "# Montando a matriz de tamanho N x N+1 e inicializando com zero\n", "# para armazenar a matriz aumentada\n", "a = np.zeros((n,n+1))" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "NHt6IFAEFhGV" }, "source": [ "# Montando a matriz de tamanho N x 1 e inicializando com zero\n", "# para armazenar o vetor solução\n", "x = np.zeros(n)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "GTal5qp_FjVb", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "76d94491-5d48-4885-fcaa-9ee93df6353c" }, "source": [ "# Lendo os coeficientes da matriz aumentada\n", "print('Enter 1Augmented Matrix Coefficients:')\n", "for i in range(n):\n", " for j in range(n+1):\n", " a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))\n" ], "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter 1Augmented Matrix Coefficients:\n", "a[0][0]=1\n", "a[0][1]=1\n", "a[0][2]=1\n", "a[0][3]=6\n", "a[1][0]=1\n", "a[1][1]=-2\n", "a[1][2]=2\n", "a[1][3]=3\n", "a[2][0]=-2\n", "a[2][1]=3\n", "a[2][2]=0\n", "a[2][3]=4\n" ] } ] }, { "cell_type": "code", "metadata": { "id": "2iMSGJc3FlIv" }, "source": [ "# Aplicando o método de Gauss\n", "for i in range(n):\n", " if a[i][i] == 0.0:\n", " sys.exit('Divide by zero detected!')\n", "\n", " for j in range(i+1, n):\n", " ratio = a[j][i]/a[i][i]\n", "\n", " for k in range(n+1):\n", " a[j][k] = a[j][k] - ratio * a[i][k]" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "0RuJccoUFoA5" }, "source": [ "# Aplicando o Back Substitution\n", "x[n-1] = a[n-1][n]/a[n-1][n-1]\n", "\n", "for i in range(n-2,-1,-1):\n", " x[i] = a[i][n]\n", "\n", " for j in range(i+1,n):\n", " x[i] = x[i] - a[i][j]*x[j]\n", "\n", " x[i] = x[i]/a[i][i]" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "rBfjovrdFqFL", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "71ec5b19-67ed-414e-d3c0-b4771087de65" }, "source": [ "# Mostrando a solução\n", "print('\\nRequired solution is: ')\n", "for i in range(n):\n", " print('X%d = %0.2f' %(i,x[i]), end = '\\t')" ], "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "Required solution is: \n", "X0 = 1.00\tX1 = 2.00\tX2 = 3.00\t" ] } ] } ] }