====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:fall-2012:math445:lab3 [2012/09/05 19:38] gibson |
gibson:teaching:fall-2012:math445:lab3 [2012/12/05 09:11] (current) gibson [Problem F] |
||
---|---|---|---|
Line 10: | Line 10: | ||
labeling different lines in the same plot via the ''legend'' command. | labeling different lines in the same plot via the ''legend'' command. | ||
- | A. Write a function ''mysort.m'' that will take in a vector of any size and sort | + | ==== 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 | 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 | them if necessary. If this process is done throughout the entire vector | ||
Line 17: | Line 18: | ||
then by switching the outer loop to use a ''while'' command. | then by switching the outer loop to use a ''while'' command. | ||
- | B. Suppose you opened a savings account that promised a 100% interest rate | + | ==== 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 | (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 | how much money that account would have after one year depending on how the | ||
Line 32: | Line 34: | ||
six months the other half of the interest is applied but this time to //B2// so | six months the other half of the interest is applied but this time to //B2// so | ||
that the ending balance is given by | that the ending balance is given by | ||
+ | |||
<latex> | <latex> | ||
(1 + r/2)B2 = (1 + r/2)(1 + r/2)B = ((1 + r/2)^2 )B. | (1 + r/2)B2 = (1 + r/2)(1 + r/2)B = ((1 + r/2)^2 )B. | ||
</latex> | </latex> | ||
+ | |||
Similarly if the interest was compounded quarterly the ending balance would be | Similarly if the interest was compounded quarterly the ending balance would be | ||
+ | |||
<latex> | <latex> | ||
((1 + r/4)^4 )B, | ((1 + r/4)^4 )B, | ||
</latex> | </latex> | ||
- | and the balance at the end of //j//th quarter would be give by | + | |
+ | and the balance at the end of //j//th quarter would be given by | ||
<latex> | <latex> | ||
((1 + r/4)^j )B. | ((1 + r/4)^j )B. | ||
- | <latex> | + | </latex> |
Before you begin, enter the command ''format bank''. What did that command do? | Before you begin, enter the command ''format bank''. What did that command do? | ||
Compute the final balance after one year of the initial investment of //$10,000// | Compute the final balance after one year of the initial investment of //$10,000// | ||
Line 48: | Line 56: | ||
quartely, monthly, and bi-weekly. Hint: one of your answers should be 26130.35. | quartely, monthly, and bi-weekly. Hint: one of your answers should be 26130.35. | ||
- | C. Now we will compound the interest weekly. Let's use a ''for'' loop to | + | |
+ | ==== Problem C ==== | ||
+ | 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 | 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 | the year but each week as well. Use the command | ||
''B=zeros(53,1)'' | ''B=zeros(53,1)'' | ||
- | to create a column vector of zeros values where we will store the account | + | to create a column vector of zeros where we will store the account balances. Let |
- | balances. Let | + | |
''B(1)=10000'' | ''B(1)=10000'' | ||
to set the initial balance. Next we need to compute the interest after each | 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, | of the 52 weeks. We can use either of the following formulas, | ||
+ | |||
<latex> | <latex> | ||
B_{j+1} = (1 + r/52)^j B_1 | B_{j+1} = (1 + r/52)^j B_1 | ||
</latex> | </latex> | ||
+ | |||
or | or | ||
+ | |||
<latex> | <latex> | ||
B_{j+1} = (1 + r/52) B_j | B_{j+1} = (1 + r/52) B_j | ||
Line 66: | Line 78: | ||
We can use the following code to evaluate the former formula | We can use the following code to evaluate the former formula | ||
+ | |||
<code> | <code> | ||
for j=1:52 | for j=1:52 | ||
B(j+1)=(1+1.00/52)ˆj * B(1); | B(j+1)=(1+1.00/52)ˆj * B(1); | ||
end | end | ||
- | <code> | + | </code> |
- | Now we can plot the results using the ''plot'' command: | + | |
- | ''plot(B)'' | + | 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 | Turn in a copy of your plot. Repeat the calculation using the equation | ||
+ | |||
<latex> | <latex> | ||
B_{j+1} = (1 + r/52) B_j . | B_{j+1} = (1 + r/52) B_j . | ||
</latex> | </latex> | ||
+ | |||
making the appropriate adjustments to code given above. Either way you should | making the appropriate adjustments to code given above. Either way you should | ||
- | get the exact same plot and a final value of 26925.97 for B53 . | + | get the exact same plot and a final value of 26925.97 for B(53) . |
- | Bonus: Can you think of a way to calculate the same vector ''B'' in one line, | + | **Bonus:** Can you think of a way to calculate the same vector ''B'' in one line, |
without using a loop? | without using a loop? | ||
- | D. As the number of compounds increases, the final amount appears to be get | + | ==== Problem D ==== |
- | closer to some final value. In order to check this, lets compound the interest | + | As the number of compounds increases, the final amount appears to be get |
+ | closer to some final value. In order to check this, let's compound the interest | ||
every second. Compute the result of compounding the interest every second on | 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 | + | ''$''10,000 at //r = 1.00// interest and check your answer versus the command |
''exp(1)*10000'' | ''exp(1)*10000'' | ||
How close are these two numbers? Note: the command | How close are these two numbers? Note: the command | ||
Line 95: | Line 111: | ||
commonly in exponential and natural logarithms. | commonly in exponential and natural logarithms. | ||
- | E. At this point I am going to make a guess that | + | ====Problem E==== |
+ | At this point I am going to make a guess that | ||
<latex> | <latex> | ||
- | (1 + 1.00/n)^n ≈ e. | + | (1 + 1.00/n)^n \approx e. |
</latex> | </latex> | ||
Line 104: | Line 122: | ||
Mathematically, that is to say that limit as //n// goes to $\infty$ of | Mathematically, that is to say that limit as //n// goes to $\infty$ of | ||
//(1 + 1.00/n)^n// is //e//, or | //(1 + 1.00/n)^n// is //e//, or | ||
+ | |||
<latex> | <latex> | ||
\lim_{n \rightarrow \infty} (1 + 1.00/n)^n = e. | \lim_{n \rightarrow \infty} (1 + 1.00/n)^n = e. | ||
</latex> | </latex> | ||
- | In fact the above statement is the definition of e. Lets test this idea of a | + | In fact the above statement is the definition of e. Let's test this idea of a |
- | limit though. Using the model for ''for'' loops above create a vector of 20 | + | limit, though. Using the model for ''for'' loops above create a vector of 20 |
values for ''n'' where $n_j = 2^j$ with //j = 1, ..., 20//. Then for each of | values for ''n'' where $n_j = 2^j$ with //j = 1, ..., 20//. Then for each of | ||
the values of n, again using a ''for'' loop, calculate | the values of n, again using a ''for'' loop, calculate | ||
- | $a_j = (1 + 1.00/n_j )^{n_j}$ for ''j = 1, ..., 20''. Plot the values of //a//. | + | $a_j = (1 + 1.00/n_j )^{n_j}$ for //j = 1, ..., 20//. Plot the values of //a//. |
Next plot //e − a//. You should see a graph that is not very informative | 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 | since the values quickly go to zero. Instead we will plot the graph on a log | ||
Line 121: | Line 140: | ||
Turn in all plots. | Turn in all plots. | ||
- | Bonus: What happens if you let ''j = 1, ..., 60''? What happens if when you | + | **Bonus:** What happens if you let //j = 1, ..., 60//? What happens if when you |
- | let $n_j = 10^j$ for ''j = 1,...,16''? Can you make a reasonable guess as to | + | let $n_j = 10^j$ for //j = 1,...,16//? Can you make a reasonable guess as to |
what's happening here? | what's happening here? | ||
- | F. Using the same procedure as in the previous problem, confirm that | + | ==== Problem F ==== |
+ | Using the same procedure as in the previous problem, confirm that | ||
<latex> | <latex> | ||
- | \lim_{n\rightarrow \infty} (1 + 0.754/n)^nn = e^{0.754} | + | \lim_{n\rightarrow \infty} (1 + 0.754/n)^n= e^{0.754} |
</latex> | </latex> | ||
+ | |||
by calculating approximations and storing them in a vector //a// using the | 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 | same values for //n// and //j// as used above. Plot //e^0.754 − a// on a log | ||
Line 134: | Line 156: | ||
the command ''exp(0.754)''. | the command ''exp(0.754)''. | ||
- | G. We have used several MATLAB functions so far. Now we are going to write our | + | ==== 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 | own function. In the main MATLAB window click File → New → Script | ||
(or File → New → m-file depending on your version of MATLAB). This will open a | (or File → New → m-file depending on your version of MATLAB). This will open a | ||
Line 149: | Line 173: | ||
value for the variable ''Bal'' which will be returned by the function. | value for the variable ''Bal'' which will be returned by the function. | ||
Mathematically this is what the code needs to return | Mathematically this is what the code needs to return | ||
+ | |||
<latex> | <latex> | ||
Bal = (1 + Rate ∗ Yrs/Ncomp)^{Ncomp} Investment | Bal = (1 + Rate ∗ Yrs/Ncomp)^{Ncomp} Investment | ||
</latex> | </latex> | ||
+ | |||
where ''Bal'' will be the balance you would have after ''Yrs'' years if you | 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 | invested ''Investment'' amount of money at an interest rate of ''Rate'' that | ||
Line 168: | Line 194: | ||
the last line. Turn in the code for your function. | the last line. Turn in the code for your function. | ||
- | H. Enter the command ''format long'' then verify the following properties of | + | ==== Problem H ==== |
+ | |||
+ | Enter the command ''format long'' then verify the following properties of | ||
exponentials and logarithms by testing the appropriate MATLAB functions with | exponentials and logarithms by testing the appropriate MATLAB functions with | ||
the parameters indicated: | the parameters indicated: | ||
+ | |||
<latex> | <latex> | ||
e^a e^b = e^{a+b} \text{ for } a= 0.3, b= 0.4, | e^a e^b = e^{a+b} \text{ for } a= 0.3, b= 0.4, | ||
Line 180: | Line 209: | ||
<latex> | <latex> | ||
- | log(ab) = log(a) + log(b), \text{ for } a = 0.1, b = 5 | + | \log(ab) = \log(a) + \log(b), \text{ for } a = 0.1, b = 5 |
</latex> | </latex> | ||
<latex> | <latex> | ||
- | log(a^b) = b log(a), \text{ for } a = 3, b = 3 | + | \log(a^b) = b \log(a), \text{ for } a = 3, b = 3 |
</latex> | </latex> | ||
Note the ''log'' function in matlab is the natural logarithm. How would you | Note the ''log'' function in matlab is the natural logarithm. How would you | ||
- | calculate $\log_{10}$ , $log_2$ , or $log_5$? | + | calculate $\log_{10}$ , $\log_2$ , or $\log_5$? |