AM 147: Computational Methods and Applications: Winter 2021 Homework #2
Instructor: Abhishek Halder Due: January 19, 2021
NOTE: Please submit your Homework as a single zip file named YourlastnameYourfirstnameHW2.zip via CANVAS. For example, HalderAbhishekHW2.zip. Please strictly follow the capital and small letters in the filename of the zip file you submit. You may not receive full credit if you do not follow the file-naming conventions. Your zip file should contain all .m files (MATLAB scripts) for the questions below.
Your zip file must be uploaded to CANVAS by 11:59 PM Pacific Time on the due date. The uploads in CANVAS are time-stamped, so please don’t wait till last moment. Late homework will not be accepted.
Problem 1 Numerical errors
For real x, the n-term Taylor series approximation of exp(x) is 10-term Taylor series approximation of exp(−5) in two different ways:
!9 (−5)j (i) exp(−5) ≈ j=0 j! ,
(ii) exp(−5) = 1 ≈ 1 . exp(5) !9 (5)j
j=0 j!
Typing exp(-5) in MATLAB command prompt (with default double precision short format) returns 0.0067. Let us call this as “true value”. Submit a MATLAB script (.m file) named
YourlastnameYourfirstnameHW2p1.m that computes the relative errors in using formula (i) and formula (ii). For your script, you may find MATLAB in-built command factorial useful.
n−1 ! xj
(20 points) j! . Consider computing the
j=0
1
Problem 2
Which positive integer generates the longest sequence (20 + 10 = 30 points) Take any positive integer n, and perform the following operation recursively to generate a se- quence until you end up with 1.
“#$n, if n is even,
#2
f(n)=%3n+1, ifnisodd.
Here are some examples.
Starting with 1,
Starting with 2,
Startingwith3, thesequenceis 3→10→5→16→8→4→2→1. Starting with 4, the sequence is 4 → 2 → 1.
It is known that starting from any positive integer ≤ 268, we are guaranteed to end up with 1. We are interested in the number of steps needed to reach 1. In the examples above, starting
with 1, we need 0 steps to reach 1. Starting with 2, we need 1 step to reach 1. Starting with 3, we need 7 steps to reach 1. Starting with 4, we need 2 steps to reach 1.
Write a MATLAB script YourlastnameYourfirstnameHW2p2.m that takes a positive integer
n as input, and outputs the “starting with positive integer” in the interval [1, n] for which the number of steps is the largest.
For example, if the input to your code is n = 4, then the output of your code should be 3. This is because 3 is the positive integer in the interval [1, 4] for which we get the maximum number of steps (7). Starting with any other integer in [1,4], the number 1 is reached in less than 7 steps.
Use your code to output the “starting with positive integer” in the interval [1, 500] for which
the number of steps is the largest. Also compute the corresponding maximum number of steps.
Hint: look up the command max in MATLAB documentation.
the sequence is 1.
the sequence is 2 → 1.
2