<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Programming with M-Files: A Personal Finance Example Using While Loops</name>
  <metadata>
  <md:version>1.4</md:version>
  <md:created>2006/02/27 15:34:26 US/Central</md:created>
  <md:revised>2006/08/22 13:19:50.780 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Credit Card Debt</md:keyword>
    <md:keyword>M-file</md:keyword>
    <md:keyword>MathScript</md:keyword>
    <md:keyword>MATLAB</md:keyword>
    <md:keyword>Octave</md:keyword>
    <md:keyword>Personal Finance</md:keyword>
    <md:keyword>While Loop</md:keyword>
  </md:keywordlist>

  <md:abstract>This is an example of using an m-file script to compute the number of months necessary to pay off a credit card debt using the minimum monthly payment.</md:abstract>
</metadata>
  <content>
    <section id="S1">
    <name>A Personal Finance Example</name>

    <para id="xyzzy1">
      A student decides to finance their college education using a credit card. They charge one semester's tuition and then make the minimum monthly payment until the credit card balance is zero. How many months will it take to pay off the semester's tuition? How much will the student have spent to pay off the tuition?
    </para>
    <para id="xyzzy2">
      We can solve this problem using an m-file script. We define the following variables:
      <list id="vars" type="bulleted">
  <item>
    <m:math>
    <m:ci>
      <m:msub>
        <m:mi>b</m:mi>
        <m:mi>n</m:mi>
      </m:msub>
    </m:ci>
  </m:math> - Balance at month <m:math><m:ci>n</m:ci></m:math>.
  </item>
  <item>
      <m:math>
    <m:ci>
      <m:msub>
        <m:mi>p</m:mi>
        <m:mi>n</m:mi>
      </m:msub>
    </m:ci>
  </m:math> - Payment in month  <m:math><m:ci>n</m:ci></m:math>.
  </item>
  <item>
      <m:math>
    <m:ci>
      <m:msub>
        <m:mi>f</m:mi>
        <m:mi>n</m:mi>
      </m:msub>
    </m:ci>
  </m:math> - Finance charge (interest) in month  <m:math><m:ci>n</m:ci></m:math>.</item>
</list>
    </para>
    <para id="xyzzy3">
      The <emphasis>finance charge</emphasis> <m:math>
    <m:ci>
      <m:msub>
        <m:mi>f</m:mi>
        <m:mi>n</m:mi>
      </m:msub>
    </m:ci>
  </m:math>is the interest that is paid on the balance each month. The finance charge is computed using the monthly interest rate <m:math> <m:ci>r</m:ci></m:math>:
<equation id="eq2">
  <m:math>
    <m:apply>
      <m:eq/>
      <m:ci>
	<m:msub>
	  <m:mi>f</m:mi>
	  <m:mi>n</m:mi>
	</m:msub>
      </m:ci>
      <m:apply>
	<m:times/>
	<m:ci>r</m:ci>
	<m:ci>
	  <m:msub>
	    <m:mi>b</m:mi>
	    <m:mi>n</m:mi>
	  </m:msub>
	</m:ci>
      </m:apply>
    </m:apply>
  </m:math>
</equation>
Credit card interest rates are typically given as an annual percentage rate (APR). To convert the APR to a monthly interest rate, use the following formula:
<equation id="eq3">
  <m:math>
    <m:apply>
      <m:eq/>
      <m:ci>r</m:ci>
      <m:apply>
	<m:minus/>
	<m:apply>
	  <m:power/>
	  <m:apply>
	    <m:plus/>
	    <m:cn>1</m:cn>
	    <m:apply>
	      <m:divide/>
	      <m:ci>APR</m:ci>
	      <m:cn>100</m:cn>
	    </m:apply>
	  </m:apply>
	  <m:apply>
	    <m:divide/>
	    <m:cn>1</m:cn>
	    <m:cn>12</m:cn>
	  </m:apply>
	</m:apply>
	<m:cn>1</m:cn>
      </m:apply>
    </m:apply>
  </m:math>
</equation>
More information on how to compute monthly rates can be found <link src="http://en.wikipedia.org/wiki/Credit_card_interest">here</link>.
    </para>
    <para id="xyzzy4">
      Credit cards usually have a <emphasis>minimum monthly payment</emphasis>. The minimum monthly payment is usually a fixed percentage of the balance; the percentage is required by federal regulations to be at least 1% higher than the monthly interest rate. If this minimum payment would be below a given threshold (usually $10 to $20), the minimum payment is instead set to the threshold. For a threshold of $10, the relationship between the balance and the minimum payment can be shown in an equation as follows:
<equation id="eq4">
  <m:math>
    <m:apply>
      <m:eq/>
      <m:ci>
	<m:msub>
	  <m:mi>p</m:mi>
	  <m:mi>n</m:mi>
	</m:msub>
      </m:ci>
      <m:apply>
	<m:ci type="fn">max</m:ci>
      <m:apply>
	<m:times/>
	<m:apply>
	  <m:plus/>
	  <m:ci>r</m:ci>
	  <m:cn>0.01</m:cn>
	</m:apply>
	<m:ci>
	  <m:msub>
	    <m:mi>b</m:mi>
	    <m:mi>n</m:mi>
	  </m:msub>
	</m:ci>
      </m:apply>
      <m:cn>10</m:cn>
      </m:apply>
    </m:apply>
  </m:math>
</equation>
    </para>
    
    <para id="xyzzy99">
To compute the balance for one month (month <m:math><m:apply><m:plus/> <m:ci>n</m:ci><m:cn>1</m:cn></m:apply></m:math>) from the balance for the previous month (month <m:math> <m:ci>n</m:ci></m:math>), we compute the finance charge on the balance in the previous month and add it to the previous balance, then subtract the payment for the previous month:
<equation id="eq1">
  <m:math>
    <m:apply>
      <m:eq/>
      <m:ci>
	<m:msub>
	  <m:mi>b</m:mi>
<m:mrow>
   <m:mi>n</m:mi>
   <m:mo>+</m:mo>
   <m:mn>1</m:mn>
</m:mrow>
	</m:msub>
      </m:ci>
      <m:apply>
	<m:plus/>
	<m:ci>
	  <m:msub>
	    <m:mi>b</m:mi>
	    <m:mi>n</m:mi>
	  </m:msub>
	</m:ci>
	<m:apply>
	  <m:minus/>
	  <m:ci>
	    <m:msub>
	      <m:mi>f</m:mi>
	      <m:mi>n</m:mi>
	    </m:msub>
	  </m:ci>
	  <m:ci>
	    <m:msub>
	      <m:mi>p</m:mi>
	      <m:mi>n</m:mi>
	    </m:msub>
	  </m:ci>
	</m:apply>
      </m:apply>
    </m:apply>
  </m:math>
</equation>
    </para>
    <para id="xyzzy100">
In the following exercises, we will develop the program to compute the number of months necessary to pay the debt. We will assume that the card APR is 14.9% (the <link src="http://money.cnn.com/pf/informa/index.html">average rate on a student credit card</link> in mid February 2006) and that the initial balance charged to the card is $2203 (the <link src="http://www.asu.edu/sbs/FallUndergradEastWest.htm">in-state tuition at Arizona State University at the Polytechnic Campus  for Spring 2006 semester</link>).
    </para>
    <exercise id="E1">
      <problem id="P1">
        <para id="P1a">
          Write code to compute the monthly interest rate <m:math> <m:ci>r</m:ci></m:math> from the APR using <cnxn target="eq3"/>.
        </para>
      </problem>
      <solution>
        <para id="S11"><code type="block">
APR = 14.9;
r = (1+APR/100)^(1/12)-1;
</code></para>
      </solution>
    </exercise>

    <exercise id="E2">
      <problem id="P2">
        <para id="P2a">
          Write code to compute the minimum monthly payment <m:math>
    <m:ci>
      <m:msub>
        <m:mi>p</m:mi>
        <m:mi>n</m:mi>
      </m:msub>
    </m:ci>
  </m:math> using <cnxn target="eq4"/>.
        </para>
      </problem>
      <solution>
        <para id="S2"><code type="block">bn = 2203;
pn = max((r+0.01)*bn,10);
</code></para>
      </solution>
    </exercise>

    <exercise id="E3">
      <problem id="P3">
        <para id="P3a">
          Write code to compute the  balance at month <m:math><m:apply><m:plus/> <m:ci>n</m:ci><m:cn>1</m:cn></m:apply></m:math> in terms of the balance at month <m:math> <m:ci>n</m:ci></m:math>  using <cnxn target="eq1"/>.
        </para>
      </problem>
      <solution>
        <para id="S3"><code type="block">
fn = r*bn;
bn = bn + fn - pn;
</code></para>
      </solution>
    </exercise>

    <exercise id="E4">
      <problem id="P4">
        <para id="P4a">
	  Place the code developed for <cnxn target="E3"/> into a while loop to determine how many months will be required to pay off the card.
	</para>
      </problem>
      <solution>
	<para id="S4">
	  This space intentionally left blank.
	</para>
      </solution>
    </exercise>

    <exercise id="E5">
      <problem id="P5">
	<para id="P5a">
	  Modify your code from  <cnxn target="E4"/> to plot the monthly balance, monthly payment, and total cost-to-date for each month until the card is paid off.
	</para>
      </problem>
      <solution>
	<para id="S5">
	  This space intentionally left blank.
	</para>
      </solution>
    </exercise>


  </section>

</content>
  
</document>
