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

Codes

The document provides examples of numerical root-finding methods including the Newton-Raphson method, regula-falsi method, bisection method, and fixed-point method. For each method, two examples are given showing the calculation steps and output.
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)
14 views

Codes

The document provides examples of numerical root-finding methods including the Newton-Raphson method, regula-falsi method, bisection method, and fixed-point method. For each method, two examples are given showing the calculation steps and output.
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/ 7

NEWTON RAPHSON METHOD

EXAMPLE NO. 1

%Newton Raphson Method


f = @ (x) exp(x) -4;
df = @ (x) exp(x);
e = 10^-8;
x0 = 1.5;
n = 10;

if df(x0)~=0
for i=1:n

xn = x0-f(x0)/df(x0);
fprintf ('x%d = %.10f\n',i,xn)
if abs (xn-x0)<e
break

end
if df(xn)==0
disp (failed')
end
x0=xn;

end

end

fprintf ('The root is approximately %.10f\n',xn)

r= input ('Enter lower limit: ');


R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

EXAMPLE NO.2

%Newton Raphson Method


f = @ (x) x^2 -4*x -7;
df = @ (x) 2*x -4;
e = 10^-8;
x0 = 5;
n = 10;

if df(x0)~=0
for i=1:n

xn = x0-f(x0)/df(x0);
fprintf ('x%d = %.10f\n',i,xn)
if abs (xn-x0)<e
break
end
if df(xn)==0
disp (failed')
end
x0=xn;
end

end
fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

REGULA FALSI METHOD

EXAMPLE N0.1

%Regula-Falsi Method
f = @ (x) x^2 -12;
a=3;
b=4;
e = 10^-8;
n = 10;
if f(a)*f(b)<0 && a<b

for i=1:n
xn =(a*f(b)-b*f(a))/(f(b)-f(a));
if f(a)*f(xn)<0
b=xn;
elseif f(b)*f(xn)<0
a=xn;
fprintf ('x%d = %.10f\n',i,xn)
end

if abs (xn-x0)<e
break
end

end
end
fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

EXAMPLE NO.2

%Regula-Falsi Method
f = @ (x) 3*(x) -cos(x) -1;
a=0.60;
b=0.61;
e = 10^-8;
n = 10;
if f(a)*f(b)<0 && a<b

for i=1:n
xn =(a*f(b)-b*f(a))/(f(b)-f(a));
if f(a)*f(xn)<0
b=xn;
elseif f(b)*f(xn)<0
a=xn;
fprintf ('x%d = %.10f\n',i,xn)
end

end
end
fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

BISECTION METHOD
EXAMPLE NO.1

%Bisection Method
f = @ (x) x^2 -3;
a=1;
b=2;
e = 10^-8;
n = 45;
if f(a)*f(b)<0 && a<b

for i=1:n
xn =((a+b)/2);
if f(a)*f(xn)<0
b=xn;
elseif f(b)*f(xn)<0
a=xn;
fprintf ('x%d = %.10f\n',i,xn)
end

end
end
fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

EXAMPLE NO.2

%Bisection Method
f = @ (x) x^4 -(x) -10;
a=1.5;
b=2;
e = 10^-8;
n = 45;
if f(a)*f(b)<0 && a<b

for i=1:n
xn =((a+b)/2);
if f(a)*f(xn)<0
b=xn;
elseif f(b)*f(xn)<0
a=xn;
fprintf ('x%d = %.10f\n',i,xn)
end

end
end
fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

FIXED-POINT METHOD

EXAMPLE NO.1

%Fixed-Point Method
g= @ (x) ((2*x +5) / 2)^(1/3);
a=1;
b=2;
e = 10^-8;
n = 15;
x0 =((a+b)/2);
for i=1:n
x0=xn;
xn=g(xn);

fprintf ('x%d = %.10f\n',i,xn)

end

fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

EXAMPLE NO.2
%Fixed-Point Method
g= @ (x) 1/(x^2+2*x +1);
a=0;
b=1;
e = 10^-8;
n = 45;
x0 =((a+b)/2);
for i=1:n
x0=xn;
xn=g(xn);

fprintf ('x%d = %.10f\n',i,xn)

end

fprintf ('The root is approximately %.10f\n',xn)

%Plotting
r= input ('Enter lower limit: ');
R=input ('Enter upper limit: ');
fplot (f,[-10,10],'m')

Reference:
Shah, P. (2005, August). Numerical Methods Calculators: Bisection, False Position, Iteration, Newton
Raphson, Secant Method: Find a real root an equation using. AtoZmath.com - Homework help.
https://atozmath.com/Menu/ConmMenu.aspx

Gershon, A., Yung, E., Khim, J, (ND). Newton Raphson Method. Brilliant.Org.
https://brilliant.org/wiki/newton-raphson-method/

Krishna. (2021, March 8). What is regula-falsi method?. Goseeko. https://www.goseeko.com/blog/what-


is-regula-falsi-method/#:~:text=Example%3A%20Apply%20Regula%20Falsi%20Method,
%E2%80%93%20cosx%20%E2%80%93%201%20%3D%200.&text=So%2C%20root%20of%20the
%20equation,f(0.61)%20%3C%200.&text=now-,So%2C%20root%20of%20the%20equation%20f(x)
%20%3D%200,f(0.61)%20%3C%200.

You might also like