0% found this document useful (0 votes)
17 views4 pages

Bracketing Methods Tut 3 Numerical

Uploaded by

tolbayoussef04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Bracketing Methods Tut 3 Numerical

Uploaded by

tolbayoussef04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

(a) Bisection Method

Bisection Method Steps

1. Initialize:

o a=0a = 0a=0, b=2b = 2b=2

o Calculate the initial midpoint: c=a+b2c = \frac{a + b}{2}c=2a+b

o Compute f(c)f(c)f(c)

2. Iterate:

o Update the interval based on the sign of f(c)f(c)f(c):

▪ If f(c)=0f(c) = 0f(c)=0: root found.

▪ If f(a)⋅f(c)<0f(a) \cdot f(c) < 0f(a)⋅f(c)<0: update b=cb = cb=c.

▪ If f(b)⋅f(c)<0f(b) \cdot f(c) < 0f(b)⋅f(c)<0: update a=ca = ca=c.

o Continue until the approximate error ϵa<ϵs\epsilon_a < \epsilon_sϵa<ϵs.

MATLAB Implementation

Here’s a MATLAB implementation of the Bisection Method:

function bisection_method()
f = @(x) -x^2 + 1.8*x + 2.5; % Define the function
a = 0; b = 2; % Initial interval
epsilon_s = 0.05; % Desired stopping criterion
tol = inf; % Initialize tolerance
iteration = 0;

while tol > epsilon_s


c = (a + b) / 2; % Midpoint
f_c = f(c);

% Calculate approximate error


if iteration > 0
tol = abs((c - prev_c) / c) * 100; % Approximate relative
error
end
% Update previous midpoint for error calculation
prev_c = c;

% Update interval based on the sign


if f(a) * f_c < 0
b = c; % Root is in [a, c]
else
a = c; % Root is in [c, b]
end

iteration = iteration + 1; % Increment iteration count


end

fprintf('Bisection Method: Approximate root = %.5f after %d


iterations\n', c, iteration);
end

b) False Position Method


False Position Method Steps

1. Initialize:

o Start with a=0a = 0a=0, b=2b = 2b=2

2. Iterate:

o Calculate the root using the formula: c=b−f(b)(a−b)f(a)−f(b)c = b - \frac{f(b)(a


- b)}{f(a) - f(b)}c=b−f(a)−f(b)f(b)(a−b)

o Compute f(c)f(c)f(c) and update the interval based on the sign of f(c)f(c)f(c).
o Continue until the approximate error ϵa<ϵs\epsilon_a < \epsilon_sϵa<ϵs.

MATLAB Implementation

Here’s a MATLAB implementation of the False Position Method:

function false_position_method()
f = @(x) -x^2 + 1.8*x + 2.5; % Define the function
a = 0; b = 2; % Initial interval
epsilon_s = 0.05; % Desired stopping criterion
tol = inf; % Initialize tolerance
iteration = 0;

while tol > epsilon_s


f_a = f(a);
f_b = f(b);

% Calculate the root using the False Position formula


c = b - (f_b * (a - b)) / (f_a - f_b);
f_c = f(c);

% Calculate approximate error


if iteration > 0
tol = abs((c - prev_c) / c) * 100; % Approximate relative
error
end

% Update previous root for error calculation


prev_c = c;

% Update interval based on the sign


if f_a * f_c < 0
b = c; % Root is in [a, c]
else
a = c; % Root is in [c, b]
end
iteration = iteration + 1; % Increment iteration count
end

fprintf('False Position Method: Approximate root = %.5f after %d


iterations\n', c, iteration);
end

You might also like