clear clf format compact format long x1 = .01; np = 100; t1 = linspace(-10*x1, 10*x1, np); figure(1) plot(t1, f(t1), '-') hold on plot(0, 0, 'or') grid on xlabel('t') ylabel('f(t)') title("Function demonstrating failure of Newton's method") x = newton(@f, @dfdx, x1, 20); number_of_iterations = length(x) t2 = logspace(-4, 4, np); figure(2) semilogx(t2, f(t2), '-') hold on semilogx(x, f(x), 'xr') grid on title("Divergence of Newton's iterations") function y = f(x) % F demonstrate the case of divergence for Newton's method alpha = 0.009; y = x./(alpha^2 + x.^2); end function y = dfdx(x) % DFDX demonstrate the case of divergence for Newton's method alpha = 0.009; y = (alpha^2 - x^2)/(alpha^2 + x^2)^2; end