Question: Whenever possible, it is better to avoid using loops, as they take quite a bit of computing power. The following problem will demonstrate this, so
Whenever possible, it is better to avoid using loops, as they take quite a bit of computing power. The following problem will demonstrate this, so recall that ctrl+c will terminate a process if it takes too long. Measure the time it takes to run a program by writing tic at the top and toc at the end. This will output the time spent to run the process. Do so for all of these problems and report the results in a commented line after each of them. a) Generate a 100 000 item vector called x from 0-10. square it and put the output in a vector called y. remember to use tic and toc (3) b) Use clear x y to clear the values from a. and perform the same operation using a for loop. Make an empty vector for both x and y first using zeroes, filling in value in the loop (6) c) Printing the output of intermediate values can be helpful for diagnosis. Using to too much will slow calculations. To demonstrate, use your program from b. but include an fprintf statement to output x and y every loop. Cut it back to be every 1000 loops. Report the time difference. Recall that ctrl+c will stop the program, as this one may take a while (6) d) Expanding the size of a vector in a for loop should be avoided if possible. To demonstrate why, redo b, but expand x and y as you go this time rather than generating an empty vector. Remember to use clear to reset these variables and to use tic and toc to get the time value.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
