diff scripts/general/linspace.m @ 4:b4df021f796c

[project @ 1993-08-08 01:26:08 by jwe] Initial revision
author jwe
date Sun, 08 Aug 1993 01:26:08 +0000
parents
children 16a24e76d6e0
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/scripts/general/linspace.m
@@ -0,0 +1,40 @@
+function retval = linspace (x1, x2, n)
+
+# usage: linspace (x1, x2, n)
+#
+# Return a vector of n equally spaced points between x1 and x2
+# inclusive. 
+#
+# If the final argument is omitted, n = 100 is assumed.
+#
+# All three arguments must be scalars.
+#
+# See also: logspace
+
+  if (nargin == 2)
+    npoints = 100;
+  elseif (nargin == 3)
+    if (length (n) == 1)
+      npoints = n;
+    else
+      error ("linspace: arguments must be scalars");
+    endif
+  else
+    error ("usage: linspace (x1, x2 [, n])");
+  endif
+
+  if (npoints < 2)
+    error ("linspace: npoints must be greater than 2");
+  endif
+
+  if (length (x1) == 1 && length (x2) == 1)
+    delta = (x2 - x1) / (npoints - 1);
+    retval = zeros (1, npoints);
+    for i = 0:npoints-1
+      retval (i+1) = x1 + i * delta;
+    endfor
+  else
+    error ("linspace: arguments must be scalars");
+  endif
+
+endfunction