0% found this document useful (0 votes)
4 views8 pages

Experiment4_2305903

The document demonstrates the use of the Regular-Falsi and Newton-Raphson methods to find the roots of various nonlinear equations. It includes coding examples, outputs, and graphs for each method applied to specific equations with defined intervals and tolerances. The conclusion highlights the effectiveness of both methods in solving nonlinear equations, with Newton-Raphson showing faster convergence.

Uploaded by

techxsumit1904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Experiment4_2305903

The document demonstrates the use of the Regular-Falsi and Newton-Raphson methods to find the roots of various nonlinear equations. It includes coding examples, outputs, and graphs for each method applied to specific equations with defined intervals and tolerances. The conclusion highlights the effectiveness of both methods in solving nonlinear equations, with Newton-Raphson showing faster convergence.

Uploaded by

techxsumit1904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT 4

AIM : Demonstration of false position method (Regular-Falsi Method) and Newton


Raphson Method for the below equation.

OBJECTIVE : To find the root of the nonlinear equation using two numerical
methods :

1. Regular Falsi Method: Based on linear interpolation and the intermediate value
theorem.
2. Newton-Raphson Method: An iterative approach using the derivative of the
function for faster convergence.

PROBLEM STATEMENT :
Find the root of f(x) = x^3 - x - 1 by using false-position method in the interval [1,2]
with a tolerance of 0.001.

CODING :
clc;
clear all;
f = @(x) x^3 - x - 1 ;
a = 1;
b = 2;
tol = 0.001;
if f(a) * f(b) > 0
disp('The function does not change sign in the given interval. Choose another
interval.');
return;
end
iteration = 0;
while abs(f((a * f(b) - b * f(a)) / (f(b) - f(a)))) > tol
iteration = iteration + 1;
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
if f(c) == 0
break;
elseif f(a) * f(c) < 0
b = c;
else
a = c;
end
end
root = c;
disp(['The root of the function is: ', num2str(root)]);
disp(['Number of iterations: ', num2str(iteration)]);

OUTPUT :
The root of the function is: 1.3243
Number of iterations: 8

GRAPH :

10
Error vs Iteration for False Position Method
0.09

0.08

0.07

0.06

0.05
Error

0.04

0.03

0.02

0.01

0
2 3 4 5 6 7 8
Iteration
PROBLEM STATEMENT :
Find the root of f(x) = x^3 - x^2 + 2 by using false-position method in the interval [-
10,10] with a tolerance of 0.01.

CODING :
clc;
clear all;
f = @(x) x^3 - x^2 + 2;
a = -10;
b = 10;
tol = 0.01;
if f(a) * f(b) > 0
disp('The function does not change sign in the given interval. Choose another
interval.');
return;
end
iteration = 0;
values = [];
errors = [];
while true
iteration = iteration + 1;
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
values = [values, c];
if iteration > 1
error = abs(values(end) - values(end-1));
errors = [errors, error];
if error < tol
break;
end
else
errors = [errors, NaN];
end
if f(c) == 0

11
break;
elseif f(a) * f(c) < 0
b = c;
else
a = c;
end
end
root = c;
disp(['The root of the function is: ', num2str(root)]);
disp(['Number of iterations: ', num2str(iteration)]);
figure;
plot(1:iteration, errors, '-o');
xlabel('Iteration');
ylabel('Error');
title('Error vs Iteration for False Position Method');
grid on;

OUTPUT :
The root of the function is: -0.99796
Number of iterations: 226

GRAPH :

PROBLEM STATEMENT :
Find the root of f(x) = cos(x) - x * exp(x) by using false-position method in the
interval [0,1] with a tolerance of 0.00001.

CODING :
clc;
clear all;
f = @(x) cos(x) - x * exp(x);
a = 0;
b = 1;
tol = 0.00001;

12
if f(a) * f(b) > 0
disp('The function does not change sign in the given interval. Choose another
interval.');
return;
end
iteration = 0;
values = [];
errors = [];
while true
iteration = iteration + 1;
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
values = [values, c];

if iteration > 1
error = abs(values(end) - values(end-1));
errors = [errors, error];
if error < tol
break;
end
else
errors = [errors, NaN];
end
if f(c) == 0
break;
elseif f(a) * f(c) < 0
b = c;
else
a = c;
end
end
root = c;
disp(['The root of the function is: ', num2str(root)]);
disp(['Number of iterations: ', num2str(iteration)]);
figure;
plot(1:iteration, errors, '-o');
xlabel('Iteration');
ylabel('Error');
title('Error vs Iteration for False Position Method');
grid on;

OUTPUT :
The root of the function is: 0.51775
Number of iterations: 11

GRAPH :

13
Error vs Iteration for False Position Method
0.14

0.12

0.1

0.08
Error

0.06

0.04

0.02

0
2 3 4 5 6 7 8 9 10 11
Iteration

PROBLEM STATEMENT:
Find the root of f(x) = x^3 - x - 1 by using Newton Raphson in the interval [1,2] with
a tolerance of 0.001.

CODING:
function y = newtonRaphson(f, df, x0, e)
i = 0;
x = x0;
errors = [];
while true
c = x - f(x) / df(x);
errors = [errors, abs(f(c))];
i = i + 1;
if abs(c - x) < e
break;
end
x = c;
end
disp(["The root is :", num2str(c)]);
disp(["Number of iterations:", num2str(i)]);
figure;
plot(1:length(errors), errors, '-o');
xlabel('Iteration');
ylabel('Error (|f(c)|)');
title('Error vs Iteration for Newton Raphson Method');
y = c;
end
f = @(x) x^3 - x - 1;
df = @(x) 3 * x^2 - 1;
x0 = 1;
e = 0.001;
root = newtonRaphson(f, df, x0, e);

14
OUTPUT:
"The root is " "1.3247"
"Number of iterations:" "4"

GRAPH :
Error vs Iteration for Newton Raphson Method
0.9

0.8

0.7

0.6
Error (|f(c)|)

0.5

0.4

0.3

0.2

0.1

0
1 1.5 2 2.5 3 3.5 4
Iteration

PROBLEM STATEMENT:
Find the root of f(x) = x^3 - x^2 + 2 by using Newton Raphson in the interval [-10,
10] with a tolerance of 0.001
CODING:

function y = newtonRaphson(f, df, x0, e)


i = 0;
x = x0;
errors = [];
while true
c = x - f(x) / df(x);
errors = [errors, abs(f(c))];
i = i + 1;
if abs(c - x) < e
break;
end
x = c;
end
disp(["The root is :", num2str(c)]);
disp(["Number of iterations:", num2str(i)]);
figure;
plot(1:length(errors), errors, '-o');
xlabel('Iteration');
ylabel('Error (|f(c)|)');
title('Error vs Iteration for Newton Raphson Method');

15
y = c;
end
f = @(x) x^3 - x^2 + 2;
df = @(x) 3*x^2 - 2*x;
x0 = 1;
e = 0.001;

root = newtonRaphson(f, df, x0, e);

OUTPUT:
“The root is :" "-1"
"Number of iterations:" "2"

GRAPH :
Error vs Iteration for Newton Raphson Method
1

0.8

0.6

0.4

0.2
Error (|f(c)|)

-0.2

-0.4

-0.6

-0.8

-1
1 1.2 1.4 1.6 1.8 2
Iteration

PROBLEM STATEMENT: Find the root of f(x) = cos(x) - x*exp(x) by using


Newton Raphson Method in the interval [0, 1] with
a tolerance of 0.001.
CODING:
function y = newtonRaphson(f, df, x0, e)
i = 0;
x = x0;
errors = [];
while true
c = x - f(x) / df(x);
errors = [errors, abs(f(c))];
i = i + 1;
if abs(c - x) < e
break;
end
x = c;
end
disp(["The root is :", num2str(c)]);
disp(["Number of iterations:", num2str(i)]);
figure;
plot(1:length(errors), errors, '-o');
xlabel('Iteration');

16
ylabel('Error (|f(c)|)');
title('Error vs Iteration for Newton Raphson Method');
y = c;
end
f = @(x) cos(x) - x*exp(x);
df = @(x) -sin(x) - exp(x)- x * exp(x);
x0 = 1;
e = 0.00001;
root = newtonRaphson(f, df, x0, e);

OUTPUT :
"The root is :" "0.51776"
"Number of iterations:" "5"

GRAPH :
Error vs Iteration for Newton Raphson Method
0.5

0.45

0.4

0.35

0.3
Error (|f(c)|)

0.25

0.2

0.15

0.1

0.05

0
1 1.5 2 2.5 3 3.5 4 4.5 5
Iteration
CONCLUSION :
The root of non-linear equations was successfully found using both the Regular Falsi
and Newton-Raphson methods with their tolerance values . While Regular Falsi
ensured convergence through interval refinement, Newton-Raphson provided faster
results with a good initial guess. Both methods validated their effectiveness in solving
nonlinear equations.

NAME : SUMIT MANDAL


ROLL : 2305903
SEC : CSE - 30

17

You might also like