14-440-127-Fall2010+REC02+Solutions

# 14-440-127-Fall2010+REC02+Solutions - Introduction to...

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

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.

Unformatted text preview: Introduction to Computers for Engineers Recitation 2 - Problems Solutions 1) Write Matlab code that displays “You must go to study right away” if the variable TVTime equals 1. if(TVtime==1) disp('You must go to bed!') end 2) Create a row vector containing the even numbers 2 through 18, a column vector containing 1,5,9,…,25,29, and a 3*3 matrix A full of zeros . v1 = 2:2:18; v2 = transpose(1:4:29); A = zeros(3); 3) Create a 3 x 4 matrix in which all the elements have a value of pi. Then, change all elements in the 2nd row to-pi. Finally, display the sine of each value in the matrix. z = ones(3,4) * pi; [r c] = size(z); %% this is the more general way of doing this problem: z(2,:) = -pi * ones(1,c); sin(z) 4) Evaluate the following Matlab Expressions by hand. Verify in Matlab: a. 2 + round(6/9 + 3*2)/2 - 3 2+round(6.6666)/2 – 3 2+7/2 – 3 2.5 b. 3^2+ceil(6/9+3)/2 – 3 3^2+ceil(3.6666)/2 – 3 9 + 4/2 – 3 8 c. 3^2+floor(6/9+3)/2 -3 3^2+floor(3.6666)/2 – 3 9 + 3/2 – 3 7.5 d. 3^2+fix(6/9+3)/2 -3 3^2+fix(3.6666)/2 – 3 9 + 3/2 – 3 7.5 5) Ask the user to input a number, and then display whether that number is odd or even. %%% note that when you divide even numbers by 2, the remainder is 0 x = input('enter a number'); if(rem(x,2)==0) disp('even') else disp('odd') end %%% a better solution that incorporates error checks and tricks: x = input('enter a number'); if(fix(x)~=x) %%% if you eliminate the decimal point and it no longer equals %%% x, then you have a number that is not an integer disp('error- not an integer') elseif(rem(x,2)) %%% if nonzero, meaning there's a remainder, this is true disp('odd') else disp('even') end 6) Create a variable x and assign it some value. Write an if statement that displays the square root of x if x is 0 or greater, and displays “X is negative” otherwise. x = 15; %%%% or whatever number you picked if(x>=0) disp(sqrt(x)) %%% note, no quotes because you want the value else disp('x is negative') end 7) Assign the variable x to be some number. Write an if statement that displays whether or not x is an integer. (hint for one approach: try comparing x with the result of rounding x .) x = 15.6; %%%% or whatever number you picked if(fix(x)==x) disp('x is an integer') else disp('x is not an integer. booyah.') end 8) Use Matlab to compute the following, given that x = 2.7: e^x + x^(1/2)sin(x)cos(x) x = 2.7; exp(x) + x^.5 * sin(x) * cos(x) if(rem(x,2)==0) disp('even') else disp('odd') end %%% a better solution that incorporates error checks and tricks: x = input('enter a number'); if(fix(x)~=x) %%% if you eliminate the decimal point and it no longer equals %%% x, then you have a number that is not an integer disp('error- not an integer') elseif(rem(x,2)) %%% if nonzero, meaning there's a remainder, this is true disp('odd') else disp('even') end 6) Create a variable x and assign it some value. Write an if statement that displays the square root of x if...
View Full Document

{[ snackBarMessage ]}

### Page1 / 8

14-440-127-Fall2010+REC02+Solutions - Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online