%Program 1.2 Fixed-Point Iteration %Computes approximate solution of f(x)=x %Input: function f, initial guess x0, and % number of steps k %Output: Approximate solution xc and err=|x(k+1)-xk| function [xc,err]=fpi(f,x0,k) x(1)=x0; for s=1:k x(s+1)=f(x(s)); end xc=x(k+1); err=f(xc)-xc; ___________________________________________________________________________ clear; format long f=inline('1+x-x.^2/4'); A=zeros(8,2); for i=1:8; [x,y]=fpi(f,1.6,j); A(j,1)=x; A(j,2)=y; end disp(' ') % a blank line disp(' n pn |f(pn)-pn| ') disp('------------------------------------------------------') for k=1:8 disp(sprintf('%2.0f %7.15e %7.15e ',k,A(k,1),A(k,2))) end n pn |f(pn)-pn| ------------------------------------------------------ 1 1.960000000000000e+000 3.960000000000008e-002 2 1.999600000000000e+000 3.999599999999770e-004 3 1.999999960000000e+000 3.999999975690116e-008 4 2.000000000000000e+000 2.220446049250313e-016 5 2.000000000000000e+000 0.000000000000000e+000 6 2.000000000000000e+000 0.000000000000000e+000 7 2.000000000000000e+000 0.000000000000000e+000 8 2.000000000000000e+000 0.000000000000000e+000 % If we try to find p=-2, then it diverges clear; format long f=inline('1+x-x.^2/4'); A=zeros(15,2); for j=1:15; [x,y]=fpi(f,-2.01,j); A(j,1)=x; A(j,2)=y; end disp(' ') % a blank line disp(' n pn |f(pn)-pn| ') disp('------------------------------------------------------') for k=1:15 disp(sprintf('%2.0f %7.15e %7.15e ',k,A(k,1),A(k,2))) end n pn |f(pn)-pn| ------------------------------------------------------ 1 -2.020025000000000e+000 -2.012525015624966e-002 2 -2.040150250156249e+000 -4.055326080315158e-002 3 -2.080703510959401e+000 -8.233177512969458e-002 4 -2.163035286089095e+000 -1.696804122166338e-001 5 -2.332715698305729e+000 -3.603906322804962e-001 6 -2.693106330586225e+000 -8.132054269609008e-001 7 -3.506311757547126e+000 -2.073555535278304e+000 8 -5.579867292825430e+000 -6.783729751385748e+000 9 -1.236359704421118e+001 -3.721463296790684e+001 10 -4.957823001211801e+001 -6.135002227836199e+002 11 -6.630784527957378e+002 -1.099172586404974e+005 12 -1.105803370932931e+005 -3.057002736916584e+009 13 -3.057113317253677e+009 -2.336485458632446e+018 14 -2.336485461689559e+018 -1.364791078171668e+036 15 -1.364791078171668e+036 -4.656636717642458e+071