1 / 16

Lecture 3 Bisection method

Lecture 3 Bisection method. Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375. %Bisection method to find roots for function ftest2 istep=0;%set initial number of steps to zero a=0.1; %initial value for interval (a,b) b=2; %initial value for interval (a,b)

dora
Download Presentation

Lecture 3 Bisection method

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. Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375

  2. %Bisection method to find roots for function ftest2 istep=0;%set initial number of steps to zero a=0.1; %initial value for interval (a,b) b=2; %initial value for interval (a,b) if sign(ftest2(a)*ftest2(b))>=0 error('sign(ftest2(a)*ftest2(b))>=0') end abserr=10^(-15); %stop criterion - desired absolute error while abs(ftest2((a+b)/2))>abserr c=(a+b)/2; %calculate midpoint istep=istep+1; fc=ftest2(c); if fc==0 break %if c is solution then exit end if (ftest2(a)*fc)<0 b=c; else a=c; end disp(['f(c)=',num2str(fc),' x=',num2str(c,10)]);%display value of function f(x) end disp(['number of steps for Bisection algorithm=',num2str(istep)]);

  3. %test function is defined at fourth line; %derivative of function is defined at firth line function [f,fderivative]=ftest2(x) f=exp(2*x)+x-3; fderivative=2*exp(2*x)+1;

  4. >> bisection02 f(c)=6.2162 x=1.05 f(c)=0.73319 x=0.575 f(c)=-0.69847 x=0.3375 f(c)=-0.053209 x=0.45625 f(c)=0.32019 x=0.515625 … f(c)=-2.2204e-015 x=0.465080868 f(c)=1.8652e-014 x=0.465080868 f(c)=7.9936e-015 x=0.465080868 f(c)=3.1086e-015 x=0.465080868 number of steps for Bisection algorithm=51

  5. It is often good idea to plot function first

  6. Inclass1 Modify bisection02.m and ftest2.m to find root of e^(-x)-x=0 at [0.2,1.5]

  7. Newton’s method Download newton02.m And ftest2.m From math.unm.edu/~plushnik/375

  8. %Newton's method to find roots for function ftest x0=0.5; %starting point abserr=10^(-15); %stop criterion - desired absolute error istep=0; x=x0; %set initial value of x to x0 %main loop to find root disp('Iterations by Newton Method'); while abs(ftest2(x))>abserr istep=istep+1; [f,fder]=ftest2(x); disp(['f(x)=',num2str(f),' x=',num2str(x,15)]);%display value of function f(x) x=x-f/fder; end [f,fder]=ftest2(x); disp(['f(x)=',num2str(f),' x=',num2str(x,15)]);%display value of function f(x) disp(['number of steps for Newton algorithm=',num2str(istep)]);

  9. >> newton02 Iterations by Newton Method f(x)=0.21828 x=0.5 f(x)=0.0061135 x=0.466087210490891 f(x)=5.1326e-006 x=0.46508171356867 f(x)=3.6251e-012 x=0.465080867976624 f(x)=-4.4409e-016 x=0.465080867976026 number of steps for Newton algorithm=4

  10. Inclass2 Modify newton02.m and ftest2.m to find root of e^(-x)-x=0 by Newton’s method starting at x=0.6

  11. Secant method Download secant02.m And ftest2.m From math.unm.edu/~plushnik/375

  12. %Secant method to find roots for function ftest2 x0=0.1; x1=2.0;%starting points abserr=10^(-14); %stop criterion - desired absolute error istep=0; xn1=x0; %set initial value of x to x0 xn=x1; %main loop to find root disp('Iterations by Secant Method'); while abs(ftest2(xn))>abserr istep=istep+1; fn=ftest2(xn); fn1=ftest2(xn1); disp(['f(x)=',num2str(fn),' xn=',num2str(xn,15)]);%display value of function f(x) xtmp=xn-(xn-xn1)*fn/(fn-fn1); xn1=xn; xn=xtmp; end f=ftest2(xn); disp(['f(x)=',num2str(fn),' xn=',num2str(xn,15)]);%display value of function f(x) disp(['number of steps for Secant algorithm=',num2str(istep)]);

  13. %test function is defined at fourth line; %derivative of function is defined at firth line function [f,fderivative]=ftest2(x) f=exp(2*x)+x-3; fderivative=2*exp(2*x)+1;

  14. >> secant02 Iterations by Secant Method f(x)=53.5982 xn=2 f(x)=-1.4715 xn=0.157697583825433 f(x)=-1.2804 xn=0.206925256821038 f(x)=0.46299 xn=0.536842578960542 f(x)=-0.094954 xn=0.449229649271443 f(x)=-0.0057052 xn=0.464140200867443 f(x)=7.5808e-005 xn=0.465093357175321 f(x)=-5.9571e-008 xn=0.465080858161814 f(x)=-6.2172e-013 xn=0.465080867975924 f(x)=-6.2172e-013 xn=0.465080867976027 number of steps for Secant algorithm=9 >>

  15. Inclass3 Modify secant02.m and ftest2.m to find root of e^(-x)-x=0 by secant method starting at x=0.2 and x=1.5

  16. Answer to inclass3 >> secant02 Iterations by Secant Method f(x)=-1.2769 xn=1.5 f(x)=-0.088702 xn=0.624324608254261 f(x)=0.012856 xn=0.558951914931113 f(x)=-0.00013183 xn=0.567227412711665 f(x)=-1.9564e-007 xn=0.567143415251049 f(x)=2.9781e-012 xn=0.567143290407884 f(x)=2.9781e-012 xn=0.567143290409784 number of steps for Secant algorithm=6

More Related