{ "cells": [ { "cell_type": "markdown", "id": "5d32decb", "metadata": {}, "source": [ "# Métodos iterativos \n", "#### https://meet.noysi.com/metodosnumericos1" ] }, { "cell_type": "markdown", "id": "9d51f4b8", "metadata": {}, "source": [ "Vamos a aplicar el método de Jacobi para resolver el sistema\n", "\\begin{cases}\n", "3 x - y + z = 3,\\\\\n", "x + 5 y - z = 2,\\\\\n", "x + y + 4 z = 1.\n", "\\end{cases}\n" ] }, { "cell_type": "markdown", "id": "da65535b", "metadata": {}, "source": [ "Definimos las matrices del método:" ] }, { "cell_type": "code", "execution_count": 1, "id": "3f720df4", "metadata": {}, "outputs": [], "source": [ "A = matrix(RDF,[[3,-1,1],[1,5,-1],[1,1,4]])\n", "M = matrix(RDF,[[3,0,0],[0,5,0],[0,0,4]])\n", "N = M - A\n", "b = vector(RDF,[3,2,1])" ] }, { "cell_type": "markdown", "id": "5fd0f394", "metadata": {}, "source": [ "En este caso está justificado calcular la inversa, por ser inmediata y no añadir mucho error." ] }, { "cell_type": "code", "execution_count": 2, "id": "0f5fe495", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{rrr}\n", "0.3333333333333333 & 0.0 & 0.0 \\\\\n", "0.0 & 0.2 & 0.0 \\\\\n", "0.0 & 0.0 & 0.25\n", "\\end{array}\\right)$$" ], "text/plain": [ "[0.3333333333333333 0.0 0.0]\n", "[ 0.0 0.2 0.0]\n", "[ 0.0 0.0 0.25]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Se podría calcular directamente\n", "Mi = matrix.diagonal([k^-1 for k in M.diagonal()], sparse=true)\n", "show(Mi)" ] }, { "cell_type": "markdown", "id": "12d43b88", "metadata": {}, "source": [ "Tomamos como aproximación inicial el vector nulo:" ] }, { "cell_type": "code", "execution_count": 3, "id": "01c8ca84", "metadata": {}, "outputs": [], "source": [ "x0 = vector([0,0,0])" ] }, { "cell_type": "markdown", "id": "c4dea0ed", "metadata": {}, "source": [ "Y aplicamos el método" ] }, { "cell_type": "code", "execution_count": 6, "id": "a27970e0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0, 0.4, 0.25)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x1 = Mi*(N*x0+b)\n", "x1" ] }, { "cell_type": "code", "execution_count": 7, "id": "494391b7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0, 0.4, 0.25)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x1 = M\\(N*x0+b)\n", "x1" ] }, { "cell_type": "code", "execution_count": 8, "id": "b1274220", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0499999999999998, 0.25, -0.09999999999999998)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x2 = Mi*(N*x1+b)\n", "x2" ] }, { "cell_type": "code", "execution_count": 9, "id": "e3495f57", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.1166666666666667, 0.17000000000000004, -0.07499999999999996)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x3 = Mi*(N*x2+b)\n", "x3" ] }, { "cell_type": "markdown", "id": "55540f05", "metadata": {}, "source": [ "Comparamos con la solución exacta." ] }, { "cell_type": "code", "execution_count": 11, "id": "a9fadeab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((1.078125, 0.17187500000000003, -0.062499999999999986),\n", " (1.1166666666666667, 0.17000000000000004, -0.07499999999999996),\n", " 0.040561381811329435)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A\\b, x3, (A\\b - x3).norm()" ] }, { "cell_type": "code", "execution_count": null, "id": "042b07c5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "bdefdaff", "metadata": {}, "source": [ "