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; |
|
33 elseif !(nargin == 2) |
|
34 usage ("irr (p [, i])"); |
|
35 endif |
3426
|
36 |
3191
|
37 tmp = output_precision; |
|
38 output_precision = 15; |
|
39 if !(is_vector (p)) |
|
40 error ("irr: p must be a vector"); |
|
41 else |
|
42 p_string = type p; |
|
43 endif |
|
44 |
|
45 if !is_scalar (i) |
|
46 error ("irr: i must be a scalar"); |
|
47 endif |
3426
|
48 |
3191
|
49 string = ["function delta = f (r) ", ... |
|
50 "delta = npv (r, %s) - %g; end"]; |
|
51 eval (sprintf (string, p_string, i)); |
|
52 |
|
53 r = fsolve ("f", 0.01); |
3426
|
54 |
3191
|
55 output_precision = tmp; |
3426
|
56 |
3191
|
57 endfunction |