0% found this document useful (0 votes)
8 views19 pages

Assignemnt ECM Ee

The document outlines various root-finding algorithms implemented in MATLAB, including the Bisection Method, Fixed Point Iteration, Newton's Method, Secant Method, and False Position Method. Each method is evaluated based on its results, convergence behavior, accuracy, and reliability, with Newton's Method being the fastest and most accurate, while Bisection is the most reliable but slower. The Fixed Point Method is noted for its unpredictability, often leading to slow convergence or failure.

Uploaded by

pakpattan464
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)
8 views19 pages

Assignemnt ECM Ee

The document outlines various root-finding algorithms implemented in MATLAB, including the Bisection Method, Fixed Point Iteration, Newton's Method, Secant Method, and False Position Method. Each method is evaluated based on its results, convergence behavior, accuracy, and reliability, with Newton's Method being the fastest and most accurate, while Bisection is the most reliable but slower. The Fixed Point Method is noted for its unpredictability, often leading to slow convergence or failure.

Uploaded by

pakpattan464
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/ 19

Ahmadyaar

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');

f = str2func(['@(x)', func_str]); % Convert to function handle

a = input('Enter the left endpoint a: ');

b = input('Enter the right endpoint b: ');

TOL = input('Enter the tolerance TOL: ');

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

FA = f(a);

% Check if root is guaranteed in the interval

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

disp('Error: f(a) and f(b) must have opposite signs.');

return;

end

% Bisection Method

i = 1;

root_found = false;

while i <= N0

p = a + (b - a)/2;

FP = f(p);

% Plot current midpoint

plot(p, FP, 'ro');

if FP == 0 || (b - a)/2 < TOL

fprintf(' Root found at p = %.10f after %d iterations\n', p, i);


plot(p, FP, 'go', 'MarkerSize', 10, 'MarkerFaceColor', 'g'); % Mark root

root_found = true;

break;

end

i = i + 1;

if FA * FP > 0

a = p;

FA = FP;

else

b = p;

end

end

% Final message if root not found

if ~root_found

fprintf(' Method failed after N0 = %d iterations\n', N0);

end

Questions solved in MATLAB

Sqrt(x)-Cos(x)=0
Fix point method:

clc;

clear;

% Step 1: Get the equation f(x) from user

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

f = str2func(['@(x)', func_str]);
% Step 2: Symbolically calculate derivative f'(x)

syms x;

f_sym = str2sym(func_str);

df_sym = diff(f_sym); % f'(x)

df = matlabFunction(df_sym); % Convert to function handle

% Step 3: Define g(x) = x - f(x)/f'(x)

g = @(x) x - f(x)/df(x);

% Step 4: Get inputs for fixed-point iteration

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

TOL = input('Enter tolerance (e.g., 1e-5): ');

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

fprintf('\n%-10s %-20s %-20s\n', 'Iter', 'p(i)', '|p(i) - p(i-1)|');

i = 1;

converged = false;

while i <= N0

p = g(p0); % Apply Newton-derived g(x)

diff = abs(p - p0);

fprintf('%-10d %-20.10f %-20.10f\n', i, p, diff);

if diff < TOL

fprintf('\nRoot found at p = %.10f after %d iterations\n', p, i);

converged = true;

break;

end

p0 = p;

i = i + 1;
end

if ~converged

fprintf('\n Method failed after %d iterations.\n', N0);

end

Questions solved in MATLAB:


Newton Method:
clc;

clear;

% Get function as input from user

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

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

% Use symbolic to compute derivative

syms x;

f_sym = str2sym(func_str);

df_sym = diff(f_sym);

df = matlabFunction(df_sym); % Convert to function handle

% Input initial guess, tolerance and max iterations

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

TOL = input('Enter the tolerance (e.g., 1e-5): ');

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

fprintf('\n%-10s %-20s %-20s\n', 'Iter', 'x(i)', '|x(i)-x(i-1)|');

% Begin iteration

i = 1;

converged = false;

while i <= N0

fx = f(x0);

dfx = df(x0);

if dfx == 0

fprintf(' Derivative zero. Method fails at iteration %d.\n', i);

break;

end
x1 = x0 - fx / dfx;

diff = abs(x1 - x0);

fprintf('%-10d %-20.10f %-20.10f\n', i, x1, diff);

if diff < TOL

fprintf('\nRoot found: x = %.10f after %d iterations\n', x1, i);

converged = true;

break;

end

x0 = x1;

i = i + 1;

end

if ~converged

fprintf('\n Method did not converge within %d iterations.\n', N0);

end

Questions Solved in MATLAB:


Secant Method:
clc;

clear;

% Get function as input from user

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

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

% Input initial approximations, tolerance and max iterations

p0 = input('Enter the initial guess p0: ');

p1 = input('Enter the next guess p1: ');

TOL = input('Enter the tolerance (e.g., 1e-5): ');

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

fprintf('\n%-10s %-20s %-20s\n', 'Iter', 'p(i)', '|p(i)-p(i-1)|');

% Initial function evaluations

q0 = f(p0);

q1 = f(p1);

i = 2;

converged = false;

while i <= N0

if q1 == q0

fprintf('Division by zero. Method fails at iteration %d.\n', i);

break;

end

p = p1 - q1 * (p1 - p0) / (q1 - q0);

diff = abs(p - p1);


fprintf('%-10d %-20.10f %-20.10f\n', i, p, diff);

if diff < TOL

fprintf('\n ✅ Root found: p = %.10f after %d iterations\n', p, i);

converged = true;

break;

end

% Update values for next iteration

p0 = p1;

q0 = q1;

p1 = p;

q1 = f(p1);

i = i + 1;

end

if ~converged

fprintf('\n Method did not converge within %d iterations.\n', N0);

end

Questions solved in MATLAB:


False Position Method:
clc;

clear;

% Get function as input from user

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

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

% Input initial approximations, tolerance and max iterations

p0 = input('Enter the initial guess p0: ');


p1 = input('Enter the next guess p1: ');

TOL = input('Enter the tolerance (e.g., 1e-5): ');

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

% Check if initial values bracket a root

q0 = f(p0);

q1 = f(p1);

if q0 * q1 > 0

fprintf('f(p0) and f(p1) must have opposite signs. Method fails.\n');

return;

end

fprintf('\n%-10s %-20s %-20s\n', 'Iter', 'p(i)', '|p(i)-p(i-1)|');

i = 2;

converged = false;

while i <= N0

if q1 == q0

fprintf(' Division by zero. Method fails at iteration %d.\n', i);

break;

end

p = p1 - q1 * (p1 - p0) / (q1 - q0);

diff = abs(p - p1);

fprintf('%-10d %-20.10f %-20.10f\n', i, p, diff);

if diff < TOL

fprintf('\n ✅ Root found: p = %.10f after %d iterations\n', p, i);

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

fprintf('\n ❌ Method did not converge within %d iterations.\n', N0);

end

Questions Solved in MATLAB:


Summary of Results:
🔹 1. Bisection Method:

 Result: Root found after ~25 iterations (depends on tolerance).

 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.

 Conclusion: Excellent for safety and certainty, but not efficient.

🔹 2. Fixed Point Iteration:

 Result: May converge slowly or even diverge, depending on how the function is
transformed (i.e., choice of g(x)g(x)g(x)).

 Convergence behavior: Highly dependent on transformation; if ∣g′(x)∣>1|g'(x)| >


1∣g′(x)∣>1, it diverges.

 Accuracy: If it converges, it does so slowly and is very sensitive to the chosen form
of g(x)g(x)g(x).

 Conclusion: Unreliable unless function is carefully prepared. Often not used in


practice for root finding.

🔹 3. Newton’s Method:

 Result: Root found in about 4–6 iterations.

 Convergence behavior: Very fast if the initial guess is close; convergence is


quadratic.

 Accuracy: High accuracy in few steps.

 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:

 Result: Root found in around 6–8 iterations.

 Convergence behavior: Slower than Newton, but still quite fast (superlinear).

 Accuracy: Similar to Newton but slightly more steps.

 Pros: Doesn’t need derivative.

 Conclusion: A great balance — faster than bisection, safer than Newton, but slightly
less efficient.

🔹 5. False Position Method:

 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.

🧠 Final Comparative Insights (Based on Results):

 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.

 Fixed Point Method showed unpredictable results. It either converged slowly or


failed entirely depending on how f(x)=0f(x) = 0f(x)=0 was rewritten.

You might also like