# newton

## 840 days ago by voloch

#Code from Jamie Mulholland http://www.sfu.ca/~jtmulhol/ x = var('x'); # declare the variable for function f(x)=x^5+x^4-5; # change this to whatever function you like df=diff(f,x); # Sage will compute the derivative of f NewtonIt(x)=x-(f/df)(x); # Newtons Iterative Formula which we are calling "NewtonIt" x1=1; # initial guess print N(x1), N(f(x1)); # prints x1 and f(x1) in output window below, N() produces a numerical result x2=N(NewtonIt(x1)); # one iteration of Newtons Method - output below print x2, N(f(x2)); x3=N(NewtonIt(x2)); # a second iteration of Newtons Method print x3, N(f(x3)); x4=N(NewtonIt(x3)); # a third iteration of Newtons Method print x4, N(f(x4)); x5=N(NewtonIt(x4)); # a third iteration of Newtons Method print x5, N(f(x5));
 1.00000000000000 -3.00000000000000 1.33333333333333 2.37448559670782 1.23942057291667 0.284579871276739 1.22476268905564 0.00600735059732571 1.22443970357315 2.85479097650310e-6 1.00000000000000 -3.00000000000000 1.33333333333333 2.37448559670782 1.23942057291667 0.284579871276739 1.22476268905564 0.00600735059732571 1.22443970357315 2.85479097650310e-6
x = var('x'); f(x)=sin(x)-x+2*3.14159/3; df=diff(f,x); NewtonIt(x)=x-(f/df)(x); x1=3; for n in range(5): #for loop for a flexible number of iterations print N(x1), N(f(x1)); #Print current value of approximation and f(approximation) x1=N(NewtonIt(x1)); #Apply Newton iteration to update approximation plot(f(x),(x,0,5))
 3.00000000000000 -0.764486658606800 2.61583440142976 -0.0195720738229194 2.60533967476400 -0.0000278041209716973 2.60532472333070 -5.71067637622491e-11 2.60532472329999 -1.11022302462516e-16 3.00000000000000 -0.764486658606800 2.61583440142976 -0.0195720738229194 2.60533967476400 -0.0000278041209716973 2.60532472333070 -5.71067637622491e-11 2.60532472329999 -1.11022302462516e-16