samer@3: % limit - Get the limit of an infinite sequence if it exists samer@3: % samer@3: % limit :: samer@3: % (A,A->nonneg) ~ 'metric', samer@3: % seq(A), ~ 'sequence of converging values', samer@3: % options { samer@3: % maxit :: natural/10e6 ~ 'maximum iterations'; samer@3: % tol :: nonneg/1e-5 ~ 'convergence tolerance' samer@3: % } samer@3: % -> A, natural. samer@3: samer@3: function [y,its]=limit(d,X,varargin) samer@37: opts=options('maxit',10e6,'tol',1e-5,varargin{:}); samer@3: samer@3: tol=opts.tol; samer@3: S.x=head(X); samer@3: S.X=next(X); samer@3: S.f=false; samer@3: S.its=0; samer@3: S=iterate(@converger,S,'its',opts.maxit); samer@3: y=S.x; samer@3: its=S.its; samer@3: samer@3: function S=converger(S) samer@3: [xx,S.X]=decons(S.X); samer@3: S.its=S.its+1; samer@3: if d(S.x,xx)