Math and Science with Python

Solve Equations in Python with SymPy solve

Find exact solutions of equations and systems of equations with sympy.solve, from quadratics to complex roots.

  • Intermédiaire
  • 2 min de lecture
  • 1 exemple exécutable
Ce que vous allez apprendre
  • sp.solve with an expression or Eq
  • Exact roots, complex roots and decimals
  • Solving a system of equations
What sp.solve returns
x**2 - 5x + 6
  • [2, 3]
  • two real roots
x**2 + 1
  • [-I, I]
  • complex roots
x + y = 10, x - y = 2
  • {x: 6, y: 4}
  • a system

Let SymPy do the algebra

Découvrez Solve Equations in Python with SymPy avec un exemple pratique modifiable et exécutable en ligne.

Points clés

  1. sp.solve(x**2 - 5*x + 6, x) returns [2, 3].
  2. sp.Eq(2*x + 3, 11) writes an equation whose right side is not 0.
  3. Roots stay exact, like 1 - sqrt(2); float() turns them into decimals.
  4. A list of equations and a list of unknowns gives a dictionary such as {x: 6, y: 4}.

Quand l’utiliser ?

  • Solve equations, integrals and differential equations in a few lines.
  • Fit models to measured data and analyse signals in science and engineering classes.
  • Check homework answers numerically and see where a hand calculation went wrong.

Code d’exemple

main.py
import sympy as sp

x, y = sp.symbols("x y")

# A quadratic: x**2 - 5x + 6 = 0 (write it as expression = 0)
print("Roots of x**2 - 5x + 6:", sp.solve(x**2 - 5*x + 6, x))

# Use Eq when the right side is not 0: 2x + 3 = 11
print("2x + 3 = 11 ->", sp.solve(sp.Eq(2*x + 3, 11), x))

# No real roots: SymPy returns complex ones
print("x**2 + 1 = 0 ->", sp.solve(x**2 + 1, x))

# Keep the square roots exact, then get decimals with evalf
roots = sp.solve(x**2 - 2*x - 1, x)
print("x**2 - 2x - 1 = 0 ->", roots, "=", [round(float(r), 4) for r in roots])

# A system of two equations: x + y = 10, x - y = 2
solution = sp.solve([sp.Eq(x + y, 10), sp.Eq(x - y, 2)], [x, y])
print("System:", solution)
Obtenir un bouton Run pour cet exemple

Résultat de l’exécution

Roots of x**2 - 5x + 6: [2, 3]
2x + 3 = 11 -> [4]
x**2 + 1 = 0 -> [-I, I]
x**2 - 2x - 1 = 0 -> [1 - sqrt(2), 1 + sqrt(2)] = [-0.4142, 2.4142]
System: {x: 6, y: 4}

Explication du code

Cet exemple montre Solve Equations in Python with SymPy en pratique. Modifiez les valeurs d’entrée et relancez le code pour voir ce qui change.

Modifiez les valeurs et exécutez le programme avec le compilateur Python en ligne CodeUtility, sans installation locale.

Exercices d’approfondissement

Essayez de modifier le code en suivant les pistes ci-dessous. Changer le code et observer le résultat vous fera bien mieux comprendre la leçon que la simple lecture.

  1. Change the function or the data and predict the result before running.
  2. Compare the numerical answer with the exact one from SymPy or a hand calculation.
  3. Plot the result with Matplotlib to see what the numbers mean.
Exécuter dans le compilateur Python →

Leçon terminée ?

Marquez-la comme apprise pour suivre votre progression.

Outils pour cette leçon