gibson:teaching:fall-2012:math445:lab3

Time Estimate: 4-6 hours.

Important MATLAB commands that will be used: **for, while, if, break, format, plot, semilogy, zeros.**

Turn in your code, results, and required plots. Use the diary function
when necessary or helpful. Be sure to annotate your plots appropriately
by labeling your axes, giving the plot a meaningful title, and possibly
labeling different lines in the same plot via the `legend`

command.

Write a function `mysort.m`

that will take in a vector of any size and sort
it from least to greatest. Use the strategy of comparing neighbors and swapping
them if necessary. If this process is done throughout the entire vector
repeatedly enough times, it will work. Rewrite your code so that it does not
go through the matrix too many time using ﬁrst the commands `if, break`

and
then by switching the outer loop to use a `while`

command.

Suppose you opened a savings account that promised a 100% interest rate
(typical rates are closer to 2.25% today). We are going to try and calculate
how much money that account would have after one year depending on how the
interest is calculated. We will assume that we open the account with `$`

10,000.

If *r* is the annual interest rate, then after one year assuming the interest
is only compounded once a year, the amount after a year is just *(1 + r)B*
where *B* is the balance before the interest. With *B = $10,000* and
*r = 1.00* we have exactly *$20,000* after one year.

If the interest is compounded every six months, then after six months, the
balance would be *B2 = (1 + r/2)B* or in this case *$15,000*. After another
six months the other half of the interest is applied but this time to *B2* so
that the ending balance is given by

Similarly if the interest was compounded quarterly the ending balance would be

and the balance at the end of *j*th quarter would be given by

Before you begin, enter the command `format bank`

. What did that command do?
Compute the ﬁnal balance after one year of the initial investment of *$10,000*
at *r = 1.00* or 100% interest if the interest is compounded semi-annually,
quartely, monthly, and bi-weekly. Hint: one of your answers should be 26130.35.

Now we will compound the interest weekly. Let's use a `for`

loop to
compute not only how much money we will have in our account at the end of
the year but each week as well. Use the command
`B=zeros(53,1)`

to create a column vector of zeros where we will store the account balances. Let
`B(1)=10000`

to set the initial balance. Next we need to compute the interest after each
of the 52 weeks. We can use either of the following formulas,

or

We can use the following code to evaluate the former formula

for j=1:52 B(j+1)=(1+1.00/52)ˆj * B(1); end

Now we can plot the results using the `plot`

command: `plot(B)`

Turn in a copy of your plot. Repeat the calculation using the equation

making the appropriate adjustments to code given above. Either way you should get the exact same plot and a ﬁnal value of 26925.97 for B(53) .

**Bonus:** Can you think of a way to calculate the same vector `B`

in one line,
without using a loop?

As the number of compounds increases, the ﬁnal amount appears to be get
closer to some ﬁnal value. In order to check this, let's compound the interest
every second. Compute the result of compounding the interest every second on
`$`

10,000 at *r = 1.00* interest and check your answer versus the command
`exp(1)*10000`

How close are these two numbers? Note: the command
`exp(1)`

calculates *e^1* or just *e* where *e* is a mathematical constant.
Search `e (mathematical constant)`

in Wikipedia and look under the history
of *e*. Does anything look familiar? The `e`

is the same value used
commonly in exponential and natural logarithms.

At this point I am going to make a guess that

I am going to also guess that the approximation is better for larger values
of `n`

until for `n`

large enough, it will be essentially perfect.
Mathematically, that is to say that limit as *n* goes to of
*(1 + 1.00/n)^n* is *e*, or

In fact the above statement is the deﬁnition of e. Let's test this idea of a
limit, though. Using the model for `for`

loops above create a vector of 20
values for `n`

where with *j = 1, …, 20*. Then for each of
the values of n, again using a `for`

loop, calculate
for *j = 1, …, 20*. Plot the values of *a*.
Next plot *e − a*. You should see a graph that is not very informative
since the values quickly go to zero. Instead we will plot the graph on a log
scale on the *y*-axis by entering the command
`semilogy(exp(1)-a)`

You should now easily be able to see that as *n* increases the value of
*(1 + 1.00/n)^n* approaches *e* since *exp(1)-a* quickly goes to zero.
Turn in all plots.

**Bonus:** What happens if you let *j = 1, …, 60*? What happens if when you
let for *j = 1,…,16*? Can you make a reasonable guess as to
what's happening here?

Using the same procedure as in the previous problem, conﬁrm that

by calculating approximations and storing them in a vector *a* using the
same values for *n* and *j* as used above. Plot *e^0.754 − a* on a log
scale in *y*. Turn in your plot. Note: *e^0.754* is computed in MATLAB by
the command `exp(0.754)`

.

We have used several MATLAB functions so far. Now we are going to write our own function. In the main MATLAB window click File → New → Script (or File → New → m-ﬁle depending on your version of MATLAB). This will open a ﬁle editor. In the ﬁrst line of the editor put the line

`function Bal=InterestCalc(Investment,Rate,Yrs,Ncomp)`

Hit Enter several times and then type

`end`

Any code that is between the `function`

command and the `end`

will execute
when you run the function. Somewhere in that function we need to deﬁne the
value for the variable `Bal`

which will be returned by the function.
Mathematically this is what the code needs to return

where `Bal`

will be the balance you would have after `Yrs`

years if you
invested `Investment`

amount of money at an interest rate of `Rate`

that
is compounded `Ncomp`

times per year. You need to use result previously
given to ﬁgure out exactly what function you need here. Save this function
as `InterestCalc.m`

in a directory for this class. Change the directory
you are working in by selecting the same directory in the main MATLAB command
window. You should now be able to test your function by typing

`InterestCalc(1000,0.05,5,60)`

which should return the value 1283.36.

Note: if you have more output line in the command window than just the one
number, add `;`

to the end of each line of your function except the ﬁrst and
the last line. Turn in the code for your function.

Enter the command `format long`

then verify the following properties of
exponentials and logarithms by testing the appropriate MATLAB functions with
the parameters indicated:

Note the `log`

function in matlab is the natural logarithm. How would you
calculate , , or ?

gibson/teaching/fall-2012/math445/lab3.txt · Last modified: 2012/12/05 09:11 by gibson