Name of Experiment
Solving roots of the equation using following methods:
f(x) = x^3 - x - 2
a) Bisection Method
b) False Position Method
c) Fixed Point Iteration Method
d) Newton-Raphson Method
Bisection method
Code:
% Bisection Method with Re-entry of a and b in MATLAB
% Define the function for which you want to find the root
f = @(x) x^3 - x - 2; % Example function f(x) = x^3 - x - 2
% Set tolerance and maximum number of iterations
tolerance = 1e-6; % Tolerance for stopping the iteration
max_iter = 100; % Maximum number of iterations
% Ask the user to input values for m and n
while true
% Input: interval [a, b] where f(a) and f(b) have opposite signs
m = input('Enter the value of m (lower bound): ');
n = input('Enter the value of n (upper bound): ');
fm = f(m);
fn = f(n);
% Check if f(a) and f(b) have opposite signs
if fm * fn > 0
fprintf('f(m) and f(n) must have opposite signs. Try again.\n');
else
break; % Exit the loop if the condition is satisfied
end
end
% Bisection method loop
for iter = 1:max_iter
% Calculate the midpoint
t = (m + n) / 2;
fc = f(t);
% Check if the root is found within the tolerance
if abs(fc) < tolerance
fprintf('The root is approximately: %.6f\n', t);
fprintf('Found in %d iterations.\n', iter);
break;
end
% Update the interval
if fm * fc < 0
n = t; % Root is in the left half
fn = fc;
else
m = t; % Root is in the right half
fm = fc;
end
end
% If maximum iterations are reached
if iter == max_iter
fprintf('Maximum number of iterations reached.\n');
fprintf('Approximate root is: %.6f\n', t);
end
Output:
False Position Method
Code:
% False Position (Regula Falsi) Method in MATLAB
% Define the function for which you want to find the root
f = @(x) x^3 - x - 2; % Example function f(x) = x^3 - x - 2
% Set tolerance and maximum number of iterations
tolerance = 1e-6; % Tolerance for stopping the iteration
max_iter = 100; % Maximum number of iterations
% Ask the user to input values for x and y
while true
% Input: interval [x, y] where f(x) and f(y) have opposite signs
X = input('Enter the value of a (lower bound): ');
y = input('Enter the value of b (upper bound): ');
fx = f(X);
fy = f(y);
% Check if f(x) and f(y) have opposite signs
if fx * fy > 0
fprintf('f(a) and f(b) must have opposite signs. Try again.\n');
else
break; % Exit the loop if the condition is satisfied
end
end
% False Position method loop
for iteration = 1:max_iter
% Calculate the position z using the formula for Regula Falsi
z = y - (fy * (y - X)) / (fy - fx);
fz = f(z);
% Check if the root is found within the tolerance
if abs(fz) < tolerance
fprintf('The root is approximately: %.6f\n', z);
fprintf('Found in %d iterations.\n', iteration);
break;
end
% Update the interval based on the sign of f(z)
if fx * fz < 0
y = z; % Root is in the left half
fy = fz;
else
X = z; % Root is in the right half
fx = fz;
end
end
% If maximum iterations are reached
if iteration == max_iter
fprintf('Maximum number of iterations reached.\n');
fprintf('Approximate root is: %.6f\n', z);
end
Output:
Fixed Point Iteration Method
Code:
% Fixed Point Iteration Method in MATLAB
% Define the function h(x) = x (rearranged form of f(x) = 0)
h = @(x) nthroot(x+2, 3); % Rearranged form of f(x) = x^3 - x - 2 = 0
% Initial guess
x0 = input('Enter the initial guess: ');
% Set tolerance and maximum number of iterations
tol = 1e-6; % Tolerance for stopping the iteration
max_iter = 100; % Maximum number of iterations
% Fixed Point Iteration loop
fprintf('Iteration\t x_n\t\t h(x_n)\n'); % Table header
for iter = 1:max_iter
% Calculate the next approximation using g(x)
x1 = h(x0);
% Display the iteration and current values
fprintf('%d\t\t %.6f\t %.6f\n', iter, x0, x1);
% Check if the result is within tolerance
if abs(x1 - x0) < tol
fprintf('The root is approximately: %.6f\n', x1);
fprintf('Found in %d iterations.\n', iter);
break;
end
% Update the current value for the next iteration
x0 = x1;
end
% If maximum iterations are reached
if iter == max_iter
fprintf('Maximum number of iterations reached.\n');
fprintf('Approximate root is: %.6f\n', x1);
end
Output:
Newton-Raphson Method
Code:
% Newton-Raphson Method in MATLAB
% Define the function f(x) and its derivative f'(x)
f = @(x) x^3 - x - 2; % Example function f(x) = x^3 - x - 2
df = @(x) 3*x^2 - 1; % Derivative of f(x), f'(x) = 3x^2 - 1
% Initial guess
x0 = input('Enter the initial guess: ');
% Set tolerance and maximum number of iterations
tolerance = 1e-6; % Tolerance for stopping the iteration
max_iteration = 100; % Maximum number of iterations
% Newton-Raphson loop
fprintf('Iteration\t x_n\t\t f(x_n)\n'); % Table header
for iter = 1:max_iteration
% Calculate the next approximation using Newton-Raphson formula
x1 = x0 - f(x0) / df(x0);
% Display the iteration and current values
fprintf('%d\t\t %.6f\t %.6f\n', iter, x0, f(x0));
% Check if the result is within tolerance
if abs(x1 - x0) < tolerance
fprintf('The root is approximately: %.6f\n', x1);
fprintf('Found in %d iterations.\n', iter);
break;
end
% Update the current value for the next iteration
x0 = x1;
end
% If maximum iterations are reached
if iter == max_iteration
fprintf('Maximum number of iterations reached.\n');
fprintf('Approximate root is: %.6f\n', x1);
end
Output: