Skip to content

Instantly share code, notes, and snippets.

@aleksejalex
Last active March 13, 2024 16:10
Show Gist options
  • Save aleksejalex/0e27790a7c2c89115725c3df39c2c367 to your computer and use it in GitHub Desktop.
Save aleksejalex/0e27790a7c2c89115725c3df39c2c367 to your computer and use it in GitHub Desktop.
solving_fxeq0.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOnBXnK/dhVGP/v+V9xbABT",
"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/aleksejalex/0e27790a7c2c89115725c3df39c2c367/solving_fxeq0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# 1) numericke reseni pomoci existujiciho solveru z knihovny SciPy\n",
"funkce [`fsolve`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html)"
],
"metadata": {
"id": "lylC2q06hcSH"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "h7ZjeRzFb1RR",
"outputId": "f5786e6e-5dc6-4632-c5a5-747191e8e17e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The root of the equation is: [1.].\n"
]
}
],
"source": [
"import numpy as np\n",
"from scipy.optimize import fsolve\n",
"\n",
"def f(x):\n",
" \"\"\"\n",
" toto bude vase funkce\n",
" mocnina se napise **, exp() ... np.exp(), odmocnina ... np.sqrt(), abs.hodnota ... np.abs()\n",
" \"\"\"\n",
" return (x-2)**2 - 1 # tohle analyticky ma dva koreny, x=1 a x=3\n",
"\n",
"\n",
"# Initial guess for the root\n",
"x0 = 0\n",
"# pozor, lze si vsimnout:\n",
"# 1) zalezi na init. guess: zaporny guess vrati mensi koren (1), kladny guess vrati vetsi koren (3)\n",
"# 2) pokud init. guess je hooodne daleko od pravdy, vysledek je velmi nepresny\n",
"\n",
"# Solve the equation f(x) = 0\n",
"root = fsolve(f, x0)\n",
"\n",
"print(f\"The root of the equation is: {root}.\")"
]
},
{
"cell_type": "code",
"source": [
"# kontrola jak jsme daleko (tj. jestli |f(x)-0| je minimalni):\n",
"np.isclose(f(root), [0.0])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JXyM8mLxb2Ci",
"outputId": "eccd5d5f-7ed6-4199-f594-ef9a8a40fde5"
},
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ True])"
]
},
"metadata": {},
"execution_count": 2
}
]
},
{
"cell_type": "code",
"source": [
"# koren rce\n",
"root"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CkN-XQtZeeom",
"outputId": "bce409bb-dbd5-4747-8be6-b7d1d77f8e3c"
},
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1.])"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"source": [
"# hodnota f v bode nalezeho jako koren (ma byt malinke cislo):\n",
"f(root)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UFdMw3XYek81",
"outputId": "74898782-b87b-4a1c-8706-7dc54014cf5f"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([4.4408921e-16])"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "AJM2YNCHeuck"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# 2) pomoci symbolickych vypoctu (knihovna SymPy)\n",
"solver [sympy.solve](https://docs.sympy.org/latest/guides/solving/solve-equation-algebraically.html)\n",
"\n",
"Vyhoda je, ze vraci vsechny koreny (protoze pocita analyticky). Nevyhoda je, ze u slozitejsich veci selze."
],
"metadata": {
"id": "RGOkMd7VhOB4"
}
},
{
"cell_type": "code",
"source": [
"import sympy as sp\n",
"\n",
"# Define the symbol\n",
"x = sp.Symbol('x')\n",
"\n",
"# Define the function g(x)\n",
"g = (x-2)**2 - 1 # stejna fce jako f, ktera byla vyse, ale jiny pythonovsky objekt, proto ji radsi nazvu g\n",
"# pozor, fce jako exp, log, sin musi byt z balicku SymPy, tj. napr. g = (sp.sin(x)-2)**2 - 1\n",
"\n",
"# Find the symbolic solutions\n",
"solutions = sp.solve(g, x)\n",
"\n",
"print(\"Symbolic solutions of the equation g(x) = 0:\")\n",
"for solution in solutions:\n",
" print(solution)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ERbVO4xqiAxH",
"outputId": "58e9ff10-ade7-40fe-eb46-592e39dd04d4"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Symbolic solutions of the equation g(x) = 0:\n",
"1\n",
"3\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "tXwWR8A6iB1X"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "oYaXUxNIiB6z"
},
"execution_count": 5,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment