home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / finance / rate.m < prev    next >
Text File  |  1997-02-19  |  2KB  |  72 lines

  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:  rate (n, p, v [, l] [, method])
  18. ##
  19. ## Computes the rate of return on an investment of present value v which
  20. ## pays p in n consecutive periods.
  21. ##
  22. ## With the optional scalar argument l, one can specify an additional
  23. ## lump-sum payment made at the end of n periods. With the optional
  24. ## string argument `method', one can specify whether payments are made
  25. ## at the end ("e", default) or at the beginning ("b") of each period.
  26. ##
  27. ## See also:  pv, pmt, nper;  npv.
  28.   
  29. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  30. ## Description:  Rate of return of an investment
  31.   
  32. function r = rate (n, p, v, l, m)
  33.  
  34.   if ((nargin < 3) || (nargin > 5))
  35.     usage ("rate (n, p, v [, l] [, method])");
  36.   endif
  37.   
  38.   if !(is_scalar (n) && (n > 0))
  39.     error ("rate:  n must be a positive scalar");
  40.   elseif !is_scalar (p)
  41.     error ("rate:  p must be a scalar");
  42.   elseif !is_scalar (v)
  43.     error ("rate:  p must be a scalar");
  44.   endif
  45.  
  46.   if (nargin == 5)
  47.     if !isstr (m)
  48.       error ("rate:  `method' must be a string");
  49.     endif
  50.   elseif (nargin == 4)
  51.     if isstr (l)
  52.       m = l;
  53.       l = 0;
  54.     else
  55.       m = "e";
  56.     endif
  57.   else
  58.     l = 0;
  59.     m = "e";
  60.   endif
  61.   
  62.   if !is_scalar (l)
  63.     error ("rate:  l must be a scalar");
  64.   endif
  65.   
  66.   string = ["function delta = f (r) ", ...
  67.       "delta = pv (r, %g, %g, %g, \"%s\") - %g;  end"];
  68.   eval (sprintf (string, n, p, l, m, v));
  69.   
  70.   [r, info] = fsolve ("f", 0);
  71.   
  72. endfunction