MAT 141, Day 26

2214 days ago by kcrisman

You can use this little applet to use Newton's method at home!  Just

  • Log in to Sage
  • Go back to this page, possibly reloading it
  • Click in the upper left on "Edit a copy"
  • Click in the cell with the programming right below this (it may say "hide", in which case you can click on that)
  • Click on the little "evaluate" button on the lower left of it that should appear
  • And you're ready to go!
%hide def Newton_Graph(a_function, guess, iterations = 5): ''' Returns a graphics object of a plot of a_function and Newton-Raphson method tangent lines. INPUT: a_function: a (callable) function of one variable start: the starting value of the iteration iterations: (optional) the number of iterations to draw xmin: (optional) the lower end of the plotted interval xmax: (optional) the upper end of the plotted interval EXAMPLES: sage: f = lambda x: x^3-3*x^2+2*x sage: show(Newton_Graph(f,.5)) ''' iter_list = [[guess,0]] f(x)=a_function(x) deriv(x)=f.derivative() approx(x)=x-f(x)/deriv(x) input = guess for i in range(iterations): iter_list.append([input,f(input)]) input=approx(input).n() iter_list.append([input,0]) approx_tangents = line(iter_list,rgbcolor=(1,0,0)) xmin=min([point[0] for point in iter_list])-1 xmax=max([point[0] for point in iter_list])+1 ymin=min([point[1] for point in iter_list])-.5 ymax=max([point[1] for point in iter_list])+.5 basic_plot = plot(a_function, xmin, xmax, color='blue',ymin=ymin,ymax=ymax) P=basic_plot + approx_tangents return P @interact def _(g=4*x^3-7*x^2,guess=1,iter=5): f(x)=g P = Newton_Graph(f,guess,iterations=iter) show(P) html("root approximation is $"+str(P[1].xdata[-1])+"$") 
       

Click to the left again to hide and once more to show the dynamic interactive window

What can go wrong with Newton's method?

%hide def Newton_Graph(a_function, guess, iterations = 5): ''' Returns a graphics object of a plot of a_function and Newton-Raphson method tangent lines. INPUT: a_function: a (callable) function of one variable start: the starting value of the iteration iterations: (optional) the number of iterations to draw xmin: (optional) the lower end of the plotted interval xmax: (optional) the upper end of the plotted interval EXAMPLES: sage: f = lambda x: x^3-3*x^2+2*x sage: show(Newton_Graph(f,.5)) ''' iter_list = [[guess,0]] f(x)=a_function(x) deriv(x)=f.derivative() approx(x)=x-f(x)/deriv(x) input = guess for i in range(iterations): iter_list.append([input,f(input)]) input=approx(input).n() iter_list.append([input,0]) approx_tangents = line(iter_list,rgbcolor=(1,0,0)) xmin=min([point[0] for point in iter_list])-1 xmax=max([point[0] for point in iter_list])+1 ymin=min([point[1] for point in iter_list])-.5 ymax=max([point[1] for point in iter_list])+.5 basic_plot = plot(a_function, xmin, xmax, color='blue',ymin=ymin,ymax=ymax) P=basic_plot + approx_tangents return P def cubic_approx(guess, intercept=0, iterations = 5): ''' Returns the Newton-Raphson zero approximation graph for x^3-3*x^2+2*x+intercept, from the given starting guess. INPUT: start: the starting value of the iteration mask: (optional) the number of initial iterates to ignore iterations: (optional) the number of iterations to draw, following the masked iterations EXAMPLES: sage: P=cubic_approx(.5,3); show(P) ''' f(x)=x^3-3*x^2+2*x+intercept return Newton_Graph(f,guess,iterations=iterations) 
       
@interact def _(c=(0,[-2,-1,0,1,2]),guess=.5,iter=(5,[3..20])): show(cubic_approx(guess,intercept=c,iterations=iter)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

plot(3*e^(-10e-13*x)+10e-13*x-3,(x,-10e11,10e12))