comparison scripts/finance/irr.m @ 3191:e4f4b2d26ee9

[project @ 1998-10-23 05:43:59 by jwe]
author jwe
date Fri, 23 Oct 1998 05:44:01 +0000
parents
children f8dde1807dee
comparison
equal deleted inserted replaced
3190:70eb3f4136cd 3191:e4f4b2d26ee9
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik
2 ##
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.
7 ##
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
11 ## General Public License for more details.
12 ##
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.
24
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)
29
30 if (nargin == 1)
31 i = 0;
32 elseif !(nargin == 2)
33 usage ("irr (p [, i])");
34 endif
35
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
47
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);
53
54 output_precision = tmp;
55
56 endfunction