Copy this error-prone script to a new m-file and name it NewtonScript.m
.
This script computes the first few Newton's Method root approximations
for the two roots of the function f(x)= x/(1+x^2/4)-1/2.
% NewtonScript.m % Finds the roots using Newton's method clear; clc; clf; format long; % First, plot the function xx = 0:0.1:10; ff = xx./(1+xx.^2/4)-1/2; plot(xx,ff); % User Newton's Method to approximate the first root x(1) = 1; x(2) = Newton3(x(1)); x(3) = Newton3(x(1)); x(4) = Newton3(x(1)); x(5) = Newton3(x(1)); x(6) = Newton3(x(1)); x % Use Newton's Method to approximate the second root w(1) = 7; w(2) = Newton3(w(1)); w(3) = Newton3(w(2)); w(4) = Newton3(w(3)); w(5) = Newton3(w(3)); w(6) = Newton3(w(5)); w
You will also need to define the Newton3.m
function, as shown, once
you have set a breakpoint and you.
function x_next = Newton3( x_input ) % Newton's formula for successive approximations % to find the roots where f(x)=x/(1+x^2/4)-1/2=0 x = x_input; f = x/(1+x^2/4)-1/2; df = 1/(1+1/4*x^2)-1/2 * x^2 /(1+1/4*x^2)^2; x_next = x - f/df;