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 |
|
17 ## usage: irr (p [, i]) |
|
18 ## |
|
19 ## Computes the internal rate of return of a series of payments p from |
|
20 ## an initial investment i, i.e., the solution of npv (r, p) = i. If the |
|
21 ## second argument is omitted, i = 0 is used. |
|
22 ## |
|
23 ## See also: npv; pv, rate. |
3426
|
24 |
3191
|
25 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
26 ## Description: Internal rate of return of an investment |
|
27 |
|
28 function r = irr (p, i) |
3426
|
29 |
3191
|
30 if (nargin == 1) |
|
31 i = 0; |
|
32 elseif !(nargin == 2) |
|
33 usage ("irr (p [, i])"); |
|
34 endif |
3426
|
35 |
3191
|
36 tmp = output_precision; |
|
37 output_precision = 15; |
|
38 if !(is_vector (p)) |
|
39 error ("irr: p must be a vector"); |
|
40 else |
|
41 p_string = type p; |
|
42 endif |
|
43 |
|
44 if !is_scalar (i) |
|
45 error ("irr: i must be a scalar"); |
|
46 endif |
3426
|
47 |
3191
|
48 string = ["function delta = f (r) ", ... |
|
49 "delta = npv (r, %s) - %g; end"]; |
|
50 eval (sprintf (string, p_string, i)); |
|
51 |
|
52 r = fsolve ("f", 0.01); |
3426
|
53 |
3191
|
54 output_precision = tmp; |
3426
|
55 |
3191
|
56 endfunction |