Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

16
Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375

Transcript of Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Page 1: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Lecture 3

Bisection method

Downloadbisection02.mAnd ftest2.m

From math.unm.edu/~plushnik/375

Page 2: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

%Bisection method to find roots for function ftest2istep=0;%set initial number of steps to zeroa=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 errorwhile 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) enddisp(['number of steps for Bisection algorithm=',num2str(istep)]);

Page 3: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

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

Page 4: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

>> bisection02f(c)=6.2162 x=1.05f(c)=0.73319 x=0.575f(c)=-0.69847 x=0.3375f(c)=-0.053209 x=0.45625f(c)=0.32019 x=0.515625

f(c)=-2.2204e-015 x=0.465080868f(c)=1.8652e-014 x=0.465080868f(c)=7.9936e-015 x=0.465080868f(c)=3.1086e-015 x=0.465080868number of steps for Bisection algorithm=51

Page 5: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

It is often good idea to plot function first

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-10

0

10

20

30

40

50

60

y

ftest2

Page 6: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Inclass1

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

Page 7: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Newton’s method

Downloadnewton02.mAnd ftest2.m

From math.unm.edu/~plushnik/375

Page 8: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

%Newton's method to find roots for function ftestx0=0.5; %starting pointabserr=10^(-15); %stop criterion - desired absolute erroristep=0;x=x0; %set initial value of x to x0%main loop to find rootdisp('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)]);

Page 9: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

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

Page 10: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Inclass2

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

Page 11: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Secant method

Downloadsecant02.mAnd ftest2.m

From math.unm.edu/~plushnik/375

Page 12: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

%Secant method to find roots for function ftest2x0=0.1; x1=2.0;%starting pointsabserr=10^(-14); %stop criterion - desired absolute erroristep=0;xn1=x0; %set initial value of x to x0xn=x1;%main loop to find rootdisp('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)]);

Page 13: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

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

Page 14: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

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

Page 15: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

Inclass3

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

Page 16: Lecture 3 Bisection method Download bisection02.m And ftest2.m From math.unm.edu/~plushnik/375.

>> secant02Iterations by Secant Methodf(x)=-1.2769 xn=1.5f(x)=-0.088702 xn=0.624324608254261f(x)=0.012856 xn=0.558951914931113f(x)=-0.00013183 xn=0.567227412711665f(x)=-1.9564e-007 xn=0.567143415251049f(x)=2.9781e-012 xn=0.567143290407884f(x)=2.9781e-012 xn=0.567143290409784number of steps for Secant algorithm=6

Answer to inclass3