# Difference Equation Model: Loan Calculations

Posted 9 years ago
4333 Views
|
4 Replies
|
3 Total Likes
|
 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= 1, x == 5000, b == 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!
4 Replies
Sort By:
Posted 9 years ago
 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:= xValue[10, 1/2, 1, {5000, 400}] Out= 42708125/128 In:= bValue[10, 1, 400] Out= 400 Or In:= N[xValue[10, 1/2, 1, {5000, 400}]] Out= 333657. You could create a table of xValues like this In:= Table[{n, N[xValue[n, 1/2, 1, {5000, 400}]]}, {n, 0, 10}] Out= {{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 9 years ago
 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.htmland athttp://reference.wolfram.com/language/guide/ActuarialComputation.html
Posted 9 years ago
 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, x[n] -> \[Lambda]^ n C + (\[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 and b might be like this RSolve[{x[1 + n] == b[1 + n] + \[Lambda] x[n], b[1 + n] == \[Alpha] b[n], x == x0, b == b0}, {x[n], b[n]}, n] which provides the explicit values for the constants C and C 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 == x0, b == 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 9 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.