]> gitweb.michael.orlitzky.com - octave.git/blobdiff - partition.m
Add the partition function.
[octave.git] / partition.m
diff --git a/partition.m b/partition.m
new file mode 100644 (file)
index 0000000..ac616cf
--- /dev/null
@@ -0,0 +1,28 @@
+function [p,delta] = partition(integerN, a, b)
+  ## Partition the interval [a,b] into integerN subintervals. We do not
+  ## requite that a<b.
+  ##
+  ## INPUTS:
+  ##
+  ##   * integerN - The number of subintervals.
+  ##
+  ##   * a - The "left" endpoint of the interval to partition.
+  ##
+  ##   * b - The "right" endpoint of the interval to partition.
+  ##
+  ##
+  ## OUTPUTS:
+  ##
+  ##   * p - The resulting partition, as a vector of length integerN+1.
+  ##
+  ##   * delta - The distance between x_i and x_{i+1} in the partition.
+  ##
+  ##
+
+  ## We don't use abs() here because `b` might be less than `a`. In that
+  ## case, we want delta negative so that when we add it to `a`, we move
+  ## towards `b`.
+  delta = (b - a)/integerN
+
+  p = [a : delta : b]
+end