Recitation 02 Problems- Solutions

Recitation 02 Problems- Solutions - 14:440:127 Introduction...

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

View Full Document Right Arrow Icon
14:440:127 Introduction to Computers for Engineers Recitation 2- Problems for students to attempt Please complete these problems on your own time if you do not finish in recitation. You don’t need to show us the completed recitation problems, although we recommending saving a copy of your solutions for your own benefit. 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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
%%% 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.'
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/11/2010 for the course 440 127 taught by Professor Blase during the Fall '09 term at Rutgers.

Page1 / 7

Recitation 02 Problems- Solutions - 14:440:127 Introduction...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online