Let $f(x) = x^3 - 1 $
.
Use the bisection method on the interval $[−1,10]$
to calculate the root of $f(x)=0$
correct to two decimal places.
How many bisections were necessary?
Use Newton–Raphson with $x_0=10$
to calculate the root of $f(x)=0$
correct to two decimal places.
How many iterations were necessary?
14 bisections are required for interval bisection:
clear
%
%% The function, interval and tolerance
theFunction=@(x)x^3-1;
theInterval=[-1,10];
theTolerance=0.001;
%
%% Interval bisection method
NumberOfBisections=0; % Initialising
a=theInterval(1);
b=theInterval(2);
%
while (b-a > theTolerance)
c=(a+b)*0.5;
fa=theFunction(a);
fc=theFunction(c);
if (fa*fc > 0)
a=c;
elseif (fa*fc < 0)
b=c;
else
b=a;
end
%
% Increment number of bisections
NumberOfBisections=NumberOfBisections+1;
end
%
root = c
NumberOfBisections
9 iterations are required for Newton–Raphson. Example code to solve this is given below:
clear
%
%% The function, starting value and tolerance
theFunction=@(x)x^3-1;
theDerivative=@(x)3*x*x;
theInitialGuess=10;
theTolerance=0.001;
%
%% Newton-Raphson method
x=theInitialGuess; % Initialising
diff=1; % Initialising
NumberOfIterations=0; % Initialising
%
while (diff > theTolerance)
y=x - theFunction(x)/theDerivative(x);
diff=abs(y-x); % calculate |x_(n+1) - x_n|
x=y; % Update solution approximation
%
% Increment number of iterations
NumberOfIterations=NumberOfIterations+1;
end
%
root = x
NumberOfIterations
$f(x)=x^5+ \frac{x}{1000000}$
.Modify the code from the previous answer. All you need to change is the function definitions.
14 bisections are required for the bisection method and 30 iterations are required for Newton–Raphson in this case.
Example code to solve this using the bisection method is given below:
clear
%
%% The function, interval and tolerance
theFunction=@(x)x^5+x*1e-6;
theInterval=[-1,10];
theTolerance=0.001;
%
%% Interval bisection method
NumberOfBisections=0; % Initialising
a=theInterval(1);
b=theInterval(2);
%
while (b-a > theTolerance)
c=(a+b)*0.5;
fa=theFunction(a);
fc=theFunction(c);
if (fa*fc > 0)
a=c;
elseif (fa*fc < 0)
b=c;
else
b=a;
end
%
% Increment number of bisections
NumberOfBisections=NumberOfBisections+1;
end
%
root = c
NumberOfBisections
Example code to solve this using Newton–Raphson is given below:
clear
%
%% The function, starting value and tolerance
theFunction=@(x)x^5+x*1e-6;
theDerivative=@(x)5*x^4+1e-6;
theInitialGuess=10;
theTolerance=0.001;
%
%% Newton-Raphson method
x=theInitialGuess; % Initialising
diff=1; % Initialising
NumberOfIterations=0; % Initialising
%
while (diff > theTolerance)
y=x - theFunction(x)/theDerivative(x);
diff=abs(y-x); % calculate |x_(n+1) - x_n|
x=y; % Update solution approximation
%
% Increment number of iterations
NumberOfIterations=NumberOfIterations+1;
end
%
root = x
NumberOfIterations
The convergence speed is slower for Newton–Raphson because the derivative of this function is very small in the region of the root.