Assignemnt ECM Ee
Assignemnt ECM Ee
Assignment #2
ECM
Section: Power
Algorithm for Finding Roots in MATLAB
Bisection Method:
clc;
clear;
% User Inputs
func_str = input('Enter the function in terms of x (e.g. x^3 - x - 2): ', 's');
FA = f(a);
return;
end
% Bisection Method
i = 1;
root_found = false;
while i <= N0
p = a + (b - a)/2;
FP = f(p);
root_found = true;
break;
end
i = i + 1;
if FA * FP > 0
a = p;
FA = FP;
else
b = p;
end
end
if ~root_found
end
Sqrt(x)-Cos(x)=0
Fix point method:
clc;
clear;
f = str2func(['@(x)', func_str]);
% Step 2: Symbolically calculate derivative f'(x)
syms x;
f_sym = str2sym(func_str);
g = @(x) x - f(x)/df(x);
i = 1;
converged = false;
while i <= N0
converged = true;
break;
end
p0 = p;
i = i + 1;
end
if ~converged
end
clear;
f = str2func(['@(x)', func_str]);
syms x;
f_sym = str2sym(func_str);
df_sym = diff(f_sym);
% Begin iteration
i = 1;
converged = false;
while i <= N0
fx = f(x0);
dfx = df(x0);
if dfx == 0
break;
end
x1 = x0 - fx / dfx;
converged = true;
break;
end
x0 = x1;
i = i + 1;
end
if ~converged
end
clear;
f = str2func(['@(x)', func_str]);
q0 = f(p0);
q1 = f(p1);
i = 2;
converged = false;
while i <= N0
if q1 == q0
break;
end
converged = true;
break;
end
p0 = p1;
q0 = q1;
p1 = p;
q1 = f(p1);
i = i + 1;
end
if ~converged
end
clear;
f = str2func(['@(x)', func_str]);
q0 = f(p0);
q1 = f(p1);
if q0 * q1 > 0
return;
end
i = 2;
converged = false;
while i <= N0
if q1 == q0
break;
end
converged = true;
break;
end
q = f(p);
i = i + 1;
if q * q1 < 0
p0 = p1;
q0 = q1;
end
p1 = p;
q1 = q;
end
if ~converged
end
Convergence behavior: Very steady and reliable, but slow. Each iteration only halves
the interval.
Accuracy: Eventually reaches the root to the required tolerance, but needs more
steps.
Result: May converge slowly or even diverge, depending on how the function is
transformed (i.e., choice of g(x)g(x)g(x)).
Accuracy: If it converges, it does so slowly and is very sensitive to the chosen form
of g(x)g(x)g(x).
🔹 3. Newton’s Method:
Failure: Can diverge or get stuck if f′(x)≈0f'(x) \approx 0f′(x)≈0 or the guess is poor.
Conclusion: Best method in terms of speed and accuracy, but needs derivative and
good guess.
🔹 4. Secant Method:
Convergence behavior: Slower than Newton, but still quite fast (superlinear).
Conclusion: A great balance — faster than bisection, safer than Newton, but slightly
less efficient.
Result: Root found in around 10–15 iterations, but sometimes much more if it stalls.
Convergence behavior: Slower than Secant, slightly better than Bisection in some
cases.
Problem: One endpoint may get stuck if function shape causes repeated small
changes.
Conclusion: Safer than Secant, but can suffer from slow convergence — a mix of
Bisection and Secant behavior.
Newton’s Method was the fastest, finding the root in the fewest iterations — ideal
when the derivative is known and the guess is good.
Secant Method performed nearly as well as Newton’s, with only slightly more
iterations, but without requiring the derivative.
False Position was slower than both Newton and Secant, but still better than
Bisection in some cases. However, it can be inconsistent depending on function
shape.
Bisection was the most reliable, always converging, but at the cost of more
iterations.