As a student I am new to python and am struggling with the following program I have been tasked to create. My code so far is like so. The idea is that the function will take a guess x, incorporate an arbitrary function f(x) and then take feps, a tolerance and max_it, the maximum number of iterations. The general idea is that this iterative approach will use the Newton_Raphson method to determin an approximation to the given value x. Example, def f(x): return x**2-2 and then newton(f,1.0,0.2,15) yields approx squared root of two - 1.414.......
def newton(f,x,feps,max_it): it=0 def fprime(x): return (f(x+feps/2.0)-f(x-feps/2.0))/feps while abs(f(x))>feps: x=x-f(x)/fprime(x) it+=1 if it>max_it: return None return x
This seems to work well but I cannot get it to return the python special type - None if the number of iterations are exceeded during the while loop.
Any suggestions would be fantastic as I have now spent an embarrasing number of hours on it.
Edited 6 Years Ago by vegaseat: added code tags