MATLAB Mid-term

MATLAB Mid-term - Fall 2010, Dr. Frank Shih CS 101 Computer...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Fall 2010, Dr. Frank Shih CS 101 Computer Programming & Problem Solving Midterm Exam (Part II: Open Book, 50 points) October 12, 2010 Name: ______________Robert_Crescenzi___________ ID#:_(Social) 153-92-6407________ 1. (10 points) Suppose that u = 1 and v = 3. Evaluate the following expressions using MATLAB: (a) 2v −2 42 πv 2 (b) (u + v) 3 Answer: >> u=1; >> v=3; >> (2*v^(-2))/(u+v)^2 ans = 0.0139 >> (4/3)*pi*v^2 ans = 37.6991 2. (20 points) Create a Matlab script file (name: “num_convert.m”) to do the following. Let a user input a number. If a number is 1, display “one”. If a number is 2, display “two”. If a number is 3, display “three”. If a number is other than 1, 2, 3, then display “something else”. You must use switch function in solving this problem. Show the Matlab code and the results of testing two cases of input: 2 and 4. Answer: n=input('Enter A Number: '); switch n case 1 disp('One'); case 2 disp('Two'); case 3 disp('Three'); otherwise disp('Something Else'); end Fall 2010, Dr. Frank Shih TEST: >> num_convert Enter A Number: 2 Two >> num_convert Enter A Number: 4 Something Else 3. (20 points) Create a Matlab script file (name: “my_plot.m”) to do the following. Let a user input a number. If a number is 1, plot y = sin(x). If a number is 2, plot y = cos(x). If a number is other than 1 and 2, then display “out of choice”. You must use x ranging from 0 to 2*pi in a step of 0.1 and use if function in solving this problem. Show the Matlab code and the result of testing the input: 1. Answer: choice=input('Enter A Number: '); x=(0:0.1:2*pi); y1=sin(x); y2=cos(x); if choice(1) plot(x,y1); elseif choice(2) plot(x,y2); else disp('Out of Choice'); end TEST: >> my_plot Enter A Number: 1 Fall 2010, Dr. Frank Shih 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 1 2 3 4 5 6 7 ...
View Full Document

Ask a homework question - tutors are online