Wichita State University Logo

M451: Mathematical Computing with MATLAB©

Assignment 9: Midterm Exam Modifications



Instructions:

Submit your work in the form of a multiple .m files. Please put your first initial and last initial (in lowercase letters) before each script or function name. If you are submitting a script file, start your file off with the command clear and please clearly label and describe each problem with comments in your file.

Question 1: Power Sine

Rewrite the powersin function from your midterm so that the argument is always evaluated on the interval $-\pi \leq x \leq \pi$. For your convenience I have included the code for powersin below.

function s = powersin(x) % POWERSIN. Power series for sin(x). % POWERSIN(x) tries to compute the sin(x) from a power series. s = 0; t = x; n = 1; while s+t ~= s s = s + t; t = -x.^2/((n+1)*(n+2)).*t; n = n + 2; end

Write a new script called PowerSinTestRedux that uses your new code and the values $x = \pm\dfrac{\pi}{2}, \pm\dfrac{3\pi}{2}, \pm\dfrac{5\pi}{2}, \pm\dfrac{7\pi}{2}, \pm\dfrac{9\pi}{2}$, to answer the following questions:

i. How accurate is the computed result?    

ii. How many terms are required?  

iii. What is the largest term in the series?   

and displays the results in the format of the following table.  

$x$ error Number of Terms Largest Term
$-\dfrac{9\pi}{2}$
$-\dfrac{7\pi}{2}$
$-\dfrac{5\pi}{2}$
$-\dfrac{3\pi}{2}$
$-\dfrac{\pi}{2}$
$\dfrac{\pi}{2}$
$\dfrac{3\pi}{2}$
$\dfrac{5\pi}{2}$
$\dfrac{7\pi}{2}$
$\dfrac{9\pi}{2}$


How do these results compare to the results you found during your exam? Is the refined code better? If so, why? If not, why?



Question 2: Power Cosine

The power series for $\cos(x)$ is given by

$$ \sin(x) = 1 - \dfrac{x^2}{2!} + \dfrac{x^{4}}{4!}-\dfrac{x^{6}}{6!}+\cdots $$
Using your rewritten powersin as a template, write a version called powercos which uses the series to evalute $\cos(x)$. You should impose the same restriction on the argument as in Question 1.

Write a script PowerCosineTest where you test your new function with some test values of your choice and compare your results with results form the built-in cosine function in MATLAB©.

Is your new function reasonably accurate? Are there any other modifications you would like to make to make to these functions? What would be the next logical code to make?



Question 3: Shuffle

During the midterm, I asked you to take a given vector $\mathbf{deck} = 1:52$, and write a basic script file called BasicShuffle that uses logical indexing to shuffle the numbers in $\mathbf{deck}$ as though they are a deck of cards. I then asked you to make make an outline to make this code more realistic. Write a script called Shuffle which implements your plan to make a more realistic shuffle. Compare your shuffle with a similar shuffle using the randperm command.

How many shuffles would you have to do to take a perfectly ordered deck and reshuffle it until it became truly random? One crude measure of randomness is the (1,2) element of corrcoef(1:52,v), which is expected to be zero if $\mathbf{v}$ is random.