190 likes | 442 Views
Lecture 5 Fixed point iteration. Download fixedpoint.m From math.unm.edu/~plushnik/375. %fixedpoint.m - solution of nonlinear equation by fixed point iterations function [x,n, xn] = fixedpoint(f, x0, tol, nmax) % find the root of equation x=f(x) by fixed point method; % input:
E N D
Lecture 5 Fixed point iteration Download fixedpoint.m From math.unm.edu/~plushnik/375
%fixedpoint.m - solution of nonlinear equation by fixed point iterations function [x,n, xn] = fixedpoint(f, x0, tol, nmax) % find the root of equation x=f(x) by fixed point method; % input: % f - inline function % x0 - initial guess % tol - exit condition f(x) < tol % nmax - maximum number of iterations % output: % x - the approximation for the root % n - number of iterations % xn - vector of apporximations x(iter) % compute function at initial guesses f0 = f(x0); n = 0; % begin iterations while ((abs(f0-x0) > tol) && (n < nmax)) x0 = f0; f0 = f(x0); disp(['Error: f0-x0=',num2str(f0-x0)]); if f0 == x0 % x0 is a root, done break; end n = n+1; xn(n) = x0; end if n==nmax disp('warning: maximum iterations reached without conversion'); end x=x0; disp(['Number of iterations: n = ',num2str(n)]); end
Enter inline functions: >> g1=inline('1-x^3'); >> g2=inline('(1-x)^(1/3)'); >> g3=inline('(1+2*x^3)/(1+3*x^2)');
>>fixedpoint(g1,0.5,10^(-8),10); Error: f0-x0=-0.54492 Error: f0-x0=0.63396 Error: f0-x0=-0.85998 Error: f0-x0=0.89482 Error: f0-x0=-0.9955 Error: f0-x0=0.99662 Error: f0-x0=-1 Error: f0-x0=1 Error: f0-x0=-1 Error: f0-x0=1 warning: maximum iterations reached without conversion Number of iterations: n = 10
>> fixedpoint(g2,0.5,10^(-8),100); Error: f0-x0=-0.20282 Error: f0-x0=0.15148 Error: f0-x0=-0.10605 Error: f0-x0=0.077491 Error: f0-x0=-0.054795 Error: f0-x0=0.039626 Error: f0-x0=-0.028184 Error: f0-x0=0.020281 Error: f0-x0=-0.01447 Error: f0-x0=0.010387 Error: f0-x0=-0.0074232 Error: f0-x0=0.0053217 Error: f0-x0=-0.0038066 Error: f0-x0=0.0027272 Error: f0-x0=-0.0019517 Error: f0-x0=0.0013978 Error: f0-x0=-0.0010005 Error: f0-x0=0.00071648 Error: f0-x0=-0.00051291 Error: f0-x0=0.00036726 Error: f0-x0=-0.00026293 Error: f0-x0=0.00018826 Error: f0-x0=-0.00013478 Error: f0-x0=9.6501e-005 Error: f0-x0=-6.9091e-005 Error: f0-x0=4.9467e-005 Error: f0-x0=-3.5416e-005 Error: f0-x0=2.5357e-005 Error: f0-x0=-1.8155e-005 Error: f0-x0=1.2998e-005 Error: f0-x0=-9.3063e-006 Error: f0-x0=6.663e-006 Error: f0-x0=-4.7705e-006 Error: f0-x0=3.4155e-006 Error: f0-x0=-2.4454e-006 Error: f0-x0=1.7508e-006 Error: f0-x0=-1.2535e-006 Error: f0-x0=8.9748e-007 Error: f0-x0=-6.4257e-007 Error: f0-x0=4.6006e-007 Error: f0-x0=-3.2938e-007 Error: f0-x0=2.3583e-007 Error: f0-x0=-1.6885e-007 Error: f0-x0=1.2089e-007 Error: f0-x0=-8.6551e-008 Error: f0-x0=6.1968e-008 Error: f0-x0=-4.4367e-008 Error: f0-x0=3.1765e-008 Error: f0-x0=-2.2743e-008 Error: f0-x0=1.6283e-008 Error: f0-x0=-1.1658e-008 Error: f0-x0=8.3468e-009 Number of iterations: n = 52
>> fixedpoint(g3,0.5,10^(-8),100); Error: f0-x0=-0.031106 Error: f0-x0=-0.0008513 Error: f0-x0=-6.1948e-007 Error: f0-x0=-3.2774e-013 Number of iterations: n = 4 >>
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)=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 >>
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
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
Matlab function: fzero • X = FZERO(FUN,X0), • X0 a scalar: Attempts to find a zero of the function FUN near X0. • FUN is a function handle. • The value X returned by FZERO is near a point • where FUN changes sign (if FUN is continuous), or NaN, if the srootfinding • search fails. • X = FZERO(FUN,X0), • X0 a 2-vector. Assumes that FUN(X0(1)) • and FUN(X0(2)) differ in sign, insuring a root. • X = FZERO(FUN,X0,OPTIONS). • Solves the equation with default optimization parameters replaced by values • in the string OPTIONS, an argument created with the OPTIMSET function.
Example of fzero use >> options = optimset('disp', 'iter', 'tolx', 1.e-15); >> fzero(@ftest2,[0.1 2],options) Func-count x f(x) Procedure 2 0.1 -1.6786 initial 3 0.157698 -1.4715 interpolation 4 0.556708 0.601452 interpolation 5 0.440938 -0.143633 interpolation 6 0.463256 -0.0110609 interpolation 7 0.465084 2.0255e-005 interpolation 8 0.465081 -3.08857e-008 interpolation 9 0.465081 -8.61533e-014 interpolation 10 0.465081 0 interpolation Zero found in the interval [0.1, 2] ans = 0.4651 >>
Inclass4 Modify ftest2.m to find root of e^(-x)-x=0 by Brent’s method starting at x=0.2 and x=1.5
Answer to inclass4 >> fzero(@ftest2b,[0.2 1.5],options) Func-count x f(x) Procedure 2 0.2 0.618731 initial 3 0.624325 -0.0887015 interpolation 4 0.571121 -0.0062285 interpolation 5 0.567143 1.13316e-006 interpolation 6 0.567143 -8.15018e-010 interpolation 7 0.567143 -1.11022e-016 interpolation 8 0.567143 -1.11022e-016 interpolation Zero found in the interval [0.2, 1.5] ans = 0.5671