You are on page 1of 2

ME2315 Computer-Aided Analysis, Spring 2011 HW #3 due 2/7/2011 1 of 2 Read: section 3.3.

1, omitting the switch statement Upload one M-file per problem unless instructed otherwise.

Problems from your text: none this time Additional problems: HW3_1: Repeat problem HW2_5, but modify it to work for a 2D vector as well. Add input validation: if the input array does not contain two or three items, return an error message, using error. Hint: numel could be helpful. HW3_2. All the rides in a state-of-the-art amusement park contain biometric sensors that measure data about potential riders while they are standing in line. Assume the sensors can detect a riders age, weight, heart problems and possible pregnancy. For each ride at the park, help the engineers write the conditional statements based on the safety specifications:
a)The Spinning Beast: All riders must be 17 years or older, more than 62 inches tall, and must not be pregnant or have a heart condition if ________ ; disp(Sorry you cannot ride the Spinning Beast); end b)The Lame Train: All riders must be 8 years or older and must not be pregnant if ________ ; disp(Sorry, you cannot ride the Lame Train); end c) The MATLAB House of Horrors: All riders must be 17 years or older and must not have a heart condition if_________ ; disp (Welcome to the MATLAB House of Horrors!); end d) The Neck Snapper: All riders must be 16 years or older, more than 65 inches tall, must not be pregnant or have a heart condition if_________ ; disp(Welcome to the Neck Snapper); end Variable definitions: age, height, weight: all integers. Heart condition (HC) and pregnancy (p), both Boolean(1 if heart condition, 0 if not; 1 if pregnant, 0 if not) Place the code for all of these in one script M-file. To test the script file, try entering the following in your workspace & running the file. Try other combinations on your own to make sure it works.
>>age=14; height=60; HC=0; p=0; %does this cause an error? Generates Spin. Beast msg >>age=18; height=66; weight=130; HC=0; p=1; %Generates LT, SB & MATLAB msgs >>age=55; height=72; weight=200; HC=0; p=0; %Generates MATLAB & Neck Snapper msgs

HW3_3: The speed limit on many Texas highways is 70 mph in the daytime & 65 at night. Write a MATLAB function that will display the message speeding to the screen when the vehicle is speeding. There should be 2 input arguments to the function: the vehicles speed, and a day/night indicator (day=1, night=0) For example: >> speeder(68,0) speeding

ME2315 Computer-Aided Analysis, Spring 2011 HW #3 due 2/7/2011 2 of 2 HW3_4 Predict what will appear in the command window when the following MATLAB code is executed. Verify the results in MATLAB. Copy each block of code into an M-file and for each case, set values of A&B so that the different results will occur than what is shown below.

A=4;B=3; if A < B * 3 disp('**') else disp('&&') end

A=9;B=3; if B > A/3 disp('**'); elseif B < A/3 disp('&&'); end

A=10;B=4; if (A == 5) || (B < 2) B=B/2 else B=B*2 end

HW3_5 Write code that it does the following: if A=5 & B<2, then B is assigned its current value divided by two. If A = 5 & B 2, then B is assigned two times its current value. Otherwise there is no change to the value of B. Test with A=5; B=1, A=6;B=2, A=5;B=2; A=6;B=1. Upload the correct code, with all four test cases. HW3_6 Predict what will appear in the command window when the following MATLAB code is executed. Write your work in comments and run the code to verify the results in MATLAB. The first one is done for you. See figure 3.1 in your book. x=3;y=6;z=-3; x<3 && y<6 %(3<3) && (6<6) => 0 && 0 => 0 x<3 || y<6 (x<y) && (y<z) || (z<x) x<y && (y<z || z<x) x>-1*z ~(y>=x*3)&& ~(y<=z*3) ~((y>=x*3)&& (y<=z*3)) z<x<y (-1*y)<z<0 HW3_7 Write MATLAB code two different ways to test for the following condition: A is between the values of 10 and 20, exclusive. Test them with A=9,10,11,19,20,21. Upload correct code & test cases in a script M-file. HW3_8 A piecewise function is useful when the relationship between a dependent and independent variable cannot be adequately described with a single equation. Write a MATLAB function to evaluate f(x) for any scalar input x. Why cant we evaluate for array input x? Include input validation to generate an error message for non-scalar input, and when f(x) is undefined (use error). Create a script M-file that sets different values of x as input to your function to test all four cases. (similar to problem HW2_4) Upload the script M-file & the function M-file
3x +12 3 f (x) = 6x x 2 undefined 4 x 3 3 x 3 3 x 6 x < 4;x > 6