Download presentation
Presentation is loading. Please wait.
Published bySalma Plant Modified over 10 years ago
1
E. T. S. I. Caminos, Canales y Puertos1 Engineering Computation Part 2: Exercises
2
E. T. S. I. Caminos, Canales y Puertos2 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;
3
E. T. S. I. Caminos, Canales y Puertos3 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 fprintf(' xl= %18.8f xu= %18.8f\n',xl,xu); end Bisect=xr;
4
E. T. S. I. Caminos, Canales y Puertos4 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 [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 fprintf(' xl= %18.8f xu= %18.8f\n',xl,xu); end function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x;
5
E. T. S. I. Caminos, Canales y Puertos5 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 xrold=xr; 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 function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x;
6
E. T. S. I. Caminos, Canales y Puertos6 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); end plot([xr],[0],'*'); function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x;
7
E. T. S. I. Caminos, Canales y Puertos7 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; ea=2*es; while ea>es && iter<=imax xrold=xr; xr=xr-f(xr)/f1(xr); iter=iter+1; if xr ~= 0 ea=abs((xr-xrold)/xr)*100; end fprintf(' xr= %18.8f ea= %18.8f\n',xr,ea); end; function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x; function [res]=f1(x); res=1000*((1-(1+x).^(-20))./x-20.*(1+i).^(-21))./x;
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.