REC04_Problems_Solutions

REC04_Problems_Solutions - 1 14:440:127 Introduction to...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 14:440:127– Introduction to Computers for Engineers Recitation 04 Problems Solutions - Pertaining to LEC04 Notes Rutgers University, Spring 2011 Instructor - Brenda V. Cortez INSTRUCTIONS: 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 cells found in Video Tutorials > LEC04 Video Tutorials . For all problems, always thing what a good variable name could be used and even if the problem doesn’t explicitly specify variable names, 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. PROBLEMS: 1. Create a variable, age , and store your age in it. Subtract one from the value of the variable. Add two to the value of the variable. If age has the value 18 , what is the value stored in age after executing the statements you just created? Answer: age = 18; age = age - 1; age = age + 2; age = 19 at the end, when age initially stores 18 2. Use MatLab to find the sum of 5, 2 squared, and 4. Assign this value to variable x . Answer: x = 5+2^2+4 3. Run the following two commands: 1) x = 56; 2) y = 87; Now, switch the values of x and y as was done in LEC04 Notes. Answer: x = 56; y = 87; temp = x; x = y; y = temp;
Image of page 1

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

View Full Document Right Arrow Icon
2 4. Write the code above in an M-file and save it as firstMLfile.m on your J: drive, in the MATLAB Files Directory. Make sure to add the J: drive to MatLab’s path. Edit the code above to display the values of x , y , before and after they are swapped. Then run your file by typing firstMLfile in the command window. Answer: x = 56; y = 87; disp(x) disp(y) temp = x; x = y; y = temp; disp(x) disp(y) 5. On a piece of paper, predict the outcome of the following MatLab calculations, and then test your predictions in MatLab: 5^2*3 5^(2*3) (1+3+5)/(5+3+1) Answer: % respectively: 75 15625 1 6. Use MatLab to calculate the square root of 2+3^4 , without using the function sqrt first, and then using the sqrt function. Answer: % Recall that a square root is a 1/2 power. (2+3^4) ^ (1/2) % or (2+3^4) ^ (0.5) % or sqrt(2+3^4) 7. Create a variable r , set it equal to 10 . Calculate the area and circumference of a circle with radius r. Answer: r = 10; circumference = pi*(2*r); area = pi*r^2;
Image of page 2
3 8. For all of the following problems, set x = 15 , y = 22 , and z = 35 . First, on a piece of paper, predict the results by hand. Then, test your predictions on MatLab: (a) x>22 & y>22 Answer: false ANDalse false 0 (b) x>22 | y>22 Answer: false OR false false 0 (c) ~0 Answer: NOT false true 1 (d) ~z Answer: (recall that non-zero is true) NOT true false 0 (e) x + y > 37 Answer: 37>37 false 0 (f) x + ( y > 37 ) Answer: x + (false) 15 + (0) 15 (g) x - y | 0 Answer: (recall that non-zero is true) 15 - 22 | 0 -7 | 0 true | false true 1 (h) y>x & z>y & z>34 Answer: 22>15 & 35>22 & 35>34 true & true & true
Image of page 3

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

View Full Document Right Arrow Icon
4 true & true true 1 (i) x>3 & x<5 Answer:
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern