Mercurial > hg > octave-lyh
comparison scripts/general/linspace.m @ 2697:6243c37ae2c5
[project @ 1997-02-18 17:51:21 by jwe]
author | jwe |
---|---|
date | Tue, 18 Feb 1997 17:51:21 +0000 |
parents | c0f319a1e51d |
children | 8b262e771614 |
comparison
equal
deleted
inserted
replaced
2696:eb2ade3c6609 | 2697:6243c37ae2c5 |
---|---|
1 # Copyright (C) 1993, 1994, 1995 John W. Eaton | 1 ## Copyright (C) 1993, 1994, 1995 John W. Eaton |
2 # | 2 ## |
3 # This file is part of Octave. | 3 ## This file is part of Octave. |
4 # | 4 ## |
5 # Octave is free software; you can redistribute it and/or modify it | 5 ## Octave is free software; you can redistribute it and/or modify it |
6 # under the terms of the GNU General Public License as published by the | 6 ## under the terms of the GNU General Public License as published by the |
7 # Free Software Foundation; either version 2, or (at your option) any | 7 ## Free Software Foundation; either version 2, or (at your option) any |
8 # later version. | 8 ## later version. |
9 # | 9 ## |
10 # Octave is distributed in the hope that it will be useful, but WITHOUT | 10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | 12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 # for more details. | 13 ## for more details. |
14 # | 14 ## |
15 # You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 # along with Octave; see the file COPYING. If not, write to the Free | 16 ## along with Octave; see the file COPYING. If not, write to the Free |
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 17 ## Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | |
19 ## usage: linspace (x1, x2, n) | |
20 ## | |
21 ## Return a vector of n equally spaced points between x1 and x2 | |
22 ## inclusive. | |
23 ## | |
24 ## If the final argument is omitted, n = 100 is assumed. | |
25 ## | |
26 ## All three arguments must be scalars. | |
27 ## | |
28 ## See also: logspace | |
29 | |
30 ## Author: jwe | |
18 | 31 |
19 function retval = linspace (x1, x2, n) | 32 function retval = linspace (x1, x2, n) |
20 | |
21 # usage: linspace (x1, x2, n) | |
22 # | |
23 # Return a vector of n equally spaced points between x1 and x2 | |
24 # inclusive. | |
25 # | |
26 # If the final argument is omitted, n = 100 is assumed. | |
27 # | |
28 # All three arguments must be scalars. | |
29 # | |
30 # See also: logspace | |
31 | 33 |
32 if (nargin == 2) | 34 if (nargin == 2) |
33 npoints = 100; | 35 npoints = 100; |
34 elseif (nargin == 3) | 36 elseif (nargin == 3) |
35 if (length (n) == 1) | 37 if (length (n) == 1) |
43 | 45 |
44 if (npoints < 2) | 46 if (npoints < 2) |
45 error ("linspace: npoints must be greater than 2"); | 47 error ("linspace: npoints must be greater than 2"); |
46 endif | 48 endif |
47 | 49 |
48 # In some cases x1 + delta * (npoints - 1) will not be equal to x2, so | 50 ## In some cases x1 + delta * (npoints - 1) will not be equal to x2, |
49 # we cheat and force the last value to be x2. | 51 ## so we cheat and force the last value to be x2. |
50 | 52 |
51 if (length (x1) == 1 && length (x2) == 1) | 53 if (length (x1) == 1 && length (x2) == 1) |
52 delta = (x2 - x1) / (npoints - 1); | 54 delta = (x2 - x1) / (npoints - 1); |
53 retval = [x1+(0:npoints-2)*delta, x2]; | 55 retval = [x1+(0:npoints-2)*delta, x2]; |
54 else | 56 else |