# channelflow.org

### Site Tools

gibson:teaching:fall-2012:math445:lab3

## Math 445 Lab 3: Sorting, Interest, Exponentials, and Limits

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.

### Problem A

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.

### Problem B

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 jth quarter would be given by

### Problem E

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?

### Problem F

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).

### Problem G

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.

### Problem H

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 ?