]> gitweb.michael.orlitzky.com - octave.git/blobdiff - fixed_point_method.m
Add the fixed point method and some tests.
[octave.git] / fixed_point_method.m
diff --git a/fixed_point_method.m b/fixed_point_method.m
new file mode 100644 (file)
index 0000000..c28a66e
--- /dev/null
@@ -0,0 +1,31 @@
+function [fixed_point, iterations] = fixed_point_method(g, epsilon, x0)
+  ## Find a fixed_point of the function `g` with initial guess x0.
+  ##
+  ## INPUTS:
+  ##
+  ##   * ``g`` - The function to iterate.
+  ##
+  ##   * ``epsilon`` - We stop when two successive iterations are within
+  ##     epsilon of each other, taken under the infinity norm. halt the
+  ##     search and return the current approximation.
+  ##
+  ## OUTPUTS:
+  ##
+  ##   * ``fixed_point`` - The fixed point that we found.
+  ##
+  ##   * ``iterations`` - The number of bisections that we performed
+  ##   during the search.
+  ##
+
+  iterations = 0;
+  prev = x0;
+  current = g(x0);
+
+  while (norm(current - prev, Inf) > epsilon)
+    prev = current;
+    current = g(current);
+    iterations = iterations + 1;
+  end
+
+  fixed_point = current;
+end