0
$\begingroup$

I am trying to combine multiple functions into one. I have defined following functions: (PV refers to Present value, FV refers to Future value, i or IPER refers to interest per period, pmt refers to payment per period and n or NPER refers to number of periods)

PV[i_, n_, pmt_, fv_] := N[(pmt/i - fv)\[Cross](1/(1 + i)^n) - pmt/i];  
FV[i_, n_, pmt_, pv_] := N[pmt/i - (1 + i)^n\[Cross](pmt/i + pv)];  
PMT[i_, n_, pv_, fv_] := N[(pv + (pv + fv)/((1 + i)^n - 1))*(-i)];    
NPER[i_, pmt_, pv_, fv_] := 
  NSolve[0 == pv *(1 + i)^n + pmt*(((1 + i)^n - 1)/i) + fv, n, Reals];    
IPER[n_, pmt_, pv_, fv_] := 
  NSolve[0 == pv *(1 + i)^n + pmt*(((1 + i)^n - 1)/i) + fv, i, Reals]; 

In each of the above function, there are four known scalars and one unknown scalar. I am thinking to make a single function that can handle all of the above function: given the value of four scalars, one can find the value the remaining scalar.

Let me make it a bit clearer. I am trying to make a combined function as follows:

TimeValueFun[i_, n_, pmt_,pv_, fv_]:=Module[{IPER,NPER,PMT,PV,FV},
If missing[fv], FV= N[pmt/i - (1 + i)^n\[Cross](pmt/i + pv)];
output FV;
elseif missing[pv],PV=N[(pmt/i - fv)\[Cross](1/(1 + i)^n) - pmt/i];
output PV;

and so on................. ( I am sure this is not correct).

Can anybody help me?

The simpler the better.

Thanks in advance.

$\endgroup$
  • $\begingroup$ Jagra, thanks for editing my question. $\endgroup$ – ramesh Oct 9 '14 at 17:57
  • 2
    $\begingroup$ Mma restricts the use of "N", N[expr] gives the numerical value of expr. Also "I",I represents the imaginary unit Sqrt[-1]. You need to use other function names. $\endgroup$ – Jagra Oct 9 '14 at 17:57
  • $\begingroup$ A question: do you use this I[n_, pmt_, pv_, fv_] anywhere? $\endgroup$ – Jagra Oct 9 '14 at 18:03
  • $\begingroup$ Jagra, yes this function is used to calculate interest rate per period given other variables. $\endgroup$ – ramesh Oct 9 '14 at 18:04
  • 2
    $\begingroup$ See also: TimeValue and Annuity $\endgroup$ – kglr Oct 9 '14 at 18:08
2
$\begingroup$

A quick and dirty way (I am sure there are more consice and simpler versions. I updated the answer by changing function name to the one mentioned in OP and included the original functions within the Module,

TimeValueFun[$i_, $n_, $pmt_, $pv_, $fv_] 
    := Module[{p},
    PV[i_, n_, pmt_, fv_] := N[(pmt/i - fv)\(1/(1 + i)^n) - pmt/i];  
    FV[i_, n_, pmt_, pv_] := N[pmt/i - (1 + i)^n\(pmt/i + pv)];  
    PMT[i_, n_, pv_, fv_] := N[(pv + (pv + fv)/((1 + i)^n - 1))*(-i)];    
    NPER[i_, pmt_, pv_, fv_] := NSolve[0 == pv *(1 + i)^n + pmt*(((1 + i)^n - 1)/i) + fv, n, Reals];    
    IPER[n_, pmt_, pv_, fv_] := NSolve[0 == pv *(1 + i)^n + pmt*(((1 + i)^n - 1)/i) + fv, i, Reals]; 
     Which[
       MatchQ[$pv, {}], p = PV[$i, $n, $pmt, $fv],
       MatchQ[$fv, {}], p = FV[$i, $n, $pmt, $pv],
       MatchQ[$pmt, {}], p = PMT[$i, $n, $pv, $fv],
       MatchQ[$n, {}], p = NPER[$i, $pmt, $pv, $fv],
       MatchQ[$i, {}], p = IPER[$n, $pmt, $pv, $fv],
   True, Print["error"]
   ];
 p]

Then call the function with a {} for the unknown scalar.

You can define your functions internal the AllTogether function, if you do not need them anywhere else (I was not sure if that was the purpose).

Please note, you are using the function Cross instead of Times when you enter x in your function definitions of FV and PV. Instead you should use * or just because a b is interpreted as a*b which again is Times[a,b]

This will ensure you get your numerical answer.

$\endgroup$
  • $\begingroup$ Sander, thanks for your reply. However, your answer does not address my question. $\endgroup$ – ramesh Oct 10 '14 at 13:22
  • $\begingroup$ Your edit makes it clearer, but I fail to understand how you want to call TimevalueFun if one variable is missing. In my answer, the missing variable is entered as {} and is subsequently returns the appropriate function? Can you explain what you are missing please, perhaps I can then update my answer to address. $\endgroup$ – Sander Oct 11 '14 at 1:46
  • $\begingroup$ @ Sander, I am not sure whether we can make the kind of function I am looking for. I just started to learn mathematica. I don't know how to assign missing argument in mathematica function. All I am trying to get help to make a function like TimeValueFun with 5 arguments. The ultimate use of the function should be as follows: TimeValueFun[i, n, pmt,pv,{}] should give me future value,TimeValueFun[{}, n, pmt,pv, fv] should give me interest rate, TimeValueFun[i, n, {},pv, fv] should give me ptm etc. Hope this helps. $\endgroup$ – ramesh Oct 11 '14 at 4:50
  • 1
    $\begingroup$ that is what I got you my friend :) did you try it? Please note you either have to evaluate your functions first, or embed them in my function. I will update my answer. $\endgroup$ – Sander Oct 11 '14 at 11:17
  • $\begingroup$ @ Sander, it seems that its working for my needs. I have to verify with more examples. I will get back to you latter. $\endgroup$ – ramesh Oct 11 '14 at 19:17
1
$\begingroup$

Assuming constant [compound] interest (r) and constant payment amounts (a), present value (pv) and future value (fv) just need 2 equations and you can solve for missing variable.

fv[a_, r_, n_] := a ((1 + r)^n - 1)/r;
pv[a_, r_, n_] := fv[a, r, n]/(1 + r)^n;

For example (noting the a will cancel), interest rate that will make future value double the present value) in 5 years:

N[r /. First@Solve[fv[a, r, 5] == 2 pv[a, r, 5], r, Reals]] 

yields: 0.148698

Or consider monthly repayment for 200000 dollar principal over 10 years with annual interest rate 6%:

a /. First[Solve[pv[a, 0.06/12, 120] == 200000., a]]

yields: 2220.41

$\endgroup$
  • $\begingroup$ Elegant simplification of the problem. $\endgroup$ – Jagra Oct 10 '14 at 13:06
  • $\begingroup$ @rka I do not understand the comment but as it is your question I am happy to delete. My point was rather than re-utilize algebraically manipulated forms of the same basic equations you can use 2 basic equations and solve for unknown with known. I declare the assumptions: compound interest, fixed interest, fixed payment...and they are consistent with q equations $\endgroup$ – ubpdqn Oct 10 '14 at 23:25
  • $\begingroup$ @ubpdqn, I understand your point. Based on your suggestion, we can compute FV or PV given A, r, and n. However, there is no direct link between PV and FV. Whereas, we want to make a function which should be able to handle all five variables at a time. Meaning, whenever we supply four arguments, we should be able to get fifth value. $\endgroup$ – ramesh Oct 11 '14 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.