0% found this document useful (0 votes)
30 views

Assignment 2

The document describes three numerical methods for finding the root of a function: bisection method, Newton-Raphson method, and secant method. It provides pseudo-code to implement each method and applies them to example functions to determine a root. For each method, it calculates the root, plots the iterative process, and displays the number of iterations and accuracy of the root found.
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)
30 views

Assignment 2

The document describes three numerical methods for finding the root of a function: bisection method, Newton-Raphson method, and secant method. It provides pseudo-code to implement each method and applies them to example functions to determine a root. For each method, it calculates the root, plots the iterative process, and displays the number of iterations and accuracy of the root found.
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/ 6

Bisection Method

%Define the function for finding the root


f = @(x)x^2-35;

%Initial guess value


a = 10;
b = 95;

%Tolerance error
tolerance = 1e-6;

%Initialize variables
iteration = 0;
x = (a+b)/2;
x_values = x;
y_values = [f(x)];

%Perform the bisection method


while abs(b-a)>tolerance
x = (a+b)/2;
x_values = [x_values,x];
y_values = [y_values,f(x)];
if f(a)*f(x)<0
b = x;
else
a = x;
end
iteration = iteration + 1;
end

%Plot the root finding graph


figure;
plot(x_values,y_values,'-o');
title('Bisection method root finding:');
xlabel('x');
ylabel('f(x)');
grid on;

%Display the root and the number of iterations


fprintf('Approximate root:%.6f\n',x);
fprintf('Number of iterations:%d\n',iteration);

Output:
Approximate root: 94.999999
Number of iterations: 27
Newton Raphson Method
%Define the function
f = @(x)x^2-25;
%Define the derivative
df = @(x)2*x;
%Initial guess values
x0 = 10;
x1 = 95;

%Tolerance
tolerance = 1e-6;
%Maximum number of iterations
maxIterations = 100;
%Initialize arrays to store iteration data
x_values = [x0,x1];
y_values = [f(x0),f(x1)];
%Perform Newton Raphson iterations
for iteration = 1:maxIterations
x0 = x0 - f(x0) / df(x0);
x1 = x1 - f(x1) / df(x1);

x_values =[x_values,x0,x1];
y_values =[y_values,f(x0),f(x1)];

%Check for convergence


if abs(f(x0))<tolerance && abs(f(x1))<tolerance
break;
end
end
%Plot the root finding process
figure;
plot(x_values,y_values,'-o');
title('Newton-Raphson Method');
xlabel('x');
ylabel('f(x)');
grid on;
%Display the final roots found
fprintf('Root 1:%.6f\n',x0);
fprintf('Root 2:%.6f\n',x1)

Output:
Root 1: 5.000000
Root 2: 5.000000
Secant Method
%Define the function
f = @(x)x^2-15;
%Initial guess values
x0 = 10;
x1 = 95;

%Maximum number of iterations


maxIterations = 100;
%Tolerance
tolerance = 1e-6;
%Initialize arrays to store iteration data
x_values = [x0,x1];
y_values = [f(x0),f(x1)];
%Perform the Secant Method
for i=3:maxIterations
x2=x1-(f(x1)*(x1-x0))/(f(x1)-f(x0));
x_values(i)=x2;
y_values(i)=f(x2);
if abs(x2-x1)<tolerance
break; % Converged
end
x0=x1;
x1=x2;
end

%Display the result


root = x_values(end);
fprintf('Approximate root: %f\n',root);
%Generate a graph of the root finding process
figure;
plot(x_values,y_values,'-o');
grid on;
title("Secant Method Root Finding");
xlabel('x');
ylabel('f(x)');
legend('f(x)','Location','NorthEast');
hold on;
plot(root,f(root),'ro','MarkerSize',10);
hold off;

%Display convergence information


fprintf('Number of iterations: %d\n',i-1);
fprintf('Approximate root: %f\n',root);
fprintf('Final function value: %f\n',f(root));

Output:
Approximate root: 3.872983
Number of iterations: 10
Approximate root: 3.872983
Final function value: 0.000000

You might also like