comput
comput
College of engineering
Individual Assignment
COuRS Code:ECEg3042
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.
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.
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.
1. Define the function f(x) for which you want to find the root.
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:
6. Output the approximate root of the equation within the desired tolerance level.
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.
5. Repeat the following steps until the absolute difference between two consecutive estimates is
less than the tolerance level ε:
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.
1. Choose initial guesses a and b for the root of the function f(x).
4. Set a = b and b = c.
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');
iter = 0;
maxIter = 5;
c = (a + b) / 2;
fc = f(c);
toll=(b-a)/2;
break;
end
if f(a) * fc < 0
b = c;
else
a = c;
end
iter = iter + 1;
end
if iter == maxIter
end
end
,................................................................
f(x) = 0.11200
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()
f = str2func([@(x)' func]);
end
iter = 0;
if f(c) == 0
break;
b = c;
else
a = c;
end
iter = iter + 1;
end
end
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
>> regulaFalsi_method
Iterations: 10
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()
maxIterations = 100;
iteration = 0;
x1 = g(x0);
break;
end
x0 = x1;
iteration = iteration + 1;
end
if iteration == maxIterations
end
end
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
>> fixed_Point_Iteration_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()
iter = 1;
f_x0 = f(x0);
x1 = x0 - f_x0/df_x0;
return;
end
x0 = x1;
iter = iter + 1;
end
end
>> Newton_Raphson_method3
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()
f = str2func([@(x)', func]);
max_iter = 100;
iter = 0;
error = tol + 1;
a= b;
b=c;
iter = iter + 1;
end
else
end
end
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
>> secant_Method
f(x)=-4.7717e-04=-0.00047717
1. Secant Method:
- Generally, Converges faster than the Bisection method but slower than the Newton-Raphson
method.
- 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.
- Convergence may be slow or may not converge at all for certain functions or initial guesses.
- Can converge to different roots depending on the initial guess and the iteration function.
4. Regula-Falsi Method:
- 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:
- Simple and robust, but may require a large number of iterations to achieve convergence.