Wichita State University Logo

M451: Mathematical Computing with MATLAB©

Assignment 6: Loops



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

Write a script file called SomeSums which finds the sum of the positive integers from 1 to 100 using the following three methods:\

  1. calculating the sum using a FOR loop,

  2. calculating the sum using a WHILE loop,

  3. calculating the sum using the 'Sum of Positive Integers' formula.

    Compare your results to verify that your loops work correctly. Write a brief comment at the end of your script discussing which one is faster.


Question 2

Write a script file called MatrixEven which creates a 3 x 3 matrix $A$ of random integers between 1 and 20 using the command randi(20,[3,3]) and incorporates what you have learned from the isEven function to check whether each element of the matrix is even or not. I want you to use an inner FOR loop for the columns and an outer FOR loop for the rows when you are specifying the element index inside the matrix. The final result should be a matrix of 1's and 0's corresponding whether the element was even or not.

Now, write a brief line of code using logical indexing to accomplish the same task. Which method is easier?


Question 3: Taylor Series

Note that the trigonometric functions (and other transcendentals) are continous and take an infinite number of values. So, when early programmers wanted to program such functions, they rellied on Taylor Series approximations to evaluate these functions. In fact, there are about 30 lines of code which the programmers for the Apollo 11 mission used a Taylor Series approximation to calculate a value for sine. Recall that the Taylor Series expansion for a differentiable function $f$ centered at a number $a$ is given by

$$ f(x) = f(a) + \dfrac{f^{\prime}(a)}{1!}(x - a) + \dfrac{f^{\prime\prime}(a)}{2!}(x-a)^{2} + \cdots + \dfrac{f^{(n)}(a)}{n!}(x-a)^{n} + \cdots = \sum_{k=0}^{\infty}\dfrac{f^{(k)}(a)}{k!}(x-a)^{k} $$
The special case of the Taylor Series, called a Maclaurin Series, arises when we set $a = 0$. Rewriting our equation of the Taylor Series we get the following form for a Maclaurin Series.

$$ f(x) = \sum_{k=0}^{\infty}\dfrac{f^{(k)}(0)}{k!}x^{k} $$
Write a function called MaclaurinSine which takes in two positive numbers $x$ and $n$ and calculates the Maclaurin Series approximation for the $\sin(x)$ using $n$ terms in the sum. The output of your function should be the value given by your approximation.

Using $n = 4$, compare the results from your function and the sine function built into MATLAB© for $x = \dfrac{\pi}{32},\;\dfrac{\pi}{16},\;\dfrac{\pi}{12},\;\dfrac{\pi}{8},\;\dfrac{\pi}{4},\text{and}\;\dfrac{\pi}{2}$. What do you notice about your approximation when the value you are evaluating at is close to zero? What happens as the value of $x$ gets farther away from zero?

Now, increase the number $n$ of terms in your approximation. What happens to your approximation? How many terms do you have to take to get an "exact" solution for $\dfrac{\pi}{4}$?