Graphical Method and Scanning

Non-linear equations such as can be

presented as

where is independent variable and is a function of .

The graphical interpretation can be made easier

by modifying the equation

Here, we should be looking for the point where

the straight line intersects the

exponential as depicted in Figure.

Bisection Method

Find the root of the function using the Bisection Method. The initial interval are and

Initialize

, so there is a zero in the interval [1, 2]

First iteration

, so the zero is not in the interval [1, 3/2]

Since the zero must be in the interval [1, 3/2], we set and and leave the current values of and unchanged.

step / a / b / xm / y(a) / y(b) / y(m)
1 / 1.0000 / 2.0000 / 1.5000 / -2.0000 / 1.0000 / -0.7500
2 / 1.5000 / 2.0000 / 1.7500 / -0.7500 / 1.0000 / 0.0625
3 / 1.5000 / 1.7500 / 1.6250 / -0.7500 / 0.0625 / -0.3594
4 / 1.6250 / 1.7500 / 1.6875 / -0.3594 / 0.0625 / -0.1523
5 / 1.6875 / 1.7500 / 1.7188 / -0.1523 / 0.0625 / -0.0459

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% program: bisectionmethod.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

% This program uses the bisection method to find the root of an equation

%

% a = left hand end of interval containing the zero

% b = right hand end of interval containing the zero

% tol= stop if absolute value of function is less than tol

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

clear

%

%---starting brackets for x and the error allowed

%

a=1.0;

b= 2.0;

tol = 0.00001;

%

%---writing out headers to the file 'bisect.dat'

%

fid=fopen('bisectmethod.dat','w');

fprintf(fid,'Roots of Equations f(x)=x^2-3 using Bisection Method \n\n')

fprintf(fid,'iter a b xm y(a) y(b) ym \n');

fprintf(fid,'------\n');

%

%---entering the loop to determine the root

%

for i=1:50

%

%---evaluate xroot, and the function at the three points (note that I

% could have used matlab's 'function' command so that I wouldn't have

% to write out the function calculation three times. For now I wanted

% to keep the coding simple.

%

ya =(a^2) - 3;

yb =(b^2) - 3;

xm= 0.5*(a+b)

ym =(xm^2) - 3;

%

%---writing out results to the file 'bisectionmethod.dat'

%

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f \n',i, a, b, xm, ya, yb, ym);

%

%

%---determine the side containing the root

%

if ya*ym < 0

b = xm;

else

a = xm;

end

%---stop the iterations if desired accuracy is reached

%

if abs(a-b) < tol

break

end

end

%

%---close the file

%

fclose(fid)

%

%--writing out final results to the matlab screen

%

disp('The number of iterations required was')

i

disp('The root of the equation is')

format long

xm

disp('The value of the function at the root is')

ym

clear

a=1.0;

b= 2.0;

tol = 0.00001;

fid=fopen('bisectmethod.dat','w');

fprintf(fid,'Roots of Equations f(x)=x^2-3 using Bisection Method \n\n')

fprintf(fid,'iter a b xm y(a) y(b) ym \n');

fprintf(fid,'------\n');

for i=1:50

ya =(a^2) - 3;

yb =(b^2) - 3;

xm= 0.5*(a+b)

ym =(xm^2) - 3;

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f \n',i, a, b, xm, ya, yb, ym);

if ya*ym < 0

b = xm;

else

a = xm;

end

if abs(a-b) < tol

break

end

end

fclose(fid)

disp('The number of iterations required was')

i

disp('The root of the equation is')

format long

xm

disp('The value of the function at the root is')

ym

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% program: Regula-false method.m f(x)=x^3-2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

% This program uses the Regula Falsi method to find the root of an equation

%

% a = left hand end of interval containing the zero

% b = right hand end of interval containing the zero

% tol= stop if absolute value of function is less than tol

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

clear

%

%---starting brackets for x and the error allowed

%

x1=1.0;

xu= 2.0;

tol = 0.00001;

%

%---writing out headers to the file 'regulafalsi.dat'

%

fid=fopen('regulafalsi.dat','w');

fprintf(fid,'Roots of Equations f(x)=x^3-2 using Regula Falsi Method \n\n');

fprintf(fid,'iter x1 xu xr y(xr) \n');

fprintf(fid,'------\n');

%

%---entering the loop to determine the root

%

for i=1:50

%

fx1 =(x1^3) - 2;

fxu =(xu^3) - 2;

xr=xu-fxu*(xu-x1)/(fxu-fx1);

fxr=(xr^3)-2;

%

%---writing out results to the file 'bisectionmethod.dat'

%

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f %7.4f \n',i, x1, xu, xr, fxr);

%

%---stop the iterations if desired accuracy is reached

%

if abs(fxr) < tol;

break

end

if (fx1*fxr<0);

xu=xr;

fxu=fxr;

else

x1=xr;

fx1=fxr;

end

end

%

%---close the file

%

fclose(fid)

%

%--writing out final results to the matlab screen

%

disp('The number of iterations required was')

i

disp('The root of the equation is')

format long

xr

clear

x1=1.0;

xu= 2.0;

tol = 0.00001;

fid=fopen('regulafalsi.dat','w');

fprintf(fid,'Roots of Equations f(x)=x^3-2 using Regula Falsi Method \n\n');

fprintf(fid,'iter x1 xu xr y(xr) \n');

fprintf(fid,'------\n');

for i=1:50

fx1 =(x1^3) - 2;

fxu =(xu^3) - 2;

xr=xu-fxu*(xu-x1)/(fxu-fx1);

fxr=(xr^3)-2;

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f %7.4f \n',i, x1, xu, xr, fxr);

if abs(fxr) < tol;

break

end

if (fx1*fxr<0);

xu=xr;

fxu=fxr;

else

x1=xr;

fx1=fxr;

end

end

fclose(fid)

disp('The number of iterations required was')

i

disp('The root of the equation is')

format long

xr

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% program: Fixed point method.m f(x)=x-ln(4+x)=0

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

% This program uses the Fixed point method to find the root of an equation

%

% x1 = an initial guess

% tol= stop if absolute (x2-xi) less than tol

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

clear

%

%---starting guess for x1 and the error allowed

%

x1=1.;

tol = 0.00001;

%

%---writing out headers to the file 'fixedpoint.dat'

% Ea= Approximate absolute error

% ea= Approximate relative error

fid=fopen('fixedpoint.dat','w');

fprintf(fid,'Roots of Equations f(x)=ln(4+x)=0 using Fixed Point Method \n\n');

fprintf(fid,'iter x1 x2 Ea ea \n');

fprintf(fid,'------\n');

%

%---entering the loop to determine the root

%

for x1=-2:.05:10

y=x1

yy=log(4+x1)

plot(x1,y,'r.',x1,yy,'b.')

hold on

xlabel('x')

ylabel('y')

grid on

text(2.05, 6.8,'* y=x')

text(2.05, 6.4,'+ y=ln(4+x)')

end

x1=1.5000;

for k=1:50

x2 =log(4+x1);

Ea=abs(x2-x1)

ear=Ea/abs(x2)

%

%---writing out results to the file 'fixedpointmethod.dat'

%

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f %7.4f \n',i, x1, x2, Ea, ear);

%

%---stop the iterations if desired accuracy is reached

%

if abs(x2-x1) < tol;

break

end

x1=x2

end

%

%---close the file

%

fclose(fid)

%

%--writing out final results to the matlab screen

%

disp('The number of iterations required was')

i

disp('The root of the equation is')

format long

x1

clear

x1=1.;

tol = 0.00001;

fid=fopen('fixedpoint.dat','w');

fprintf(fid,'Roots of Equations f(x)=ln(4+x)=0 using Fixed Point Method \n\n');

fprintf(fid,'iter x1 x2 Ea ea \n');

fprintf(fid,'------\n');

for x1=-2:.05:10

y=x1

yy=log(4+x1)

plot(x1,y,'r.',x1,yy,'b.')

hold on

xlabel('x')

ylabel('y')

grid on

text(2.05, 6.8,'* y=x')

text(2.05, 6.4,'+ y=ln(4+x)')

end

x1=1.5000;

for k=1:50

x2 =log(4+x1);

Ea=abs(x2-x1)

ear=Ea/abs(x2)

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f %7.4f \n',i, x1, x2, Ea, ear);

if abs(x2-x1) < tol;

break

end

x1=x2

end

fclose(fid)

disp('The number of iterations required was')

i

disp('The root of the equation is')

format long

x1

%Newton-Raphson Method f(x)=sqrt(x)+ln(x)-2sin(x/2)

x0=1.1;

E=1.0E-6;

%

%---writing out headers to the file 'newtonmethod.dat'

%

fid=fopen('newtonmethod.dat','w');

fprintf(fid,'Roots of Equations f(x)=sqrt(x)+ln(x)-2sin(x/2) \n\n')

fprintf(fid,'Using Newton-Raphson Method \n\n')

fprintf(fid,'iter x0 x1 abs(x1-xo) \n');

fprintf(fid,'------\n');

%

%---entering the loop to determine the root

%

for i=1:100

fx0=sqrt(x0)+log(x0)-2*sin(x0/2);

fdx0=1/(2*sqrt(x0))+1/log(x0)-cos(x0/2);

x1=x0-fx0/fdx0;

%---writing out results to the file 'newtonmethod.dat'

%

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f \n',i,x0,x1,abs(x1-x0));

%

if abs(x1-x0)<E;

break;

end

x0=x1;

end

fclose(fid)

disp('Root approximation=')

x0

------

x0=1.1;

E=1.0E-6;

fid=fopen('newtonmethod.dat','w');

fprintf(fid,'Roots of Equations f(x)=sqrt(x)+ln(x)-2sin(x/2) \n\n')

fprintf(fid,'Using Newton-Raphson Method \n\n')

fprintf(fid,'iter x0 x1 abs(x1-xo) \n');

fprintf(fid,'------\n');

for i=1:100

fx0=sqrt(x0)+log(x0)-2*sin(x0/2);

fdx0=1/(2*sqrt(x0))+1/log(x0)-cos(x0/2);

x1=x0-fx0/fdx0;

fprintf(fid,'%4.1f %7.4f %7.4f %7.4f \n',i,x0,x1,abs(x1-x0));

if abs(x1-x0)<E;

break;

end

x0=x1;

end

fclose(fid)

disp('Root approximation=')

x0