the assignment is to force you to write several nested loops with an if statement inside, as well as recognize the time penalty associated with for loops in Matlab. We are going to evaluate a summation in one of two ways, either a vector operation or a for loop. The method will be randomly determined. We will then repeat this random trial many times and record how long the computer spends performing the summation for each method (vector or for loop).1. For the following sum: \[ y=\sum_{k}\cos (3\pi k) e^{−2|k|}\]
k
is given as: \[ k=\left[\begin{array}{llllllllll}−10 & −9.99 & \ldots & −3.01 & −2.99 & \ldots & 2.99 & 3.01 & \ldots & 10\end{array}\right]\]
k
starts at −10 and ends at 10, but skips over
k=3
and
k=3
. Each step increments
k
by .01. Since the logic is complex it is easier to get the inner loops functioning first, then add the outer loops to the code later. The following steps will aid you in this: Have your script generate a random number between 0 and 100. If the random number is greater than 50, evaluate the sum using a for loop. Otherwise, evaluate the sum using a vectorized method such as sum(). In both cases you are evaluating the same sum, so you will get the same answer. Adjust your script so that the random number generation and corresponding sum eval−uation is implemented 10,500:500:5000, and 10000 times. You should always get the same answer, we are just having the computer do the math over and over. Adjust your script so that the elapsed time of each sum evaluation is recorded and added. The end result of this is a variable with the total evaluation time for the for loop imple−mentation and a variable with the total evaluation time of the non loop implementation. Use the tic and toc commands to record elapsed time. Plot the time required for both the sum command and the for methods for each number of iterations. Time should be on the y−axis, and the number of iterations should be on the
x
−axis. Your result should resemble the following plot.