Codes
Codes
EXAMPLE NO. 1
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
EXAMPLE NO.2
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')
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);
end
%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);
end
%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/