{ "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": 20, "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": 21, "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": 22, "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": 23, "id": "a27970e0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0, 0.4, 0.25)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x1 = Mi*(N*x0+b)\n", "x1" ] }, { "cell_type": "code", "execution_count": 24, "id": "693dc08e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0, 0.4, 0.25)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x1 = M\\(N*x0+b)\n", "x1" ] }, { "cell_type": "code", "execution_count": 5, "id": "b1274220", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0499999999999998, 0.25, -0.09999999999999998)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x2 = Mi*(N*x1+b)\n", "x2" ] }, { "cell_type": "code", "execution_count": 6, "id": "e3495f57", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.1166666666666667, 0.17000000000000004, -0.07499999999999996)" ] }, "execution_count": 6, "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": 7, "id": "a9fadeab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((1.078125, 0.17187500000000003, -0.062499999999999986),\n", " (1.1166666666666667, 0.17000000000000004, -0.07499999999999996),\n", " 0.0405613818113294)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A\\b, x3, (A\\b - x3).norm().n()" ] }, { "cell_type": "code", "execution_count": 66, "id": "71ecf691", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.4659658624115353, 0.9746794344808964)" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MJ = matrix(RDF,[[1,0,0],[0,1,0],[0,0,1]])\n", "MG = matrix(RDF,[[1,0,0],[0,1,0],[1,0,1]])\n", "NJ = matrix([[0,1.9,1.6],[0,0,0.5],[-1,0,0.8]])\n", "NG = matrix([[0,1.9,1.6],[0,0,0.5],[0,0,0.8]])\n", "max([k.abs() for k in (MJ.inverse()*NJ).eigenvalues()]),max([k.abs() for k in (MG.inverse()*NG).eigenvalues()])" ] }, { "cell_type": "code", "execution_count": 67, "id": "da35aab2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\begin{array}{rrr}\n", "1.0 & -1.9 & -1.6 \\\\\n", "0.0 & 1.0 & -0.5 \\\\\n", "1.0 & 0.0 & 0.19999999999999996\n", "\\end{array}\\right) \\left(\\begin{array}{rrr}\n", "1.0 & -1.9 & -1.6 \\\\\n", "0.0 & 1.0 & -0.5 \\\\\n", "1.0 & 0.0 & 0.19999999999999996\n", "\\end{array}\\right)$$" ], "text/plain": [ "[ 1.0 -1.9 -1.6]\n", "[ 0.0 1.0 -0.5]\n", "[ 1.0 0.0 0.19999999999999996] [ 1.0 -1.9 -1.6]\n", "[ 0.0 1.0 -0.5]\n", "[ 1.0 0.0 0.19999999999999996]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(MJ - NJ,MG - NG)" ] }, { "cell_type": "code", "execution_count": null, "id": "1d6ec3d3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 38, "id": "042b07c5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4.1248854197645715, -0.7615571818318911, 0.636671762067317]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix(RDF,A(x=1)).eigenvalues()" ] }, { "cell_type": "markdown", "id": "bdefdaff", "metadata": {}, "source": [ "