{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# R\u00e9solution d'\u00e9quations" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from __future__ import division, print_function # Python 3\n", "from sympy import init_printing\n", "init_printing(use_latex='mathjax',use_unicode=False) # Affichage des r\u00e9sultats" ], "outputs": [], "metadata": {} }, { "source": [ "## D\u00e9finir une \u00e9quation\n", "\n", "La fonction Eq permet de d\u00e9finir une \u00e9quation:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import Eq\n", "from sympy.abc import x,y\n", "Eq(x, 3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x == 3" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "Eq(x + y, 3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "x + y == 3" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## R\u00e9soudre une \u00e9quation\n", "\n", "La fonction solve permet de r\u00e9soudre une \u00e9quation:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import solve\n", "solve(Eq(x, 3), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[3]" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve(Eq(x + y, 3), x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[-y + 3]" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Dans le premier cas, indiquer la variable x n'est pas n\u00e9cessaire:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve(Eq(x, 3))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[3]" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela fonctionne aussi s'il y a plus d'une solution:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve(Eq(x**2, 3))" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[-sqrt(3), sqrt(3)]" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## R\u00e9soudre un syst\u00e8me d'\u00e9quations\n", "\n", "La fonction solve permet aussi de r\u00e9soudre un syst\u00e8me d'\u00e9quations. Par exemple pour trouver deux variables x et y telle que leur somme vaut 47 et leur diff\u00e9rence vaut 33:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "eq1 = Eq(x + y, 47)\n", "eq2 = Eq(x - y, 33)\n", "[eq1, eq2]" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[x + y == 47, x - y == 33]" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve([eq1, eq2])" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "{x: 40, y: 7}" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "**Attention**: Pour r\u00e9soudre un syst\u00e8me d'\u00e9quations, il faut absoluement mettre les \u00e9quations dans une liste [eq1, eq2] entre crochets []. Autrement, on obtient une erreur:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "In [279]: solve(eq1, eq2)\n", "Traceback (most recent call last):\n", "...\n", "TypeError: cannot determine truth value of\n", "x - y == 33" ], "outputs": [], "metadata": {} }, { "source": [ "Cela fonctionne aussi pour des syst\u00e8mes d'\u00e9quations non lin\u00e9aires. Par exemple pour trouver deux nombres dont la somme est 47 et dont le produit est 510:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "eq1 = Eq(x + y, 47)\n", "eq2 = Eq(x * y, 510)\n", "[eq1, eq2]" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[x + y == 47, x*y == 510]" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve([eq1, eq2])" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[{x: 17, y: 30}, {x: 30, y: 17}]" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Syntaxe abr\u00e9g\u00e9e\n", "\n", "Souvent on d\u00e9sire r\u00e9soudre des \u00e9quations dont l'un des termes est z\u00e9ro: solve(Eq(expression,0)). Pour ce cas, il est \u00e9quivalent d'\u00e9crire solve(expression) et la fonction solve trouvera les valeurs des variables avec lesquelles expression est \u00e9valu\u00e9e \u00e0 z\u00e9ro. Les exemples ci-haut s'\u00e9crivent de la fa\u00e7on suivante avec cette syntaxe abr\u00e9g\u00e9e:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve(x - 3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[3]" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve(x**2 + x - 6)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[-3, 2]" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve([x + y - 47, x - y - 33])" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "{x: 40, y: 7}" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve([x + y - 47, x * y - 510])" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "[{x: 17, y: 30}, {x: 30, y: 17}]" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Un exemple sur un polyn\u00f4me de degr\u00e9 3:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve(x**3 + 2*x**2 - 1, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " ___ ___ \n", " 1 \\/ 5 \\/ 5 1 \n", "[-1, - - + -----, - ----- - -]\n", " 2 2 2 2 " ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La syntaxe abr\u00e9g\u00e9e peut aussi \u00eatre utilis\u00e9e pour r\u00e9soudre un syst\u00e8me d'\u00e9quations. Dans l'exemple qui suit, on calcule les points d'intersection d'une ellipse et d'une droite:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "solve( [x**2 + 4*y**2 -2, -10*x + 2*y -15], [x, y])" ], "outputs": [], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ " ____ ____ ____ ____\n", " 150 \\/ 23 *I 15 5*\\/ 23 *I 150 \\/ 23 *I 15 5*\\/ 23 *I\n", "[(- --- - --------, --- - ----------), (- --- + --------, --- + ----------)]\n", " 101 101 202 101 101 101 202 101" ], "outputs": [], "metadata": {} }, { "source": [ "## Trouver les racines d'une fonction\n", "\n", "La fonction roots permet de calculer les racines d'une fonction polynomiale univari\u00e9e :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy import roots\n", "roots(x - 7)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "{7: 1}" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "roots(x**6)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "{0: 6}" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Le r\u00e9sultat est un dictionnaire ({}) qui associe \u00e0 chaque racine sa multiplicit\u00e9. La fonction roots trouve aussi les racines complexes:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "roots(x**5 - 7*x**4 + 2*x**3 - 14*x**2 + x - 7, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "{7: 1, -I: 2, I: 2}" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Les coefficients des polyn\u00f4mes peuvent \u00eatre des variables symboliques:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from sympy.abc import a,b,c\n", "roots(a*x + b, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "{-b/a: 1}" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Mais \u00e0 ce moment-l\u00e0, il faut absoluement sp\u00e9cifier par rapport \u00e0 quelle variable on cherche les racines. Autrement, on obtient une erreur:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "roots(a*x + b)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Traceback (most recent call last)\n", "...\n", "PolynomialError: multivariate polynomials are not supported" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La fonction roots trouve les formules qui expriment les racines d'un polyn\u00f4me quadratique:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "roots(a*x**2 + b*x + c, x)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ " _____________ _____________\n", " / 2 / 2\n", " b \\/ -4*a*c + b b \\/ -4*a*c + b\n", "{- --- - ----------------: 1, - --- + ----------------: 1}\n", " 2*a 2*a 2*a 2*a" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "On trouvera d'autres exemples (r\u00e9solution d'\u00e9quations diff\u00e9rentielles) et des explications plus d\u00e9taill\u00e9es dans la section $Solver$ du tutoriel de SymPy: " ], "cell_type": "markdown", "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "python2", "name": "python2" } } }