iterator.R

Template for a fixed point iterator
language: R
license: GPL 2

Code for Snippet:

                
# general fixed point iterator template
 
myIterator<-function(xinit,f,eps=1e-6,itmax=100,verbose=FALSE,...) {
xold<-xinit
itel<-1
repeat {
	xnew<-f(xold,...)
	if (verbose) cat(
		"Iteration: ",formatC(itel,width=3, format="d"),
		"xold: ",formatC(xold,digits=8,width=12,format="f"),
		"xnew: ",formatC(xnew,digits=8,width=12,format="f"),
		"\n")	
	if ((supDist(xold,xnew) < eps) || (itel == itmax)) return(xnew)
	xold<-xnew; itel<-itel+1
	}
}
 
supDist<-function(x,y) return(max(abs(x-y)))
 
fSqrt<-function(x,y) (x+(y/x))/2
 
comments powered by Disqus

Info

Source Site:

Jan

Tags: Iterator Fixed Points

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 540
Date Added: 2013-03-07 16:31:43
Last Modified: 2013-04-17 22:12:19

Web Analytics