In: Advanced Math
Finally, consider the following fixed point iteration xk+1 = g(xk) = arccos −1 1 + e 2x and show that finding a fixed point of g(x) is equivalent to finding a root of f(x) = 0. Use the code fixedpt.m to try to approximate the root using an initial guess of x0 = −3. Can you explain why your iteration behaves as it does? Hint: Plot the fixed-point function and think convergence!
Code in fixedpt.m:-
function [xfinal, niter, xlist] = fixedpt( gfunc, xguess, tol )
% FIXEDPT: Fixed point iteration for x=gfunc(x).
%
%  Sample usage:
%     [xfinal, niter, xlist] = fixedpt( gfunc, xguess, tol )
%
%  Input:
%     gfunc   - fixed point function 
%     xguess  - initial guess at the fixed point
%     tol     - convergence tolerance (OPTIONAL, defaults to 1e-6)
%
%  Output:
%     xfinal  - final estimate of the fixed point
%     niter   - number of iterations to convergence
%     xlist   - list of interates, an array of length 'niter'
% First, do some error checking on parameters.
if nargin < 2
  fprintf( 1, 'FIXEDPT: must be called with at least two arguments' );
  error( 'Usage:  [xfinal, niter, xlist] = fixedpt( gfunc, xguess, [tol] )' );
end
if nargin < 3, tol = 1e-6; end
% fcnchk(...) allows a string function to be sent as a parameter, and
% coverts it to the correct type to allow evaluation by feval().
gfunc = fcnchk(gfunc);
x = xguess;
xlist = [ x ];
niter = 0;
done  = 0;
while ~done,
  xnew  = feval(gfunc,  x);
  xlist = [ xlist; xnew ];  % create a list of x-values 
  niter = niter + 1;
  if abs(x-xnew) < tol,     % stopping tolerance for x only
    done = 1;
  end
  x = xnew;
end
xfinal = xnew;
clc
clear all
close all
format long;
g=@(x) acos(-1./(1+exp(2*x)));
[xfinal, niter, xlist] = fixedpt( g, -3,1e-6);
fplot(g,[-3,3]);
function [xfinal, niter, xlist] = fixedpt( gfunc, xguess, tol
)
% FIXEDPT: Fixed point iteration for x=gfunc(x).
%
% Sample usage:
% [xfinal, niter, xlist] = fixedpt( gfunc, xguess, tol )
%
% Input:
% gfunc - fixed point function
% xguess - initial guess at the fixed point
% tol - convergence tolerance (OPTIONAL, defaults to 1e-6)
%
% Output:
% xfinal - final estimate of the fixed point
% niter - number of iterations to convergence
% xlist - list of interates, an array of length 'niter'
% First, do some error checking on parameters.
if nargin < 2
fprintf( 1, 'FIXEDPT: must be called with at least two arguments'
);
error( 'Usage: [xfinal, niter, xlist] = fixedpt( gfunc, xguess,
[tol] )' );
end
if nargin < 3, tol = 1e-6; end
% fcnchk(...) allows a string function to be sent as a
parameter, and
% coverts it to the correct type to allow evaluation by
feval().
gfunc = fcnchk(gfunc);
x = xguess;
xlist = [ x ];
niter = 0;
done = 0;
while ~done,
xnew = feval(gfunc, x);
xlist = [ xlist; xnew ]; % create a list of x-values
niter = niter + 1;
if abs(x-xnew) < tol, % stopping tolerance for x only
done = 1;
end
x = xnew;
end
xfinal = xnew;
end
