0% found this document useful (0 votes)
48 views7 pages

Engineering Computation: Exercises

The document describes and provides code for several root-finding algorithms: the bisection method, false position method, fixed point method, modified secant method, and Newton-Raphson method. For each method, the code provides a function that implements the algorithm to find the root of a given function within a specified error tolerance.

Uploaded by

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

Engineering Computation: Exercises

The document describes and provides code for several root-finding algorithms: the bisection method, false position method, fixed point method, modified secant method, and Newton-Raphson method. For each method, the code provides a function that implements the algorithm to find the root of a given function within a specified error tolerance.

Uploaded by

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

Engineering

Computation
Part 2:
Exercises
E. T. S. I. Caminos, Canales y Puertos
1

Bisection Method
% This is the MainBisection program
xl=0.0000001;
xu=1;
es=0.00002;
imax=50;
xx=linspace(xl,xu,20);
yy=f(xx);
plot(xx,yy);
hold on
[Bisect ea iter]=Bisection(xl,xu,es,imax);
fprintf('Solution: %18.12f Relative error: %18.12f Iterations: %5d\n',Bisect,ea,iter);

function [res]=f(x)
res=7500-1000.*(1-(1+x).^(-20))./x;

E. T. S. I. Caminos, Canales y Puertos


2

Bisection Method
function [Bisect ea iter]=Bisection(xl,xu,es,imax);
iter=0;
fl=f(xl);
ea=2*es;
xr=xl;
while ea>es && iter<=imax
xrold=xr;
xr=(xl+xu)/2;
fr=f(xr);
iter=iter+1;
if xr ~= 0
ea=abs((xr-xrold)/xr)*100;
end
test=fl*fr;
if test<0
xu=xr;
else if test>0
xl=xr;
fl=fr;
else
ea=0;
end
end
fprintf(' xl= %18.8f xu= %18.8f\n',xl,xu);
end
Bisect=xr;

E. T. S. I. Caminos, Canales y Puertos


3

False Position Method


xl=0.0000001;
xu=1;
es=0.00002;
imax=100;
xx=linspace(xl,xu,20);
yy=f(xx);
plot(xx,yy);
hold on
plot([xl,xu],[0,0],'*');
[xr ea iter]=FalsePosition(xl,xu,es,imax);
fprintf('Solution: %18.12f Relative error: %18.12f Iterations:
%5d\n',xr,ea,iter);

function [res]=f(x)
res=7500-1000.*(1-(1+x).^(-20))./x;

function [xr ea iter]=FalsePosition(xl,xu,es,imax);


iter=0;
fl=f(xl);
fu=f(xu);
ea=2*es;
xr=xl;
il=0;
iu=0;
while ea>es && iter<=imax;
xrold=xr;
xr=xu-fu*(xl-xu)/(fl-fu);
fr=f(xr);
iter=iter+1;
if xr ~= 0
ea=abs((xr-xrold)/xr)*100;
end
test=fl*fr;
if test<0
xu=xr;
fu=f(xu);
iu=0;
il=il+1;
if il>=2
fl=fl/2;
else if test>0
xl=xr;
fl=f(xl);
il=0;
iu=iu+1;
if iu>=2
fu=fu/2;
else
ea=0;
end
end
end
end
fprintf(' xl= %18.8f xu= %18.8f\n',xl,xu);
end

E. T. S. I. Caminos, Canales y Puertos


4

Fixed Point Method


x0=0.000001;
es=0.0000000001;
imax=30;
[xr ea iter]=FixedPoint(x0,es,imax);
fprintf('Solution: %18.12f Relative error: %18.12f Iterations: %5d\n',xr,ea,iter);
function [xr ea iter]=FixedPoint(x0,es,imax)
xr=x0;
iter=0;
ea=2*es;
while ea>es && iter<=imax
function [res]=f(x)
xrold=xr;
res=7500-1000.*(1-(1+x).^(-20))./x;
xr=g(xrold);
iter=iter+1;
if xr ~= 0
ea=abs((xr-xrold)/xr)*100;
end
fprintf(' xr= %18.8f ea= %18.8f\n',xr,ea);
end

E. T. S. I. Caminos, Canales y Puertos


5

Modified Secant Method


x0=0.1;
es=0.00002;
imax=30;
[xr ea iter]=ModifiedSecant(x0,es,imax);
fprintf('Solution: %18.12f Relative error: %18.12f Iterations: %5d\n',xr,ea,iter);
function [xr ea iter]=ModifiedSecant(x0,es,imax);
xr=x0
iter=0;
ea=2*es;
eps=0.01;
while ea>es && iter<=imax
xrold=xr;
xr=xr-f(xr)/(f(xr+eps)-f(xr-eps))*2*eps;
iter=iter+1;
if xr ~= 0
ea=abs((xr-xrold)/xr)*100;
end
fprintf(' xr= %18.8f ea= %18.8f\n',xr,ea);
function [res]=f(x)
end
plot([xr],[0],'*');
res=7500-1000.*(1-(1+x).^(-20))./x;

E. T. S. I. Caminos, Canales y Puertos


6

Newton-Raphson Method
x0=0.2;
es=0.00002;
imax=30;
[xr ea iter]=NewtonRaphson(x0,es,imax);
fprintf('Solution: %18.12f Relative error: %18.12f Iterations:
%5d\n',xr,ea,iter);
function [xr ea iter]=NewtonRaphson(x0,es,imax)
xr=x0;
iter=0;
function [res]=f1(x);
ea=2*es;
res=1000*((1-(1+x).^(-20))./x-20.*(1+i).^(-21))./x;
while ea>es && iter<=imax
xrold=xr;
xr=xr-f(xr)/f1(xr);
iter=iter+1;
function [res]=f(x)
if xr ~= 0
res=7500-1000.*(1-(1+x).^(-20))./x;
ea=abs((xr-xrold)/xr)*100;
end
fprintf(' xr= %18.8f ea= %18.8f\n',xr,ea);
end;

E. T. S. I. Caminos, Canales y Puertos


7

You might also like