3191
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3191
|
18 |
3407
|
19 ## -*- texinfo -*- |
3439
|
20 ## @deftypefn {Function File} {} pv (@var{r}, @var{n}, @var{p}, @var{l}, @var{method}) |
3407
|
21 ## Returns the present value of an investment that will pay off @var{p} for @var{n} |
|
22 ## consecutive periods, assuming an interest @var{r}. |
3191
|
23 ## |
3439
|
24 ## The optional argument @var{l} may be used to specify an additional |
3407
|
25 ## lump-sum payment made at the end of @var{n} periods. |
3191
|
26 ## |
3439
|
27 ## The optional argument @var{method} may be used to specify whether |
|
28 ## payments are made at the end (@code{"e"}, default) or at the |
|
29 ## beginning (@code{"b"}) of each period. |
3191
|
30 ## |
3439
|
31 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05, |
|
32 ## not 5 percent). |
5642
|
33 ## @seealso{pmt, nper, rate, npv} |
3407
|
34 ## @end deftypefn |
3426
|
35 |
5428
|
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3458
|
37 ## Description: Present value of an investment |
3191
|
38 |
|
39 function v = pv (r, n, p, l, m) |
3426
|
40 |
6497
|
41 if (nargin < 3 || nargin > 5) |
6046
|
42 print_usage (); |
3191
|
43 endif |
3426
|
44 |
6497
|
45 if (! (isscalar (r) && r > -1)) |
3458
|
46 error ("pv: r must be a scalar > -1"); |
6497
|
47 elseif (! (isscalar (n) && n > 0)) |
3458
|
48 error ("pv: n must be a positive scalar"); |
4030
|
49 elseif (! isscalar (p)) |
3458
|
50 error ("pv: p must be a scalar"); |
3191
|
51 endif |
3426
|
52 |
3191
|
53 if (r != 0) |
|
54 v = p * (1 - (1 + r)^(-n)) / r; |
|
55 else |
|
56 v = p * n; |
|
57 endif |
3426
|
58 |
3191
|
59 if (nargin > 3) |
|
60 if (nargin == 5) |
5443
|
61 if (! ischar (m)) |
3458
|
62 error ("pv: `method' must be a string"); |
3191
|
63 endif |
5443
|
64 elseif (ischar (l)) |
3191
|
65 m = l; |
|
66 l = 0; |
|
67 else |
|
68 m = "e"; |
|
69 endif |
3456
|
70 if (strcmp (m, "b")) |
3191
|
71 v = v * (1 + r); |
|
72 endif |
4030
|
73 if (isscalar (l)) |
3191
|
74 v = v + pvl (r, n, l); |
|
75 else |
3458
|
76 error ("pv: l must be a scalar"); |
3191
|
77 endif |
|
78 endif |
3426
|
79 |
3191
|
80 endfunction |
3426
|
81 |