%main function call for newton's method function newton(x0) %set tol equal to x for convergence purposes tol=1e5; %keep track of iterations count=1; x = x0; %do until convergence or too many loops while tol>1e-10 && count<500 %newton's method %xnew=x-f'(x)/f''(x); xnew=x(count)-fprime(x(count))/fdblprime(x(count)); x = [x xnew]; %how close is my iteration tol=abs(x(count)-x(count+1)); %keep track of iteration count=count+1; end disp(['# of iterations: ',num2str(count)]); disp(['min/max is: ',num2str(x(end))]); x1 = linspace(-2,6,100); y1 = zeros(100,1); for i=1:100 y1(i)= f(x1(i)); end y = zeros(count,1); for j = 1:count y(j) = f(x(j)); end figure(1); plot(x1,y1,'-',x,y,'--.') text(x(1)+.15,y(1)+5,'x0','FontSize',14) text(x(end)+.15,y(end)+5,'x*','FontSize',14) function out1 = f(x) out1 = 10*x^3-50*x^2+2*x+1; function out2=fprime(x) h=eps^(1/3); out2=(f(x+h)-f(x))/h; function out3=fdblprime(x) h = eps^(1/3); out3 = (fprime(x+h)-fprime(x))/h;