{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

REC06_Problems_Solutions

# REC06_Problems_Solutions - 1 14:440:127 Introduction to...

This preview shows pages 1–4. Sign up to view the full content.

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 M-file 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 M-Files, is to create a single M-file 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 doesn’t 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 don’t 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;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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. Answer: x = input(‘Which number do you want to find?’); for j = 1:length(A) if(A(j)==x) fprintf(‘%.0f is at element %.0f\n’,x,j) break end end 9. Write MatLab code that doesn’t use loops to determine approximately what point on the curve y = 15 /x + 23 - sin ( x ) from x = 1 to x = 100 is closest to the origin (0,0) . Recall that the distance to the origin is sqrt ( x 2 + y 2 ).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}