This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 14:440:127 Introduction to Computers for Engineers Recitation 06 Problems Solutions Pertaining to LEC06 Notes Rutgers University, Spring 2011 Instructor  Brenda V. Cortez INSTRUCTIONS: If you want, you can create a separate Mfile for each of the problems in your recitation problem sets, so that you can reference back to your answers later when you study. Save all your work to your MATLAB Files directory in the J: Drive. An alternative to separate MFiles, is to create a single Mfile for all the problems, and use %% to create a cell for each problem. Refer to my Video Tutorial on the topic of program cells found in Video Tutorials > LEC04 Video Tutorials . For all problems, always think about using good variable names, even if the problem doesnt explicitly specify them; get used to defining your own. Make sure you go through all the problems before the exam and ask any questions if you dont understand why something works the way it does. I suggest that you write simple pseudocode stating the algorithm you will use to solve each problem, before you start coding it. This will save you making simple errors. PROBLEMS: 1. Vectorize the following code using an arbitrary value for NUM . t = 0; for k=1:NUM t = t + k; end Answer: total = sum(1:NUM); 2. Write a for loop that prints out the decimal equivalent of the reciprocals: 1/2, 1/3, 1/4, ... , 1/19, 1/20 . Then, write this without using a loop, vectorize , and use tic/toc to see which is faster. Answer: tic; for x = 1:20 disp(1/x) end toc; 2 tic x = 1./(1:20) toc 3. Vectorize the following code using an arbitrary value for something . p = 1; for k=1:something p = p * k; end disp(p) Answer: num = factorial(something); 4. Find the sum of the geometric series: 1 + 2^1 + 2^2 +...+ 2^10 using a for loop. Then, rewrite this without using a loop, vectorize , and use tic/toc to see which is faster. Answer: tic total = 0; for x = 0:10 total = total+2^x; end disp(total) toc tic x = 2.^(0:10); disp(sum(x)) toc 5. Vectorize the following code using an arbitrary value for ending . g = 0; for k=1:length(ending) g = g + ending(k); end disp(g) Answer: disp(sum(ending)); 3 6. Display the values of x 3 for all positive integer values x such that x 2 is less than 2000 . Answer: x = 1; while(x^2<2000) disp(x^3) x = x+1; end 7. Vectorize the following code using an arbitrary value for ending . startP = 1; vp = 1; for k=startP:ending vp = vp * k; end disp(vp) Answer: disp(prod(startP:ending)); 8. Sequential Search Algorithm: Given a vector A of arbitrary length, write a simple code to search for the 1st occurrence of a particular number a user wants to find. Your code does not need to do anything if the number is not found. Hint: remember the use of BREAK....
View
Full
Document
This note was uploaded on 03/30/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.
 Spring '08
 Finch

Click to edit the document details