444 lines
12 KiB
Plaintext
444 lines
12 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "b680955d",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### Alumno: Isaac Mejia Flores\n",
|
||
"#### Definicion de la logica para la correcta solucion y clasificacion de los sistemas de ecuaciones usando numpy y sympy "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"id": "49dc1fac",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"import sympy as sp\n",
|
||
"\n",
|
||
"\n",
|
||
"def solve_equations_sp(equations, variables):\n",
|
||
" try:\n",
|
||
" solutions = sp.solve(equations, variables, dict=True)\n",
|
||
"\n",
|
||
" if not solutions:\n",
|
||
" return \"El sistema no tiene solución\"\n",
|
||
"\n",
|
||
" sol = solutions[0]\n",
|
||
"\n",
|
||
" free_vars = []\n",
|
||
" dependent_vars = []\n",
|
||
"\n",
|
||
" for var in variables:\n",
|
||
" if var in sol:\n",
|
||
" expr = sol[var]\n",
|
||
"\n",
|
||
" # Detecta si la solución depende de otras variables\n",
|
||
" # para determinar variables libres\n",
|
||
" if any(sym in variables for sym in expr.free_symbols):\n",
|
||
" dependent_vars.append(f\"{var} = {expr}\")\n",
|
||
" else:\n",
|
||
" free_vars.append(str(var))\n",
|
||
"\n",
|
||
" # Si existen variables libres, entonces el sistema\n",
|
||
" # tiene infinitas soluciones\n",
|
||
" if free_vars:\n",
|
||
" deps = \", \".join(dependent_vars)\n",
|
||
"\n",
|
||
" if len(free_vars) == 1:\n",
|
||
" libres = free_vars[0]\n",
|
||
" else:\n",
|
||
" libres = \" y \".join(free_vars)\n",
|
||
"\n",
|
||
" return (\n",
|
||
" f\"Infinitas soluciones: \"\n",
|
||
" f\"{deps}, con {libres} libres\"\n",
|
||
" )\n",
|
||
"\n",
|
||
" return sol\n",
|
||
"\n",
|
||
" except Exception as e:\n",
|
||
" return f\"Error: {e}\"\n",
|
||
"\n",
|
||
"\n",
|
||
"def solve_equations_np(A, b):\n",
|
||
" try:\n",
|
||
" A = np.array(A, dtype=float)\n",
|
||
" b = np.array(b, dtype=float)\n",
|
||
"\n",
|
||
" # Teorema de Rouché-Frobenius:\n",
|
||
" #\n",
|
||
" # rank(A) = rank([A|b]) = n\n",
|
||
" # -> solución única\n",
|
||
" #\n",
|
||
" # rank(A) = rank([A|b]) < n\n",
|
||
" # -> infinitas soluciones\n",
|
||
" #\n",
|
||
" # rank(A) < rank([A|b])\n",
|
||
" # -> sistema inconsistente\n",
|
||
"\n",
|
||
" rank_A = np.linalg.matrix_rank(A)\n",
|
||
" rank_aug = np.linalg.matrix_rank(np.c_[A, b])\n",
|
||
"\n",
|
||
" num_vars = A.shape[1]\n",
|
||
"\n",
|
||
" if rank_A < rank_aug:\n",
|
||
" return \"El sistema no tiene solución\"\n",
|
||
"\n",
|
||
" if rank_A < num_vars:\n",
|
||
" return \"Infinitas soluciones\"\n",
|
||
"\n",
|
||
" # Se utiliza lstsq en lugar de solve porque\n",
|
||
" # permite resolver sistemas rectangulares\n",
|
||
" # (m ecuaciones con n incógnitas),\n",
|
||
" # incluyendo sistemas sobredeterminados.\n",
|
||
" sol, _, _, _ = np.linalg.lstsq(\n",
|
||
" A,\n",
|
||
" b,\n",
|
||
" rcond=None\n",
|
||
" )\n",
|
||
"\n",
|
||
" return {\n",
|
||
" f'x{i+1}': round(float(s), 6)\n",
|
||
" for i, s in enumerate(sol)\n",
|
||
" }\n",
|
||
"\n",
|
||
" except Exception as e:\n",
|
||
" return f\"Error: {e}\"\n",
|
||
"\n",
|
||
"def classify_system(A, b):\n",
|
||
" rank_A = np.linalg.matrix_rank(A)\n",
|
||
" rank_aug = np.linalg.matrix_rank(np.c_[A, b])\n",
|
||
"\n",
|
||
" num_vars = A.shape[1]\n",
|
||
"\n",
|
||
" # Teorema de Rouché-Frobenius\n",
|
||
"\n",
|
||
" if rank_A < rank_aug:\n",
|
||
" return \"Sin solución\"\n",
|
||
"\n",
|
||
" if rank_A < num_vars:\n",
|
||
" return \"Infinitas soluciones\"\n",
|
||
"\n",
|
||
" return \"Solución única\"\n",
|
||
"\n",
|
||
"def print_system_info(A, system_type):\n",
|
||
" rows, cols = A.shape\n",
|
||
"\n",
|
||
" print(f\"Sistema {rows}x{cols}\")\n",
|
||
"\n",
|
||
" if system_type == \"Solución única\":\n",
|
||
" print(\"Solución única:\")\n",
|
||
"\n",
|
||
" elif system_type == \"Infinitas soluciones\":\n",
|
||
" print(\"Infinitas soluciones:\")\n",
|
||
"\n",
|
||
" else:\n",
|
||
" print(\"El sistema no tiene solución\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "2c028080",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Ejercicio 1 — Sistema cuadrado 2×2 (solución única)\n",
|
||
"Se tiene el siguiente sistema de 2 ecuaciones con 2 incógnitas ($x, y$):\n",
|
||
"\n",
|
||
"$$\n",
|
||
"\\begin{cases} \n",
|
||
"2x + 3y = 8 \\\\ \n",
|
||
"x - 4y = -5 \n",
|
||
"\\end{cases}\n",
|
||
"$$\n",
|
||
"\n",
|
||
"### Objetivo\n",
|
||
"Verificar que tanto **NumPy** como **SymPy** resuelven correctamente este caso base (sistema compatible determinado con solución única)."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"id": "f45cf88e",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Sistema 2x2\n",
|
||
"Solución única:\n",
|
||
"Numpy: {'x1': 1.545455, 'x2': 1.636364}\n",
|
||
"Sympy: {x: 17/11, y: 18/11}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"x, y = sp.symbols('x y')\n",
|
||
"equations = (sp.Eq(2*x + 3*y, 8), sp.Eq(x - 4*y, -5))\n",
|
||
"\n",
|
||
"A = np.array([[2, 3], [1, -4]])\n",
|
||
"b = np.array([8, -5])\n",
|
||
"\n",
|
||
"solution_sp = solve_equations_sp(equations, (x, y))\n",
|
||
"solution_np = solve_equations_np(A, b)\n",
|
||
"\n",
|
||
"system_type = classify_system(A,b)\n",
|
||
"print_system_info(A, system_type)\n",
|
||
"print('Numpy:', solution_np)\n",
|
||
"print('Sympy:', solution_sp)\n",
|
||
"\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "07e4c825",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Ejercicio 2 — Sistema cuadrado 3×3\n",
|
||
"Sistema de 3 ecuaciones con 3 incógnitas (x, y, z):\n",
|
||
"\n",
|
||
"$$\n",
|
||
"\\begin{cases} \n",
|
||
"x + y + z = 6 \\\\ \n",
|
||
"2x - y + z = 3 \\\\\n",
|
||
"x + 2y - 3z = -4 \n",
|
||
"\\end{cases}\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"id": "7798069d",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Solving equations using SymPy and numpy: \n",
|
||
"\n",
|
||
"Sistema 3x3\n",
|
||
"Solución única:\n",
|
||
"Numpy: {'x1': 1.0, 'x2': 2.0, 'x3': 3.0}\n",
|
||
"Sympy: {x: 1, y: 2, z: 3}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print('Solving equations using SymPy and numpy: \\n')\n",
|
||
"x, y, z = sp.symbols('x y z')\n",
|
||
"\n",
|
||
"eq1 = sp.Eq(x + y + z, 6)\n",
|
||
"eq2 = sp.Eq(2*x - y + z, 3)\n",
|
||
"eq3 = sp.Eq(x + 2*y - 3*z, -4)\n",
|
||
"\n",
|
||
"equations = (eq1, eq2, eq3)\n",
|
||
"A = np.array([[1, 1, 1], [2, -1, 1], [1, 2, -3]])\n",
|
||
"b = np.array([6, 3, -4])\n",
|
||
"\n",
|
||
"solution_sp = solve_equations_sp(equations, (x, y, z))\n",
|
||
"solution_np = solve_equations_np(A, b)\n",
|
||
"\n",
|
||
"system_type = classify_system(A, b)\n",
|
||
"print_system_info(A, system_type)\n",
|
||
"print('Numpy:', solution_np)\n",
|
||
"print('Sympy:', solution_sp)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6d01413a",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Ejercicio 3 — Sistema 3×3\n",
|
||
"Sistema de 3 ecuaciones con 3 incógnitas donde las ecuaciones son linealmente dependientes:\n",
|
||
"$$\n",
|
||
"\\begin{cases} \n",
|
||
"x + y + z = 4 \\\\ \n",
|
||
"2x + 2y + 2z = 8 \\\\\n",
|
||
"3x + 3y + 3z = 12 \n",
|
||
"\\end{cases}\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"id": "09de85e4",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Solving equations using SymPy and numpy: \n",
|
||
"\n",
|
||
"Sistema 3x3\n",
|
||
"Infinitas soluciones:\n",
|
||
"Numpy: Infinitas soluciones\n",
|
||
"Sympy: Infinitas soluciones: x = -y - z + 4, con y y z libres\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print('Solving equations using SymPy and numpy: \\n')\n",
|
||
"x, y, z = sp.symbols('x y z')\n",
|
||
"\n",
|
||
"eq1 = sp.Eq(x + y + z, 4)\n",
|
||
"eq2 = sp.Eq(2*x + 2*y + 2*z, 8)\n",
|
||
"eq3 = sp.Eq(3*x + 3*y + 3*z, 12)\n",
|
||
"\n",
|
||
"equations = (eq1, eq2, eq3)\n",
|
||
"A = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])\n",
|
||
"b = np.array([4, 8, 12])\n",
|
||
"\n",
|
||
"solution_sp = solve_equations_sp(equations, (x, y, z))\n",
|
||
"solution_np = solve_equations_np(A, b)\n",
|
||
"\n",
|
||
"system_type = classify_system(A, b)\n",
|
||
"print_system_info(A, system_type)\n",
|
||
"print('Numpy:', solution_np)\n",
|
||
"print('Sympy:', solution_sp)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "c018e193",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Ejercicio 4 — Sistema 2×2 sin solución (inconsistente)\n",
|
||
"Sistema de 2 ecuaciones con 2 incógnitas cuyas rectas son paralelas:\n",
|
||
"$$\n",
|
||
"\\begin{cases} \n",
|
||
"3x - 6y = 9 \\\\ \n",
|
||
"x - 2y = -1 \n",
|
||
"\\end{cases}\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 17,
|
||
"id": "9057d167",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Solving equations using SymPy and numpy: \n",
|
||
"\n",
|
||
"Sistema 2x2\n",
|
||
"El sistema no tiene solución\n",
|
||
"Numpy: El sistema no tiene solución\n",
|
||
"Sympy: El sistema no tiene solución\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print('Solving equations using SymPy and numpy: \\n')\n",
|
||
"x, y = sp.symbols('x y')\n",
|
||
"\n",
|
||
"eq1 = sp.Eq(3*x - 6*y, 9)\n",
|
||
"eq2 = sp.Eq(x - 2*y, -1)\n",
|
||
"\n",
|
||
"equations = (eq1, eq2)\n",
|
||
"A = np.array([[3, -6], [1, -2]])\n",
|
||
"b = np.array([9, -1])\n",
|
||
"\n",
|
||
"solution_sp = solve_equations_sp(equations, (x, y))\n",
|
||
"solution_np = solve_equations_np(A, b)\n",
|
||
"\n",
|
||
"system_type = classify_system(A, b)\n",
|
||
"print_system_info(A, system_type)\n",
|
||
"print('Numpy:', solution_np)\n",
|
||
"print('Sympy:', solution_sp)\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "678e269f",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Ejercicio 5 — Sistema rectangular 4×3 (M > N)\n",
|
||
"Sistema sobredeterminado: 4 ecuaciones con 3 incógnitas (x, y, z):\n",
|
||
"$$\n",
|
||
"\\begin{cases} \n",
|
||
"x + y + z = 6 \\\\ \n",
|
||
"2x - y = 1 \\\\\n",
|
||
"3y + 2z = 11 \\\\\n",
|
||
"x + y - 2z = 0\n",
|
||
"\\end{cases}\n",
|
||
"$$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 18,
|
||
"id": "dda9eae8",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Solving equations using SymPy and numpy: \n",
|
||
"\n",
|
||
"Sistema 4x3\n",
|
||
"Solución única:\n",
|
||
"Numpy: {'x1': 1.666667, 'x2': 2.333333, 'x3': 2.0}\n",
|
||
"Sympy: {x: 5/3, y: 7/3, z: 2}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print('Solving equations using SymPy and numpy: \\n')\n",
|
||
"x, y, z = sp.symbols('x y z')\n",
|
||
"\n",
|
||
"eq1 = sp.Eq(x + y + z, 6)\n",
|
||
"eq2 = sp.Eq(2*x - y, 1)\n",
|
||
"eq3 = sp.Eq(3*y + 2*z, 11)\n",
|
||
"eq4 = sp.Eq(x + y - 2*z, 0)\n",
|
||
"\n",
|
||
"equations = (eq1, eq2, eq3, eq4)\n",
|
||
"A = np.array([[1, 1, 1], [2, -1, 0], [0, 3, 2], [1, 1, -2]])\n",
|
||
"b = np.array([6, 1, 11, 0])\n",
|
||
"\n",
|
||
"solution_sp = solve_equations_sp(equations, (x, y, z))\n",
|
||
"solution_np = solve_equations_np(A, b)\n",
|
||
"\n",
|
||
"system_type = classify_system(A, b)\n",
|
||
"print_system_info(A, system_type)\n",
|
||
"print('Numpy:', solution_np)\n",
|
||
"print('Sympy:', solution_sp)\n"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "propedeutico",
|
||
"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.10.20"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|