Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Amortization Schedule--Global Symbol Value Error

28 views
Skip to first unread message

waterhand

unread,
May 20, 2012, 2:37:28 AM5/20/12
to
Let me begin by saying that I am very new to Mathematica, so any help is appreciated.

I am trying to make an amortization schedule in a table format. The code I have is below:


***

(* psv = Present Value, r = interest rate as a decimal, t = time in years, k = number of payments made *)
(* mortgage function calculates the monthly mortgage amount *)
mortgage[psv_, r_, t_] := N[psv*(r/12)/(1 - (1 + r/12)^(-12*t)), 2]

(* amountLeftOwed function calculates the amount left on the loan including interest *)
amountLeftOwed[psv_, r_, t_, k_] := N[psv*(1 + r/12)^k - mortgage[psv, r, t]*((1 + r/12)^k - 1)/(r/12), 2]

(* paidInInterest function calculates the amount of the mortgage payment that goes towards the interest *)
paidInInterest[psv_, r_, t_, k_] := N[amountLeftOwed[psv, r, t, k]*r/12, 2]

(* paidInPrincipal function calculates the amount of the mortgage payment that goes towards the principal *)
paidInPrincipal[psv_, r_, t_, k_] := N[mortgage[psv, r, t] - paidInInterest[psv, r, t, k], 2]

(* remainingBalance function gives the remaining balance after payment has been made *)
remainingBalance[psv_, r_, t_, k_] := N[amountLeftOwed[psv, r, t, k] - mortgage[psv, r, t], 2]

(* j is the number of months. this converts months to years *)
t = j/12;

Manipulate[
TableForm[
Prepend[
Table[{i, paidInInterest[psv, r, t, i], paidInPrincipal[psv, r, t, i], remainingBalance[psv, r, t, i]}, {i, 1, j}],
{"Month", "Interest", "Principal", "Balance"}]],
{{psv, 100000, "Loan Amount"}, 100, 200000, 100}, {{r, 0.05, "Interest Rate"}, 0.01, .1, 0.0005},
{{j, 120, "Number of Months"}, {120, 240, 360}}
]

***

Here's the issues I am having:

1) The variable j is coming up with the "Global symbols that have no assigned value" error despite it being assigned 120, 240, and 360 in the Manipulate[] function. When I add a line like "j = 120", the code "works". The reason for the quotations is explained in number 3 below.
2) Despite the N[*, 2] function, the numbers are coming out as scientific notation. Even with the line j = 120, the rounding is not to two places.
3) The table should end with the ending balance being zero. When I force the value of j (again, j = 120, 240, or 360), I am getting a negative value for the last two months. Is there a rounding issue related to #2?

Again, very new to Mathematica. I appreciate any help.

Thanks for reading,

Mike

Tomas Garza

unread,
May 21, 2012, 6:00:38 AM5/21/12
to
I didn't check your formulas, and I assume they are almost correct, except that the last two lines give negative values for the balance. The issue with your Mathematica code is very simple: the line before the Manipulate defines t = j/12, but this is not recognized later, when you try to use t as a variable. Delete that line (t = j/12) and use j/12 instead of tin the Manipulate. I.e.,
Manipulate[TableForm[Prepend[Table[{i,Chop@paidInInterest[psv,r,j/12,i],paidInPrincipal[psv,r,j/12,i],remainingBalance[psv,r,j/12,i]},{i,1,j}],{"Month","Interest","Principal","Balance"}]],{{psv,100000,"Loan Amount"},100,200000,100},{{r,0.05,"Interest Rate"},0.01,.1,0.0005},{{j,120,"Number of Months"},{120,240,360}}]
This works nicely - the table is now OK. However, you must check why you get negative values in the last two lines. I'm sure this doesn't come from rounding.
-Tomas

> Date: Sun, 20 May 2012 02:36:13 -0400
> From: sandwa...@gmail.com
> Subject: Amortization Schedule--Global Symbol Value Error
> To: math...@smc.vnet.net

djo...@comcast.net

unread,
May 21, 2012, 6:01:09 AM5/21/12
to
Replace t=j/12; with t[j_]:=j/12; and replace references to t inside of Manipulate to t[j].

This will get you numeric values so you can correct your issue concerning incorrect
values.

Be sure to do Clear[t] if you have executed t=j/12;

Don Orser

Bob Hanlon

unread,
May 21, 2012, 5:58:05 AM5/21/12
to
The control variable j is local to the Manipulate so it is not the
same as the j used in the external definition of t. An easy fix is to
move the definition of t inside the Manipulate. Alternatively, you
could define t as a function ( t[j_] = j/12 ) and the function calls
inside the Manipulate would use the control variable.

Labeling slider controls makes it easier to use them.

It is much more useful to have usage messages rather than comments to
document functions. With usage messages you can access your
documentation with Definition ( ? ) and Information ( ?? ) and use
"Make Template" when entering the functions.

To display numbers with two decimal places use NumberForm rather than N.

Recommend that you look at Grid for tables instead of TableForm.

As far as the final result being negative, you need to look at the
derivation of your equations. Using the default case, look at the
first month: the balance is not consistent with the payment. 413.98 +
646.67 = 1,060.65 payment of interest and principal whereas 100,000 -
98,295.36 = 1,704.64 change in balance

(*psv=Present Value,r=interest rate as a decimal,t=time in years,k=number of \
payments made*)

mortgage::usage =
"mortgage[presentValue, annualInterestRate, timeYrs] calculates the monthly \
mortgage amount.";

mortgage[psv_, r_, t_] :=
psv*(r/12)/(1 - (1 + r/12)^(-12*t))

amountLeftOwed::usage =
"amountLeftOwed[presentValue, annualInterestRate, timeYrs, \
numberPaymentsMade] calculates the amount left on the loan including \
interest.";

amountLeftOwed[psv_, r_, t_, k_] := psv*(1 + r/12)^k -
mortgage[psv, r, t]*((1 + r/12)^k - 1)/(r/12)

paidInInterest::usage =
"paidInInterest[presentValue, annualInterestRate, timeYrs, \
numberPaymentsMade] calculates the amount of the mortgage payment that goes\
towards the interest.";

paidInInterest[psv_, r_, t_, k_] :=
amountLeftOwed[psv, r, t, k]*r/12

paidInPrincipal::usage =
"paidInPrincipal[presentValue, annualInterestRate, timeYrs, \
numberPaymentsMade] calculates the amount of the mortgage payment that goes\
towards the principal.";

paidInPrincipal[psv_, r_, t_, k_] :=
mortgage[psv, r, t] - paidInInterest[psv, r, t, k]

remainingBalance::usage =
"remainingBalance[presentValue, annualInterestRate, timeYrs, \
numberPaymentsMade] gives the remaining balance after payment has been made.";

remainingBalance[psv_, r_, t_, k_] :=
amountLeftOwed[psv, r, t, k] - mortgage[psv, r, t]

nf[x_] := NumberForm[x, {16, 2}, DigitBlock -> 3];

Manipulate[
(* j is the number of months. This converts months to years *)
t = j/12;
Grid[
Prepend[
Flatten /@
Thread[
Partition[
Table[
{i, paidInInterest[psv, r, t, i] // nf,
paidInPrincipal[psv, r, t, i] // nf,
remainingBalance[psv, r, t, i] // nf},
{i, 1, j}],
j/2]],
{"Month", "Interest", "Principal", "Balance",
"Month", "Interest", "Principal", "Balance"}],
Dividers -> {{5 -> Black},
Prepend[
Table[(12 k + 2) -> Black, {k, j/24}],
2 -> Black]},
Background -> {None, {{White, LightGray}}}],
{{psv, 100000, "Loan Amount"},
100, 200000, 100, Appearance -> "Labeled"},
{{r, 0.05, "Interest Rate"},
0.01, .1, 0.0005, Appearance -> "Labeled"},
{{j, 120, "Number of Months"},
{120, 240, 360}}]

Manipulate[t = j/12;
Grid[Prepend[Flatten /@
Thread[Partition[Table[
{i, nf[paidInInterest[
psv, r, t, i]],
nf[paidInPrincipal[psv,
r, t, i]],
nf[remainingBalance[psv,
r, t, i]]}, {i, 1, j}],
j/2]], {"Month",
"Interest", "Principal",
"Balance", "Month",
"Interest", "Principal",
"Balance"}], Dividers ->
{{5 -> Black}, Prepend[
Table[12*k + 2 -> Black,
{k, j/24}], 2 -> Black]},
Background -> {None,
{{White, LightGray}}}],
{{psv, 100000, "Loan Amount"},
100, 200000, 100, Appearance ->
"Labeled"},
{{r, 0.05, "Interest Rate"},
0.01, 0.1, 0.0005,
Appearance -> "Labeled"},
{{j, 120, "Number of Months"},
{120, 240, 360}}]


Bob Hanlon


On Sun, May 20, 2012 at 2:36 AM, waterhand <sandwa...@gmail.com> wrote:
> Let me begin by saying that I am very new to Mathematica, so any help is appreciated.
>
> I am trying to make an amortization schedule in a table format. The code I have is below:
>
>
> ***
>
> (* psv = Present Value, r = interest rate as a decimal, t = time in years, k = number of payments made *)
> (* mortgage function calculates the monthly mortgage amount *)
> mortgage[psv_, r_, t_] := N[psv*(r/12)/(1 - (1 + r/12)^(-12*t)), 2]
>
> (* amountLeftOwed function calculates the amount left on the loan including interest *)
> amountLeftOwed[psv_, r_, t_, k_] := N[psv*(1 + r/12)^k - mortgage[psv, r, t]*((1 + r/12)^k - 1)/(r/12), 2]
>
> (* paidInInterest function calculates the amount of the mortgage payment that goes towards the interest *)
> paidInInterest[psv_, r_, t_, k_] := N[amountLeftOwed[psv, r, t, k]*r/12, 2]
>
> (* paidInPrincipal function calculates the amount of the mortgage payment that goes towards the principal *)
> paidInPrincipal[psv_, r_, t_, k_] := N[mortgage[psv, r, t] - paidInInterest[psv, r, t, k], 2]
>
> (* remainingBalance function gives the remaining balance after payment has been made *)
> remainingBalance[psv_, r_, t_, k_] := N[amountLeftOwed[psv, r, t, k] - mortgage[psv, r, t], 2]
>
> (* j is the number of months. this converts months to years *)
> t = j/12;
>
> Manipulate[
> TableForm[
> Prepend[
> Table[{i, paidInInterest[psv, r, t, i], paidInPrincipal[psv, r, t, i], remainingBalance[psv, r, t, i]}, {i, 1, j}],
> {"Month", "Interest", "Principal", "Balance"}]],
> {{psv, 100000, "Loan Amount"}, 100, 200000, 100}, {{r, 0.05, "InterestRate"}, 0.01, .1, 0.0005},
0 new messages