Message Boards Message Boards

0
|
4606 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Difference Equation Model: Loan Calculations

Posted 10 years ago

Hi there,

I'm extremely new to Mathematica. I am trying to build a loan model wherein I use a first order difference equation to create a spreadsheet based on parameters I set in the equation. I have a few specific questions in mind...

Since I know Latex, I'm going to present my equation in that format. I'm using the following system of equations: $x_{n+1}=\lambda x_{n} + b_{n+1}, b_{n+1}=\alpha b_{n}$ is the specific system, where $\lambda=1+r$, and $0<r<1$ is an interest rate on the loan $x_{n}$ given the n^th month on the loan from month $n=0$. Moreover, $\alpha\geq 1, x_{0}=5000,b_{0}=439.79$. For the sake of a table and/or graph of data based on the solved equation (which I know to be $x_{n}=\lambda^{n}x_{0}-b_{0}\sum_{i=1}^{n}\alpha^{i}\lambda^{n-i}$. I can solve the equation perfectly fine and I understand finite difference equations and discrete dynamical systems, but I have zero clue when it comes to Mathematica's syntax, formatting, etc. Please help! This is the pathetic start on things I have right now:

Rsolve[{x[1 + n] == b[1 + n] + \[Lambda]x[n], 
  b[1 + n] == \[Alpha]b[n], \[Lambda] == 1 + r, 
  0 < r < 1, \[Alpha] >= 1, x[0] == 5000, b[0] == 438.79}, {x[1 + n],
   b[1 + n]}, 360 >= n >= 0]

I would ideally like to create something where I have knobs to adjust the parameters lambda (or r, alternatively) and alpha, with a corresponding plot that will shift in accordance to how the parameters are tweaked. Finally, I'd like to be able to create a table of values or spreadsheet for payment amount $b_{n}$ over varying alpha values. Let me know if you have tips, tricks, answers, or resources that can get me on my way!

POSTED BY: Nick Leroux
4 Replies

You might want to take a look through

http://www.wolfram.com/language/fast-introduction-for-programmers/interactive-usage/

to get a sense of how to do a variety of things in Mathematica and what the syntax is for, for example, creating functions based on patterns, plotting functions, generating tables of values from functions and so on.

To get your started, you can create functions based on the results generated by your RSolve examples using something like this

xValue[n_Integer, 
    r_, \[Alpha]_, {x0_, b0_}] /; (0 < r < 1 && \[Alpha] >= 1) := 
  1/(1 + r - \[Alpha]) ((1 + r)^n x0 + 
     r (1 + r)^n x0 - (1 + r)^n x0 \[Alpha] + 
     b0 (1 + r)^n \[Alpha] UnitStep[-1 + n] - 
     b0 \[Alpha]^(1 + n) UnitStep[-1 + n]);

bValue[n_Integer, \[Alpha]_, b0_] /; (\[Alpha] >= 1) := b0 \[Alpha]^n;

Then, for example you will get:

In[24]:= xValue[10, 1/2, 1, {5000, 400}]

Out[24]= 42708125/128

In[27]:= bValue[10, 1, 400]

Out[27]= 400

Or

In[28]:= N[xValue[10, 1/2, 1, {5000, 400}]]

Out[28]= 333657.

You could create a table of xValues like this

In[30]:= Table[{n, N[xValue[n, 1/2, 1, {5000, 400}]]}, {n, 0, 10}]

Out[30]= {{0, 5000.}, {1, 7900.}, {2, 12250.}, {3, 18775.}, {4, 
  28562.5}, {5, 43243.8}, {6, 65265.6}, {7, 98298.4}, {8, 
  147848.}, {9, 222171.}, {10, 333657.}}

And so on.... you could use Grid to format it, or TableForm if you want a quick and dirty tabular view.

POSTED BY: David Reiss
Posted 10 years ago

What if I were to try and plot these results, or list them in a table? I'm trying to create something that assembles the data in front of me, rather than something that calculates it pointwise.

P.S. Thank you so much for your responses, they've already helped tremendously.

POSTED BY: Nick Leroux

Note also that there are financial and actuarial functions contained in Mathematica which may suit your needs as well. E.g. look at some of the function listings at

http://reference.wolfram.com/language/guide/Finance.html

and at

http://reference.wolfram.com/language/guide/ActuarialComputation.html

POSTED BY: David Reiss

First thing you will need is the right syntax for RSolve (note, RSolve, not Rsolve: Mathematica is case-sensitive). Here is the general case for the code you attempted:

RSolve[{x[1 + n] == b[1 + n] + \[Lambda] x[n], 
  b[1 + n] == \[Alpha] b[n]}, {x[n], b[n]}, n]

which gives the general solution:

{b[n] -> \[Alpha]^n C[1], 
 x[n] -> \[Lambda]^
    n C[2] + (\[Alpha] (\[Alpha]^n - \[Lambda]^n) C[
     1] UnitStep[-1 + n])/(\[Alpha] - \[Lambda])}

Or another way to do it in order to have parameters for x[0] and b[0] might be like this

RSolve[{x[1 + n] == b[1 + n] + \[Lambda] x[n], 
  b[1 + n] == \[Alpha] b[n], x[0] == x0, b[0] == b0}, {x[n], b[n]}, n]

which provides the explicit values for the constants C[1] and C[2] in the earlier case in the result:

{b[n] -> b0 \[Alpha]^n, 
 x[n] -> (-x0 \[Alpha] \[Lambda]^n + x0 \[Lambda]^(1 + n) - 
   b0 \[Alpha]^(1 + n) UnitStep[-1 + n] + 
   b0 \[Alpha] \[Lambda]^n UnitStep[-1 + n])/(-\[Alpha] + \[Lambda])}

If you want this in terms of [Lambda]=1+r, then all you need to do is perform that substitution using ReplaceAll. But perhaps it'd have been easier to use [Lambda]=1+r explicitly in the original expression:

RSolve[{x[1 + n] == b[1 + n] + (1 + r) x[n], 
  b[1 + n] == \[Alpha] b[n], x[0] == x0, b[0] == b0}, {x[n], b[n]}, n]

Ok, given this, all you need to do to create an interactive solution would be to make use of Manipulate. Here is a crude example--you can use this and the above to get started learning about these things, and the very best resource is to use the Documentation Center, starting with the simplest examples and working your way up.

Manipulate[

 Grid[{{x[n], 
    N[1/(1 + 
       r - \[Alpha]) ((1 + r)^n x0 + 
        r (1 + r)^n x0 - (1 + r)^n x0 \[Alpha] + 
        b0 (1 + r)^n \[Alpha] UnitStep[-1 + n] - 
        b0 \[Alpha]^(1 + n) UnitStep[-1 + n])]},
   {b[n], N[b0 \[Alpha]^n]}},
  Alignment -> Left, Frame -> All],

 {x0, 5000, InputField},
 {b0, 400, InputField},
 {\[Alpha], 1, InputField},
 {n, 1, InputField},
 {r, 10^-10, 1 - 10^-10}

 ]
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract