Last active
February 7, 2016 13:40
-
-
Save SYZYGY-DEV333/237cd89b014243d026d5 to your computer and use it in GitHub Desktop.
Polynomial root finder in Python. Quick and dirty, but it works. Have fun! :-)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Polynomial Root Finder | |
# SYZYGY-DEV333 | |
# Apache 2.0 | |
print "Python Polynomial Root Finder" | |
print "Use '**' to mean '^'" | |
while 1 == 1: | |
equ = raw_input("0=") # asks for your equation | |
f = lambda x: eval(equ,{"__builtins__":None},{"x":x}) # for saftey, won't accept anything other than an equation | |
step = 0.001 # smaller steps are more accurate, but slower | |
start = -10 # these parameters should be fine at -10 and 10 | |
stop = 10 | |
sign = f(start) > 0 | |
z = start | |
while z <= stop: | |
value = f(z) | |
if value == 0: | |
print "Root found at", z | |
elif (value > 0) != sign: | |
print "Root found near", z | |
sign = value > 0 | |
z += step |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Polynomial Root Finder
Simple polynomial root finder made in Python. It's quick and dirty and horribly inefficient, but it works. Just type in the equation when prompted. You have to change it a little though, so
-7x^2+8x+10
would become-7 * x ** 2 + 8 * x + 10
. That's all, have fun! :-)