Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Fixed point iteration Download fixedpoint.m From math.unm.edu/~plushnik/375.

Similar presentations


Presentation on theme: "Lecture 5 Fixed point iteration Download fixedpoint.m From math.unm.edu/~plushnik/375."— Presentation transcript:

1 Lecture 5 Fixed point iteration Download fixedpoint.m From math.unm.edu/~plushnik/375

2 %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

3 >> g1=inline('1-x^3'); >> g2=inline('(1-x)^(1/3)'); >> g3=inline('(1+2*x^3)/(1+3*x^2)'); Enter inline functions:

4 >>fixedpoint(g1,0.5,10^(-8),10); Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= 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

5 >> fixedpoint(g2,0.5,10^(-8),100); Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0= Error: f0-x0=9.6501e-005 Error: f0-x0= e-005 Error: f0-x0=4.9467e-005 Error: f0-x0= e-005 Error: f0-x0=2.5357e-005 Error: f0-x0= e-005 Error: f0-x0=1.2998e-005 Error: f0-x0= e-006 Error: f0-x0=6.663e-006 Error: f0-x0= e-006 Error: f0-x0=3.4155e-006 Error: f0-x0= e-006 Error: f0-x0=1.7508e-006 Error: f0-x0= e-006 Error: f0-x0=8.9748e-007 Error: f0-x0= e-007 Error: f0-x0=4.6006e-007 Error: f0-x0= e-007 Error: f0-x0=2.3583e-007 Error: f0-x0= e-007 Error: f0-x0=1.2089e-007 Error: f0-x0= e-008 Error: f0-x0=6.1968e-008 Error: f0-x0= e-008 Error: f0-x0=3.1765e-008 Error: f0-x0= e-008 Error: f0-x0=1.6283e-008 Error: f0-x0= e-008 Error: f0-x0=8.3468e-009 Number of iterations: n = 52

6 >> fixedpoint(g3,0.5,10^(-8),100); Error: f0-x0= Error: f0-x0= Error: f0-x0= e-007 Error: f0-x0= e-013 Number of iterations: n = 4 >>

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

8 %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)]);

9 %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;

10 >> 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 >>

11 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

12 >> 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

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

14 >> options = optimset('disp', 'iter', 'tolx', 1.e-15); >> 2],options) Func-count x f(x) Procedure initial interpolation interpolation interpolation interpolation e-005 interpolation e-008 interpolation e-014 interpolation interpolation Zero found in the interval [0.1, 2] ans = >> Example of fzero use

15 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

16 >> 1.5],options) Func-count x f(x) Procedure initial interpolation interpolation e-006 interpolation e-010 interpolation e-016 interpolation e-016 interpolation Zero found in the interval [0.2, 1.5] ans = Answer to inclass4


Download ppt "Lecture 5 Fixed point iteration Download fixedpoint.m From math.unm.edu/~plushnik/375."

Similar presentations


Ads by Google