Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
find x with f(x) = 0 (limited area of solution)
#!/usr/bin/python2 """ Exp: find x with f(x) = 0 f(x) = 2*x+3 solve for x 100 Trying... ---[ x = -2.000000000000000000 <=> f(x) = -1 ---[ x = -1.499999999999998668 <=> f(x) = 2.6645352591003757e-15 ---[ x = -1.499999999999998668 <=> f(x) = 2.6645352591003757e-15 ---[ x = -1.499999999999999778 <=> f(x) = 4.440892098500626e-16 ---[ x = -1.499999999999999778 <=> f(x) = 4.440892098500626e-16 ---[ x = -1.499999999999999112 <=> f(x) = 1.7763568394002505e-15 ---[ x = -1.500000000000000000 <=> f(x) = 0.0 x = -1.5 Done! """ def FloatRange(start, stop, step): while start < stop: yield start start += step def FindxSmallest(expression, Start, Stop, Step): array = [abs(eval(expression)) for x in FloatRange(Start, Stop, Step)] return list(FloatRange(Start, Stop, Step))[array.index(min(array))] def main(): expression = input("find x with f(x) = 0\n f(x) = ") # limited area of solution zone = int(input('solve for x ')) NumStartScan = -abs(zone) NumStopScan = abs(zone) Step = 1 # default print ("Trying...\n") while True: x = FindxSmallest(expression, NumStartScan, NumStopScan, Step) print ("---[ x = %-18.18f" % x, "<=> f(x) =", eval(expression)) if round(eval(expression), 18) == 0: print ("\nx =", x) break else: NumStartScan = x - Step NumStopScan = x + Step Step = Step / 10 if __name__ == "__main__": main() print ("Done!")
run
|
edit
|
history
|
help
0
GINGERMANZ
Lesson 9 hw
molecular mass
hw1
test2.py
Sort word in alphabetical order
test4.py
https://rextester.com/REAL13110
filtering python3
Gift_Card Interview SQL Analysis Conducted by Miranda Zhao