Skip to content

Instantly share code, notes, and snippets.

@henryscala
Created November 6, 2015 09:43
Show Gist options
  • Save henryscala/2e3d7f1affebf8912a9c to your computer and use it in GitHub Desktop.
Save henryscala/2e3d7f1affebf8912a9c to your computer and use it in GitHub Desktop.
using sympy to solve(approximate) numeric equations's root (it uses bi-sect method)
In [9]: import sympy
In [11]: from sympy import symbols
In [12]: x = symbols('x')
In [13]: x
Out[13]: x
In [14]: type(x)
Out[14]: sympy.core.symbol.Symbol
In [15]: y=x**2 - 2
In [16]: y
Out[16]: x**2 - 2
In [17]: from sympy import nsolve
sympy.plot(y,(x,-2,2))
Out[22]: <sympy.plotting.plot.Plot at 0x77ec198>
In [18]: nsolve(y,x,(0,2))
Out[18]: mpf('1.414213562373095')
In [19]: nsolve(y,x,(-2,2)) #it will report error
nsolve(y,x,(-2,0))
Out[20]: mpf('-1.414213562373095')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment