3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3191
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3191
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3191
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3439
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} irr (@var{p}, @var{i}) |
|
19 ## Return the internal rate of return of a series of payments @var{p} |
|
20 ## from an initial investment @var{i} (i.e., the solution of |
|
21 ## @code{npv (r, p) = i}. If the second argument is omitted, a value of |
|
22 ## 0 is used. |
|
23 ## @end deftypefn |
|
24 ## @seealso{npv, pv, and rate} |
3426
|
25 |
3191
|
26 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
27 ## Description: Internal rate of return of an investment |
|
28 |
|
29 function r = irr (p, i) |
3426
|
30 |
3191
|
31 if (nargin == 1) |
|
32 i = 0; |
3456
|
33 elseif (! (nargin == 2)) |
|
34 usage ("irr (p, i)"); |
3191
|
35 endif |
3426
|
36 |
3456
|
37 if (! (is_vector (p))) |
3191
|
38 error ("irr: p must be a vector"); |
|
39 else |
3456
|
40 p_string = strcat ("[", sprintf ("%.15f, ", p), "]"); |
3191
|
41 endif |
|
42 |
3456
|
43 if (! is_scalar (i)) |
3191
|
44 error ("irr: i must be a scalar"); |
|
45 endif |
3426
|
46 |
3456
|
47 r = fsolve (sprintf ("npv (x, %s) - %g", p_string, i), 0.01); |
3426
|
48 |
3191
|
49 endfunction |