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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3191
|
19 |
3407
|
20 ## -*- texinfo -*- |
3439
|
21 ## @deftypefn {Function File} {} pv (@var{r}, @var{n}, @var{p}, @var{l}, @var{method}) |
3407
|
22 ## Returns the present value of an investment that will pay off @var{p} for @var{n} |
|
23 ## consecutive periods, assuming an interest @var{r}. |
3191
|
24 ## |
3439
|
25 ## The optional argument @var{l} may be used to specify an additional |
3407
|
26 ## lump-sum payment made at the end of @var{n} periods. |
3191
|
27 ## |
3439
|
28 ## The optional argument @var{method} may be used to specify whether |
|
29 ## payments are made at the end (@code{"e"}, default) or at the |
|
30 ## beginning (@code{"b"}) of each period. |
3191
|
31 ## |
3439
|
32 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05, |
|
33 ## not 5 percent). |
3407
|
34 ## @end deftypefn |
5053
|
35 ## |
3408
|
36 ## @seealso{pmt, nper, rate, and npv} |
3426
|
37 |
3458
|
38 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
39 ## Description: Present value of an investment |
3191
|
40 |
|
41 function v = pv (r, n, p, l, m) |
3426
|
42 |
3191
|
43 if ((nargin < 3) || (nargin > 5)) |
3456
|
44 usage ("pv (r, n, p, l, method)"); |
3191
|
45 endif |
3426
|
46 |
4030
|
47 if (! (isscalar (r) && (r > -1))) |
3458
|
48 error ("pv: r must be a scalar > -1"); |
4030
|
49 elseif (! (isscalar (n) && (n > 0))) |
3458
|
50 error ("pv: n must be a positive scalar"); |
4030
|
51 elseif (! isscalar (p)) |
3458
|
52 error ("pv: p must be a scalar"); |
3191
|
53 endif |
3426
|
54 |
3191
|
55 if (r != 0) |
|
56 v = p * (1 - (1 + r)^(-n)) / r; |
|
57 else |
|
58 v = p * n; |
|
59 endif |
3426
|
60 |
3191
|
61 if (nargin > 3) |
|
62 if (nargin == 5) |
3456
|
63 if (! isstr (m)) |
3458
|
64 error ("pv: `method' must be a string"); |
3191
|
65 endif |
3456
|
66 elseif (isstr (l)) |
3191
|
67 m = l; |
|
68 l = 0; |
|
69 else |
|
70 m = "e"; |
|
71 endif |
3456
|
72 if (strcmp (m, "b")) |
3191
|
73 v = v * (1 + r); |
|
74 endif |
4030
|
75 if (isscalar (l)) |
3191
|
76 v = v + pvl (r, n, l); |
|
77 else |
3458
|
78 error ("pv: l must be a scalar"); |
3191
|
79 endif |
|
80 endif |
3426
|
81 |
3191
|
82 endfunction |
3426
|
83 |