1 / 7

Engineering Computation

Engineering Computation. Part 2: Exercises. Bisection Method. function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x;. % 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

naeva
Download Presentation

Engineering Computation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Bisection Method function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x; % 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); E. T. S. I. Caminos, Canales y Puertos

  3. 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

  4. False Position Method 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 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); E. T. S. I. Caminos, Canales y Puertos

  5. Fixed Point Method function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x; 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 E. T. S. I. Caminos, Canales y Puertos

  6. Modified Secant Method function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x; 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],'*'); E. T. S. I. Caminos, Canales y Puertos

  7. Newton-Raphson Method function [res]=f1(x); res=1000*((1-(1+x).^(-20))./x-20.*(1+i).^(-21))./x; function [res]=f(x) res=7500-1000.*(1-(1+x).^(-20))./x; 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; E. T. S. I. Caminos, Canales y Puertos

More Related