diff general/numerical/fixpoint.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children beb8a3f4a345
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/numerical/fixpoint.m	Sat Jan 12 19:21:22 2013 +0000
@@ -0,0 +1,26 @@
+function y=fixpoint(f,x0,varargin)
+% fixpoint - Find the fixed point of a function by repeated evaluation
+%
+% fixpoint :: 
+%    (A->A)	~'function to iterate',
+%    A      ~'initial value',
+%    options {
+%       its    :: natural/inf ~'maximimum numbert of iterations';
+%       testfn :: (A,A->boolean) ~'convergence test'
+%    }
+% -> maybe A ~'final value or nan if none'.
+
+
+opts=prefs('its',inf,'testfn',@epseq,varargin{:});
+
+conv=opts.testfn;
+its =opts.its; 
+i=0; while i<its
+	x=f(x0);
+	if conv(x0,x), break; end
+	x0=x; i=i+1;
+end
+
+if i<its, y=x; else y=nan; end
+
+function b=epseq(x,y), b=abs(y-x)<=eps(x);