I'm writing a program in python that will solve for zeros using newtons method. I finished writing the rough version of it, then realized a couple different things and was wondering if I need to impement/change this. (knowledge of the subject may help)
def main():
dir(sympy)
print ("NEWTONS METHOD")
print ("Write your expression in terms of 'x' ")
e = sympy.sympify(raw_input("input expression here: "))
f = sympy.Symbol('x')
func1 = e
func1d = sympy.diff(e,f)
print ("the dirivative of your function = "), func1d
x = input("number to substitude for x: ")
func1sub = func1.subs({'x':x})
func1dsub = func1d.subs({'x':x})
n = x - float(func1sub/func1dsub)
while n != x:
func1sub = func1.subs({'x':x})
func1dsub = func1d.subs({'x':x})
n = x - float(func1sub/func1dsub)
print n
main()
1) Well first I was wondering since the values of n
and x
may not always be exactly the same I would have to round using the round function.
2) After looking at this I feel that my while loop is not solving for what it should be solving, it should be solving for what is the x
that you can plug it into x
in the function and the output will be x
. Could I do this by appending the values to a array then seeing where their are multiples instances of one number?