0% found this document useful (0 votes)
7 views

comput

The document outlines algorithms for various root-finding methods including Bisection, Regula-Falsi, Fixed Point Iteration, Newton-Raphson, and Secant methods. Each method is described with step-by-step procedures and includes MATLAB scripts for implementation. Additionally, a comparison of the methods highlights their convergence speed, requirements for initial guesses, and robustness.

Uploaded by

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

comput

The document outlines algorithms for various root-finding methods including Bisection, Regula-Falsi, Fixed Point Iteration, Newton-Raphson, and Secant methods. Each method is described with step-by-step procedures and includes MATLAB scripts for implementation. Additionally, a comparison of the methods highlights their convergence speed, requirements for initial guesses, and robustness.

Uploaded by

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

DBRE BIRHAN UNIVERSITY

College of engineering

Department of electrical Engineering

Individual Assignment

COURS Title: Computational Methed

COuRS Code:ECEg3042

NAME Mekuanent Taye

ID No 0313/13

(Q1) Develop algorithm for each of the bracketing methods (bisection and Regula-
Falsi method) and open methods (fixed point iteration, Newton-Raphson and
Secant method) for root finding of non-linear equation.

A) Algorithm for the bisection method

1. Choose two initial guesses, a and b, such that f(a) * f(b) < 0, where f(x) is the non-linear
function we want to find the root for.

2. Calculate the midpoint of the interval, c = (a + b) / 2.

3. Evaluate f(c).

4. If (b-a)/2 is close enough to zero (within a user-defined tolerance, tol), c is the root. Stop.

5. If f(c) * f(a) < 0, update b = c. Otherwise, update a = c.


6. Repeat steps 2-5 until the root is found within the desired accuracy.

B) The Regula-Falsi method

1. Define the function f(x) for which you want to find the root.

2. Choose an interval [a, b] such that f(a) * f(b) < 0

3. Calculate the value of the function at the endpoints of the interval: f(a) and f(b).

4. Initialize variables for the iteration count, tolerance level, and maximum number of iterations
allowed.

5. Repeat the following steps until the desired tolerance level is reached.

(5.a.) Calculate the value of the function at the midpoint of the interval: c = (a * f(b) - b *
f(a)) / (f(b) - f(a)).

(5.b.) Determine the sign of f(c) and update the current interval [a, b] based on the sign change:

- If f(c) has the same sign as f(a), set a = c.

- If f(c) has the same sign as f(b), set b = c.

(5.c.) Calculate the absolute error: error = abs(b - a).

(5.d.) Increment the iteration count.

6. Output the approximate root of the equation within the desired tolerance level.

c) Algorithm fixed point iteration method

1. Choose an initial guess for the root, let's call it xo.

2. Define a function g(x) such that the original equation can be written in the form x = g(x).

3. Iterate using the formula x(n+1) = g(xn) until the desired level of accuracy is reached

4. The root of the equation can be approximated by the final value of x after the iteration process.

D) The Newton-Raphson method


1. Define the function f(x) for which you want to find the roots.

2. Define the derivative of the function, f'(x).

3. Choose an initial guess x0 for the root of the function.

4. Set a tolerance level, tol for the solution accuracy.

5. Repeat the following steps until the absolute difference between two consecutive estimates is
less than the tolerance level ε:

 Calculate the value of the function at the current estimate, f(x0).

 Calculate the value of the derivative of the function at the current estimate, f'(x0).

 Update the estimate of the root using the formula: x1 = x0 - f(x0) / f'(x0).

 Set xo = x1.

6. Return the final estimate xo as the approximate root of the function.

E) The secant method

Here is the algorithm for the secant method:

1. Choose initial guesses a and b for the root of the function f(x).

2. Calculate the function values f(a) and f(b).

3. Calculate the next approximation as follows:

c= b - (f(b) * (b - a)) / (f(b) - f(a))

4. Set a = b and b = c.

5. Repeat steps 2-4 until the desired level of accuracy is achieved.

(A) Bisection method

Here is a Matlab script that accepts a user-defined function and implements the Bisection method
for finding the root of a non-linear equation:

function bisection_solver()
double(double xl, double xu, double ea,int iter_max)

f = inline(func,x');

a = input(Enter the lower bound of the interval: ');

b = input(Enter the upper bound of the interval: ');

tol = input(Enter the tolerance value: ');

iter = 0;

maxIter = 5;

while iter < maxIter

c = (a + b) / 2;

fc = f(c);

toll=(b-a)/2;

if toll < tol

disp([Root found at x = ', num2str(c)]);

disp([f(x) = ', num2str(f(c))]);

break;

end

if f(a) * fc < 0

b = c;

else

a = c;

end

iter = iter + 1;
end

if iter == maxIter

disp(The bisection method did not converge');

end

end

,................................................................

>> bisection_method example

Enter the function f(x): x-sin(x)

Enter the lower bound of the interval: 0.7

Enter the upper bound of the interval: 0.9

Enter the tolerance value: 0.02

Root found at x = 0.8875

f(x) = 0.11200

(B) Regula-falsi method

Here is a Matlab script that accepts a user-d pmefined function and implements the Regula Falsi
method for finding the root of a non-linear equation:

function regulaFalsiSolver()

func = input(Enter the function in terms of x: ', s');

f = str2func([@(x)' func]);

a = input(Enter the lower bound of the interval: ');

b = input(Enter the upper bound of the interval: ');

tolerance = input(Enter the tolerance: ');

if f(a) * f(b) > 0


error(The function values at the interval endpoints have the same sign. Choose a different interval.');

end

iter = 0;

while (b - a) >= tolerance

c = (a*f(b) - b*f(a)) / (f(b) - f(a));

if f(c) == 0

break;

elseif f(a) * f(c) < 0

b = c;

else

a = c;

end

iter = iter + 1;

end

disp([Root found at x = ', num2str(c)]);

disp([Iterations: ', num2str(iter)]);

end

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

>> regulaFalsi_method

Enter the function in terms of x: x-cos(x)

Enter the lower bound of the interval: 0.5

Enter the upper bound of the interval: 0.9

Enter the tolerance: 0.02

Root found at x = 0.73909

Iterations: 10

f(x) = 8.1451e-06 (0.0000081451)


(C) Fixed point iteration method

Here is an m-file in MATLAB that accepts a user-defined function and implements the fixed-point iteration method
for root finding of a nonlinear equation:

function fixedPointIterationFuncSolver()

equation = input(Enter the function g(x) in terms of x: ',s');

g = str2func([@(x) ' equation]);

x0 = input(Enter initial guess x0: ');

tol = input(Enter tolerance: ');

maxIterations = 100;

iteration = 0;

while iteration < maxIterations

x1 = g(x0);

if abs(x1 - x0) < tol

fprintf(Root found at x = %.6f\n', x1);

break;

end

x0 = x1;

iteration = iteration + 1;

end

if iteration == maxIterations

disp(Root not found within maximum iterations.');

end

end

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

>> fixed_Point_Iteration_method

Enter the function g(x) in terms of x: x-cos(x)


Enter initial guess x0: 0.68

Enter tolerance: 0.02

Root found at x = -1.570795

D) Newton Raphson method

Here is a Matlab script that accepts a user-defined function and implements the Newton Raphson method for finding
the root of a non-linear equation:

function newton_raphson_solver()

func = input(Enter the function f(x): ', s');

f = str2func([@(x) ' func]);

x0 = input(Enter initial guess x0: ');

tolerance = input(Enter tolerance: ');

max_iter = input(Enter maximum number of iterations: ');

iter = 1;

while iter <= max_iter

f_x0 = f(x0);

df_x0 = (f(x0 + tolerance) - f(x0)) / tolerance;

x1 = x0 - f_x0/df_x0;

if abs(x1 - x0) < tolerance

disp([Root found: x = ', num2str(x1)]);

return;

end

x0 = x1;

iter = iter + 1;

end

disp(Root not found within maximum iterations.');

end
>> Newton_Raphson_method3

Enter the function f(x): x-cos(x)

Enter initial guess x0: 1

Enter tolerance: 0.02

Enter maximum number of iterations: 100

Root found: x = 0.73917

f(x)=1.4204e-04 or 0.00014204

E) Secant method

Matlab script that implements the Secant method for root finding of a user-defined function:

function secantMethod()

func = input(Enter the function in terms of x: ',s');

f = str2func([@(x)', func]);

a = input(Enter the first initial guess: ');

b = input(Enter the second initial guess: ');

tol = input(Enter the tolerance level: ');

max_iter = 100;

iter = 0;

error = tol + 1;

while error > tol && iter < max_iter

c = b - f(b) * (b - a) / (f(b) - f(a));

error = abs(c - b);

a= b;

b=c;
iter = iter + 1;

end

if error <= tol

disp([Root found at x = ', num2str(c), after ', num2str(iter), iterations']);

else

disp(Solution did not converge within the specified tolerance level');

end

end

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

>> secant_Method

Enter the function in terms of x: x-cos(x)

Enter the first initial guess: 0.5

Enter the second initial guess: 0.9

Enter the tolerance level: 0.02

Root found at x = 0.7388 after 2 iterations

f(x)=-4.7717e-04=-0.00047717

(Q3) compare and contrast each method in detail

1. Secant Method:

- Requires two initial guesses.

- Generally, Converges faster than the Bisection method but slower than the Newton-Raphson
method.

- Not as efficient as Newton's method


2. Newton-Raphson Method:

- Requires only one initial guess.

- Generally, converges quickly.

- Requires the computation of the derivative of the function, which may be challenging for
complex functions.

- May fail to converge if the initial guess is far from the actual root or if the derivative is close to
zero near the root.

3. Fixed-Point Iteration Method:

- Requires the function to be transformed into an equivalent fixed-point iteration form.

- Convergence may be slow or may not converge at all for certain functions or initial guesses.

- Requires careful selection of the iteration function for convergence.

- Can converge to different roots depending on the initial guess and the iteration function.

4. Regula-Falsi Method:

- Requires two initial guesses, one bracketing the root.

- Converges relatively slowly compared to the Secant and Newton-Raphson methods.

- Can be more robust than the Secant method for certain functions and initial guesses.

- May suffer from slow convergence if the initial bracketing interval is too wide.

5. Bisection Method:

- Requires an initial bracketing interval where the function changes sign.

- Guaranteed to converge to a root due to the intermediate value theorem.

- Converges slowly compared to other methods.

- Simple and robust, but may require a large number of iterations to achieve convergence.

You might also like