{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1492f1f5", "metadata": {}, "outputs": [], "source": [ "import sympy as sym " ] }, { "cell_type": "code", "execution_count": 2, "id": "f4d5f61f", "metadata": {}, "outputs": [], "source": [ "x = sym.Symbol(\"x\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "010c42af", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle f{\\left(x \\right)}$" ], "text/plain": [ "f(x)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = sym.Function('f')(x)\n", "f" ] }, { "cell_type": "code", "execution_count": 10, "id": "f47319a5", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d x} f{\\left(x \\right)}$" ], "text/plain": [ "Derivative(f(x), x)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.diff()" ] }, { "cell_type": "code", "execution_count": 32, "id": "628ae0b5", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle x^{2} \\frac{d^{2}}{d x^{2}} f{\\left(x \\right)} + \\frac{d}{d x} f{\\left(x \\right)} = 1$" ], "text/plain": [ "Eq(x**2*Derivative(f(x), (x, 2)) + Derivative(f(x), x), 1)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff_eq = sym.Eq(x**2*f.diff(x,x)+f.diff() , 1)\n", "diff_eq" ] }, { "cell_type": "code", "execution_count": 33, "id": "39b034e0", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle 1$" ], "text/plain": [ "1" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff_eq.rhs" ] }, { "cell_type": "code", "execution_count": 34, "id": "62fe83b7", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle x^{2} \\frac{d^{2}}{d x^{2}} f{\\left(x \\right)} + \\frac{d}{d x} f{\\left(x \\right)}$" ], "text/plain": [ "x**2*Derivative(f(x), (x, 2)) + Derivative(f(x), x)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff_eq.lhs" ] }, { "cell_type": "code", "execution_count": 35, "id": "74c4593c", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle f{\\left(x \\right)} = C_{1} + C_{2} \\left(x e^{\\frac{1}{x}} - \\operatorname{Ei}{\\left(\\frac{1}{x} \\right)}\\right) + x$" ], "text/plain": [ "Eq(f(x), C1 + C2*(x*exp(1/x) - Ei(1/x)) + x)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = sym.dsolve(diff_eq, f)\n", "sol" ] }, { "cell_type": "code", "execution_count": 19, "id": "9a0702fe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sympy.core.relational.Equality" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sol)" ] }, { "cell_type": "code", "execution_count": 17, "id": "88d17b44", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle C_{1} + C_{2} \\log{\\left(x \\right)} + \\frac{x^{4}}{16}$" ], "text/plain": [ "C1 + C2*log(x) + x**4/16" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp = sol.rhs\n", "exp" ] }, { "cell_type": "code", "execution_count": 23, "id": "313d30f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[C1, C2]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c2,_, c1 = tuple(exp.free_symbols)\n", "[c1, c2]" ] }, { "cell_type": "code", "execution_count": null, "id": "43a91997", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }