diff options
author | hardythe1 | 2013-10-22 17:31:15 +0530 |
---|---|---|
committer | hardythe1 | 2013-10-22 17:31:15 +0530 |
commit | c7b48513fb2cb0cdd0e4ec0050b8dbcbddc698f5 (patch) | |
tree | 52b3a71ce8b42ca74a9bd92fff4560fcb3cf7895 /tbc/static/uploads/Hardik/C/newton.py | |
parent | 266347a58de32b21c3fa1ae3b9a4b466d4584372 (diff) | |
download | Python-TBC-Interface-c7b48513fb2cb0cdd0e4ec0050b8dbcbddc698f5.tar.gz Python-TBC-Interface-c7b48513fb2cb0cdd0e4ec0050b8dbcbddc698f5.tar.bz2 Python-TBC-Interface-c7b48513fb2cb0cdd0e4ec0050b8dbcbddc698f5.zip |
Adding the project initial commit
Diffstat (limited to 'tbc/static/uploads/Hardik/C/newton.py')
-rw-r--r-- | tbc/static/uploads/Hardik/C/newton.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tbc/static/uploads/Hardik/C/newton.py b/tbc/static/uploads/Hardik/C/newton.py new file mode 100644 index 0000000..258dc58 --- /dev/null +++ b/tbc/static/uploads/Hardik/C/newton.py @@ -0,0 +1,35 @@ +from sympy import * +import sys + + +print "Enter a function: ", +func = raw_input() + +try: + sym_x = Symbol('x') + fx = S(func) + fdashx = diff(fx, Symbol('x')) +except: + print "Function could not be differenciated..." + sys.exit() + +print "Enter initial guess: ", +curr_root = float(raw_input()) +print "Enter precision value: ", +precision = float(raw_input()) +new_root = (curr_root - fx.subs({sym_x : curr_root})/fdashx.subs({sym_x : curr_root})) + + +iterations = 1 +while(abs(new_root-curr_root)>precision): + curr_root = new_root + new_root = (curr_root - fx.subs({sym_x : curr_root})/fdashx.subs({sym_x : curr_root})) + iterations += 1 + if iterations >= 50: + print "Root was not derived uptil 50 iterations, current root: ", curr_root + break + +print "Function: ", fx +print "Given tolerance: ", precision +print "Number of iterations: ", iterations +print "Root to the funtion upto given precision: ", curr_root |