COLLEGE OF E&ME, NUST, RAWALPINDI
DEPARTMENT OF MECHANICAL ENGINEERING
MATH-334 NUMERICAL ANALYSIS LAB
ASSIGNMENT#1
SUBMITTED BY:
WALEED HASSAN
REG NO#460429
45-ME-C
SUBMITTED TO:
Sir Hammad Waheed
DATE:15 October 2025
CODE:
clc;
clear;
close all;
% ------------------- USER INPUTS -------------------------------
funcStr = input('Enter the function: ', 's');
func = str2func(['@(x) ' funcStr]);
x0 = input('Enter the first initial guess: ');
x1 = input('Enter the second initial guess: ');
tol = input('Enter the tolerance : ');
maxIter = input('Enter the maximum number of iterations : ');
derivFunc = @(x) numericDerivative(func, x); % automatic numeric derivative
% ------------------- FUNCTION PLOT ------------------------------
xRange = linspace(x0 - 10, x0 + 10, 1000);
yValues = arrayfun(func, xRange);
figure;
plot(xRange, yValues, 'b', 'LineWidth', 1.5); grid on;
xlabel('x'); ylabel('f(x)');
title('Function Plot');
hold on;
disp('Function plotted successfully.');
disp(' ');
% ------------------- ROOT FINDING METHODS -----------------------
results = struct();
[[Link].root1, [Link].iter1, [Link].table1] =
bisectionMethod(func, x0-1, x0+1, tol, maxIter);
[[Link].root2, [Link].iter2, [Link].table2] =
bisectionMethod(func, x1-1, x1+1, tol, maxIter);
[[Link].root1, [Link].iter1, [Link].table1] =
regulaFalsiMethod(func, x0-1, x0+1, tol, maxIter);
[[Link].root2, [Link].iter2, [Link].table2] =
regulaFalsiMethod(func, x1-1, x1+1, tol, maxIter);
[[Link].root1, [Link].iter1, [Link].table1] =
secantMethod(func, x0, x1, tol, maxIter);
[[Link].root2, [Link].iter2, [Link].table2] =
secantMethod(func, x1, x0, tol, maxIter);
[[Link].root1, [Link].iter1, [Link].table1] =
newtonRaphsonMethod(func, derivFunc, x0, tol, maxIter);
[[Link].root2, [Link].iter2, [Link].table2] =
newtonRaphsonMethod(func, derivFunc, x1, tol, maxIter);
gFunc = @(x) x - func(x); % transformation for fixed-point
[[Link].root1, [Link].iter1, [Link].table1] =
fixedPointMethod(gFunc, x0, tol, maxIter);
[[Link].root2, [Link].iter2, [Link].table2] =
fixedPointMethod(gFunc, x1, tol, maxIter);
% ------------------- DISPLAY SUMMARY ----------------------------
disp(' ');
disp('Root Finding Results Summary');
disp('-----------------------------------------------------------');
disp('Method | Root (First Guess) | Root (Second Guess)');
disp('-----------------------------------------------------------');
fprintf('Bisection | %12.6f | %12.6f\n', [Link].root1,
[Link].root2);
fprintf('Regula Falsi | %12.6f | %12.6f\n', [Link].root1,
[Link].root2);
fprintf('Secant | %12.6f | %12.6f\n', [Link].root1, [Link].root2);
fprintf('Newton-Raphson | %12.6f | %12.6f\n', [Link].root1,
[Link].root2);
fprintf('Fixed-Point | %12.6f | %12.6f\n', [Link].root1, [Link].root2);
disp('-----------------------------------------------------------');
disp(' ');
% ------------------- ASK 5 TIMES FOR TABLE ----------------------
for i = 1:5
query = input(['(' num2str(i) '/5) Enter which table you want to view (Bisection,
Regula Falsi, Secant, Newton Raphson, Fixed Point) or press Enter to skip: '], 's');
switch lower(strtrim(query))
case 'bisection'
disp('--- Bisection Method Table (Guess 1) ---');
disp(array2table([Link].table1, 'VariableNames',
{'Iter','a','b','c','f(c)'}));
disp('--- Bisection Method Table (Guess 2) ---');
disp(array2table([Link].table2, 'VariableNames',
{'Iter','a','b','c','f(c)'}));
case 'regula falsi'
disp('--- Regula Falsi Method Table (Guess 1) ---');
disp(array2table([Link].table1, 'VariableNames',
{'Iter','a','b','x','f(x)'}));
disp('--- Regula Falsi Method Table (Guess 2) ---');
disp(array2table([Link].table2, 'VariableNames',
{'Iter','a','b','x','f(x)'}));
case 'secant'
disp('--- Secant Method Table (Guess 1) ---');
disp(array2table([Link].table1, 'VariableNames',
{'Iter','x0','x1','x_new','f(x_new)'}));
disp('--- Secant Method Table (Guess 2) ---');
disp(array2table([Link].table2, 'VariableNames',
{'Iter','x0','x1','x_new','f(x_new)'}));
case 'newton raphson'
disp('--- Newton-Raphson Method Table (Guess 1) ---');
disp(array2table([Link].table1, 'VariableNames', {'Iter','x','f(x)'}));
disp('--- Newton-Raphson Method Table (Guess 2) ---');
disp(array2table([Link].table2, 'VariableNames', {'Iter','x','f(x)'}));
case 'fixed point'
disp('--- Fixed Point Method Table (Guess 1) ---');
disp(array2table([Link].table1, 'VariableNames', {'Iter','x'}));
disp('--- Fixed Point Method Table (Guess 2) ---');
disp(array2table([Link].table2, 'VariableNames', {'Iter','x'}));
otherwise
disp('No table displayed this round.');
end
disp(' ');
end
% ================================================================
% FUNCTIONS USED BELOW
% ================================================================
function dfdx = numericDerivative(f, x)
h = 1e-6;
dfdx = (f(x + h) - f(x - h)) / (2 * h);
end
function [root, iterations, iterationTable] = bisectionMethod(func, a, b, tol,
maxIter)
iterationTable = [];
if func(a) * func(b) > 0, root = NaN; iterations = 0; return; end
for iter = 1:maxIter
c = (a + b) / 2;
iterationTable = [iterationTable; iter, a, b, c, func(c)];
if abs(func(c)) < tol, root = c; iterations = iter; return; end
if func(a) * func(c) < 0, b = c; else, a = c; end
end
root = c; iterations = maxIter;
end
function [root, iterations, iterationTable] = regulaFalsiMethod(func, a, b, tol,
maxIter)
iterationTable = [];
for iter = 1:maxIter
x = a - func(a)*(b - a)/(func(b) - func(a));
iterationTable = [iterationTable; iter, a, b, x, func(x)];
if abs(func(x)) < tol, root = x; iterations = iter; return; end
if func(a) * func(x) < 0, b = x; else, a = x; end
end
root = x; iterations = maxIter;
end
function [root, iterations, iterationTable] = secantMethod(func, x0, x1, tol, maxIter)
iterationTable = [];
for iter = 1:maxIter
x2 = x1 - func(x1)*(x1 - x0)/(func(x1) - func(x0));
iterationTable = [iterationTable; iter, x0, x1, x2, func(x2)];
if abs(func(x2)) < tol, root = x2; iterations = iter; return; end
x0 = x1; x1 = x2;
end
root = x2; iterations = maxIter;
end
function [root, iterations, iterationTable] = newtonRaphsonMethod(func, derivFunc, x0,
tol, maxIter)
iterationTable = [];
for iter = 1:maxIter
x1 = x0 - func(x0)/derivFunc(x0);
iterationTable = [iterationTable; iter, x0, func(x0)];
if abs(func(x1)) < tol, root = x1; iterations = iter; return; end
x0 = x1;
end
root = x1; iterations = maxIter;
end
function [root, iterations, iterationTable] = fixedPointMethod(gFunc, x0, tol,
maxIter)
iterationTable = [];
for iter = 1:maxIter
x1 = gFunc(x0);
iterationTable = [iterationTable; iter, x0];
if abs(x1 - x0) < tol, root = x1; iterations = iter; return; end
x0 = x1;
end
root = x1; iterations = maxIter;
end
OUTPUT:
Graph: