]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/irr.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / irr.m
1 ## Copyright (C) 1995-1998, 2000, 2002, 2004-2007 Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} irr (@var{p}, @var{i})
18 ## Return the internal rate of return of a series of payments @var{p}
19 ## from an initial investment @var{i} (i.e., the solution of
20 ## @code{npv (r, p) = i}.  If the second argument is omitted, a value of
21 ## 0 is used.
22 ## @seealso{npv, pv, rate}
23 ## @end deftypefn
24
25 function r = irr (p, i = 0)
26   ## Check input
27   if (nargin != 1 && nargin != 2)
28     print_usage ();
29   elseif (! (isvector (p)))
30     error ("irr: p must be a vector");
31   elseif (! isscalar (i))
32     error ("irr: i must be a scalar");
33   endif
34
35   ## Solve system
36   f = @(x) npv (x, p) - i;
37   r = fsolve (f, 0);
38
39 endfunction