# Bisection Secant Method

In this project we use MATLAB to analyze some of the numerical techniques. Consider the function

f(x) = x2 − 3x + 1

• Implement the bisection method to find a zero of the function over [0,1]
• Implement the Newton’s method to find a zero of the function over [0,1]
• Implement the secant method to find a zero of the function over [0,1].

Include the edited MATLAB scripts you used.

Solution

clc,clear,close all;

f = @(x) x^2 – 3*x + 1; % create anonymous function f(x)

%% Part (a) Bisection method

a = 0; b = 1; % interval [a, b] where solution will be searched

tol = 1e-9; % tolerance for the method to terminate

fa = feval(f,a);

fb = feval(f,b);

while (abs(a-b)) >tol + eps*max(abs(a),abs(b))

m = (a+b)/2;

fm = feval(f,m);

if (fa*fm) <= 0

% case where there is a root in [a, m]

b = m;

fb = fm;

else

% case where there is a root in [m, b]

a = m;

fa = fm;

end

end

r1 = (a + b)/2;

%% Part (b) Newton method

fd = @(x) 2*x – 3; % anonymous function of f'(x)

x0 = input(‘Enter initial estimate of root in [0,1]: ‘);

N = input(‘Enter number of maximum iterations: ‘);

xn = x0;

fn = feval(f,xn);

dfn = feval(fd,xn);

for i = 1:N

xn1 = xn – (fn/dfn);

dx = abs(xn1-xn);

xn = xn1;

fn = feval(f,xn);

dfn = feval(fd,xn);

if (dx <tol+eps)

r2 = xn;

break;

end

end

%% Part (c) Secant method

x0 = a; x1 = b;

xk = x1 – (f(x1)*(x1-x0))/(f(x1)-f(x0));

while abs(xk-x0)>tol % terminate loop when criterion is true

xnew = xk – (f(xk)*(xk-x0))/(f(xk)-f(x0));

x0 = xk;

xk = xnew;

end

r3 = xk;

%% Part (d) Summary of all 3 methods

fprintf(‘————————————————————————-\n’)

fprintf(‘%15s %22s %23s\n’,’Bisection’,’Newton’,’Secant’);

fprintf(‘x:    %.14f          %.14f        %.14f\n’,r1,r2,r3);

fprintf(‘f(x): %.14f          %.14f        %.14f\n’,f(r1),f(r2),f(r3));

fprintf(‘————————————————————————-\n’)

The Matlab script Assignment2.m (Appendix) finds a root of the function f(x) = x2 – 3x + 1 in the interval [0, 1] numerically using the bisection method, the Newton method, and the secant method. Running the script will yield the following results as depicted below. It can be observed from the results obtained that the Newton and the Secant methods are more accurate than the Bisection method. However, each solution is dependent on the respective method’s algorithm. For example, the tolerance that was used in all three methods was predefined to 10-9. If this value changes to something larger than 10-9, then the root returned by each method will change significantly and the function will diverge from 0 even more.

Appendix – Matlab code

%% Assignment 2

clc,clear,closeall;

f = @(x) x^2 – 3*x + 1; % create anonymous function f(x)

%% Part (a) Bisection method

a = 0; b = 1; % interval [a, b] where solution will be searched

tol = 1e-9; % tolerance for the method to terminate

fa = feval(f,a);

fb = feval(f,b);

while (abs(a-b)) >tol + eps*max(abs(a),abs(b))

m = (a+b)/2;

fm = feval(f,m);

if (fa*fm) <= 0

% case where there is a root in [a, m]

b = m;

fb = fm;

else

% case where there is a root in [m, b]

a = m;

fa = fm;

end

end

r1 = (a + b)/2;

%% Part (b) Newton method

fd = @(x) 2*x – 3; % anonymous function of f'(x)

x0 = input(‘Enter initial estimate of root in [0,1]: ‘);

N = input(‘Enter number of maximum iterations: ‘);

xn = x0;

fn = feval(f,xn);

dfn = feval(fd,xn);

for i = 1:N

xn1 = xn – (fn/dfn);

dx = abs(xn1-xn);

xn = xn1;

fn = feval(f,xn);

dfn = feval(fd,xn);

if (dx <tol+eps)

r2 = xn;

break;

end

end

%% Part (c) Secant method

x0 = a; x1 = b;

xk = x1 – (f(x1)*(x1-x0))/(f(x1)-f(x0));

while abs(xk-x0)>tol% terminate loop when criterion is true

xnew = xk – (f(xk)*(xk-x0))/(f(xk)-f(x0));

x0 = xk;

xk = xnew;

end

r3 = xk;

%% Part (d) Summary of all 3 methods

fprintf(‘————————————————————————-\n’)

fprintf(‘%15s %22s %23s\n’,’Bisection’,’Newton’,’Secant’);

fprintf(‘x:    %.14f          %.14f        %.14f\n’,r1,r2,r3);

fprintf(‘f(x): %.14f          %.14f        %.14f\n’,f(r1),f(r2),f(r3));

fprintf(‘————————————————————————-\n’)