the assignment is to force you to write several nested loops with an if statement inside, as
Fantastic news! We've Found the answer you've been seeking!
Question:
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 For the following sum: ysumkcos pi k ek
k
is given as: kleftbeginarrayllllllllll & & ldots & & & ldots & & & ldots & endarrayright
k
starts at and ends at but skips over
k
and
k
Each step increments
k
by 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 and If the random number is greater than 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 evaluation is implemented :: and 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 implementation 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 yaxis, and the number of iterations should be on the
x
axis. Your result should resemble the following plot.
Related Book For
Legal Research Analysis and Writing
ISBN: 978-1305948372
4th edition
Authors: William H. Putman, Jennifer Albright
Posted Date: