X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Ffinancial-0.4.0%2Frate.m;fp=octave_packages%2Ffinancial-0.4.0%2Frate.m;h=64d02f85318c57b03ec944ce6b757915202c3d7b;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/financial-0.4.0/rate.m b/octave_packages/financial-0.4.0/rate.m new file mode 100644 index 0000000..64d02f8 --- /dev/null +++ b/octave_packages/financial-0.4.0/rate.m @@ -0,0 +1,67 @@ +## Copyright (C) 1995-1998, 2000, 2002, 2004-2007 Kurt Hornik +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}) +## @deftypefnx {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}, @var{l}) +## @deftypefnx {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}, @var{l}, @var{method}) +## @deftypefnx {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}, @var{method}) +## Return the rate of return @var{r} on an investment of present value @var{v} +## which pays @var{p} in @var{n} consecutive periods. +## +## The optional argument @var{l} may be used to specify an additional +## lump-sum payment made at the end of @var{n} periods. +## +## The optional string argument @var{method} may be used to specify +## whether payments are made at the end (@code{"e"}, default) or at the +## beginning (@code{"b"}) of each period. +## @seealso{pv, pmt, nper, npv} +## @end deftypefn + +function r = rate (n, p, v, l = 0, m = "e") + + if (nargin < 3 || nargin > 5) + print_usage (); + elseif (!isnumeric (n) || !isscalar (n) || n <= 0) + error ("number of consecutive periods `n' must be a positive scalar"); + elseif (!isnumeric (p) || !isscalar (p)) + error ("second argument `p' must be a numeric scalar"); + elseif (!isnumeric (v) || !isscalar (v)) + error ("present value `v' must be a numeric scalar"); + + ## the following checks is to allow using default value for `l' while specifying `m' + elseif (nargin == 5) + if (!isnumeric (l) || !isscalar (l)) + error ("value of additional lump-sum payment `l' must be numeric scalar"); + elseif (!ischar (m)) + error ("`method' must be a string") + endif + elseif (nargin == 4) + if (ischar (l)) + m = l; + l = 0; # default value for `l' again + elseif (!isnumeric (l) || !isscalar (l)) + error ("fourth argument must either be a numeric scalar for lump-sum payment `l' or a string for `method'"); + endif + endif + + if (!any (strcmpi (l, {"e","b"}))) + error ("`method' must either be `e' or `b") + endif + + f = @(x) pv (x, n, p, l, m) - v; + r = fsolve (f, 0); + +endfunction