Sunday, November 06, 2011

3 Awk Scripts For Calculating Compound Interest

Last year, I posted 2 Awk Scripts for calculating compound interest.

The "compound" script simply adds compound interest to an initial starting principle.

The second script, "compound_add", uses the principle value as a regular contribution. In other words, it adds in this amount each period and then calculates interest on the total.

I recently created a third script, called "compound_add2". This script combines the functionality of the 2 previous scripts. It allows you to add a regular contribution to an account that already has principle.

Here are some examples of how to use these scripts:

1. You open a money market account with $5,000 and earn 0.45% / month. How much is the account worth after 1 year? You would use "compound 5000 .45 12"

2. You open a money market account with $200, contribute $200 each month, and earn 0.45% / month. How much is the account worth after 1 year? You would use "compound_add 200 .45 12"

3. You have an existing money market account with $5,000. You now contribute $200 each month and earn 0.45% / month. How much is the account worth after 1 year? You would use "compound_add2 5000 200 .45 12"

Here is the compound_add2 script:

BEGIN {

if (ARGC < 5)

{

printf ("Usage: %s start add rate time\n",ARGV[0])

exit 1

}

start=ARGV[1]

add=ARGV[2]

rate=ARGV[3]/100+1

time=ARGV[4]


printf ("Start with %.2f.\n",start,rate,time)

printf ("Amount %.2f added in each time and compounded at %.4f for %d yields: \n\n",add,rate,time)

total = start

invest = start

for (i=1;i<=time; i++)

{

total = (add + total)*rate

invest += add

printf ("%d: %.2f (invested: %d)\n", i, total, invest)

}

}

0 comments: