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) 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)]);
%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;
>> bisection02 f(c)= x=1.05 f(c)= x=0.575 f(c)= x= f(c)= x= f(c)= x= … f(c)= e-015 x= f(c)=1.8652e-014 x= f(c)=7.9936e-015 x= f(c)=3.1086e-015 x= number of steps for Bisection algorithm=51
It is often good idea to plot function first
Inclass1 Modify bisection02.m and ftest2.m to find root of e^(-x)-x=0 at [0.2,1.5]
Newton’s method Download newton02.m And ftest2.m From math.unm.edu/~plushnik/375
%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)]);
>> newton02 Iterations by Newton Method f(x)= x=0.5 f(x)= x= f(x)=5.1326e-006 x= f(x)=3.6251e-012 x= f(x)= e-016 x= number of steps for Newton algorithm=4
Inclass2 Modify newton02.m and ftest2.m to find root of e^(-x)-x=0 by Newton’s method starting at x=0.6
Secant method Download secant02.m And ftest2.m From math.unm.edu/~plushnik/375
%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)]);
%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;
>> secant02 Iterations by Secant Method f(x)= xn=2 f(x)= xn= f(x)= xn= f(x)= xn= f(x)= xn= f(x)= xn= f(x)=7.5808e-005 xn= f(x)= e-008 xn= f(x)= e-013 xn= f(x)= e-013 xn= number of steps for Secant algorithm=9 >>
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
>> secant02 Iterations by Secant Method f(x)= xn=1.5 f(x)= xn= f(x)= xn= f(x)= xn= f(x)= e-007 xn= f(x)=2.9781e-012 xn= f(x)=2.9781e-012 xn= number of steps for Secant algorithm=6 Answer to inclass3