Jeopardy Game (MATLAB)

31
if… then… when? While do you? What for What’s your function And the plot thickens Easy Easy Easy Easy Easy Mediu m Mediu m Mediu m Mediu m Mediu m Hard Hard Hard Hard Hard

description

MATLAB review, if then else, while loop, for loop, plot, function

Transcript of Jeopardy Game (MATLAB)

if… then… when?

While do you?

What for What’s your function

And the plot thickens

Easy Easy Easy Easy Easy

Medium Medium Medium Medium Medium

Hard Hard Hard Hard Hard

If… then… when? Easy

Nick is taking CHEE 1331 for his first time. He doesn’t even need the course but the curriculum forbids him to skip it. It’s not that he doesn’t like it but he is just confident in his programming skills (a little over confident). Thus, he pays less and less attention to the course material and take-home assignments. Nick starts to score unexpectedly low in the tests. When it comes to the last exam, he calculates that he needs an 67.8 to get a C and a 93.9 to get a B on the final. Let N be Nick’s final exam grade, and N be given by you (assume that you were a god and you had the power to set Nick’s exam grade to be anything). Write a script to determine Nick’s fate in this course, given that he is desperate for a C, or else his dad will kick his butt for 100 times. Note that the highest possible score on the final is 100.

If… then… when? Easy

Answer

Analysis: N is given (by you)• N < 67.8: Butt kicked!• 67.8 < N < 93.9: grade C• 93.9 < N < 100: grade B• N > 100: Wow, this god just breaks the rule. This’s impossible.

Script:N = input('Nicks final exam grade is: ');if N < 67.8 fprintf('Nick fails \n')elseif and((N >= 67.8),(N < 93.9)) == 1 fprintf('Nick gets a C \n')else fprintf('Nick gets a B \n') fprintf('Wow! \n')end

If… then… when? Medium

Write a script to check if a number is divisible by 7 or not, using the following 2 methods.a) ‘mod’ function that gives the remainder of any division.b) ‘floor’ function that rounds a real number to an integer that is less than or equal

that number.

Your script shall be used to check for the divisibility of a number by any divisor by only a slight modification.

If… then… when? Medium

Answer

Analysis:• Definition of being divisible: remainder is zero• If the quotient of A/B is exactly the floor of itself then A is divisible by

B.

Script:A = input(‘Give a number: ');if mod(A,7) == 0 (or if floor(A/7) == A/7) fprintf(‘A is divisible by 7 \n')else fprintf('A is not divisible by 7 \n ')end

If… then… when? Hard

4.20

Optional

If… then… when? Hard

Answer

Analysis:• Two formulas for x, which to use depends on d.

Script:function x = package(W,k1,k2,d);if W < k1*d x = W/k1;else x = (W+2*k2*d)/(k1+2*k2);enddisp(‘The distance traveled is:’)disp(x)end

While do you? Easy

Alice is working on an experiment. She is given a beaker of starch solution and a pipet that can pump a fixed volume of 0.2 mL of iodine. What happens if she adds iodine to starch solution? A light source is shone through the beaker and the intensity of the transmitted beam is measured in lumens (lm) unit. When no iodine is present, it measures 500 lm. Alice’s advisor wants her to calculate the total amount of iodine required to reach a measurement right below 200 lm, given that the intensity is dependent on the added volume of iodine via the following relationship:

Where I represents the light intensity (lm) and V indicates the added volume of iodine (mL). Note: the given expression is made up for the purpose of this problem only. One should not use it as a reference.

While do you? Easy

Answer

Analysis: Easily solved: . But how about “While” loop?• Condition to do the loop: I > 200. Exit whenever I drops below 200• Keep calculating the intensity and feed it back to the condition

Script:%Initialize the variablesV = 0;I = 500;%While loopwhile I >= 200 V = V + 0.2; I = 500*exp(-0.353*V);end%Output resultfprintf('Total amount of iodine required: %0.1f',V)fprintf(' mL \n');

While do you? Medium

Write a script to check if a number is divisible by 4 or not. Can your script be modified only slightly (within a few characters) to check for the divisibility of a number by 7? How about any divisor?

While do you? Medium

Answer

While do you? Hard

Write a script to check if a number is divisible by 4 or not. Can your script be modified only slightly (within a few characters) to check for the divisibility of a number by 7? How about any divisor?

While do you? Hard

Answer

What for Easy

Jimmy is assigned to measure the relative amount of cancer cells in 500 mice (in percent) after they are treated with an experimental cancer treatment fluid. It is widely believed that if the amount of cancer cells present in the mice that has the most malignant tumor is less than 10% then the treatment is accepted for clinical trial. Suppose that Jimmy has already recorded all the data needed from the mice. However, the size of data is so large that he decides to turn to you for some computational help. Fortunately, you have just learned the powerful “for” loop today. How would you apply the loop to help Jimmy on this?

Note: Let C be the data vector

What for Easy

Answer

Analysis:• Find the maximum value using “for” loop• Compare the max to 10%Script:%For loopMax = C(1);for i = 1:500 if C(i) >= Max

Max = C(i); endend%Output resultif Max < 10 fprintf(‘Ready for clinical trial')else fprintf(‘Not ready for clinical trial’)end

What for Medium

Compute the sum of the cubes of the first 100 positive integers.

What for Medium

Answer

What for Hard

4.23

What for Hard

Answer

What’s your function Easy

Mohammad has been studying abroad in the US for only a few week, which is not a long enough time that he keeps running into trouble of understanding the Fahrenheit system, a common temperature unit in the US. As a result, he constantly asks his roommate, Adam, to check on the temperature outside. Adam is getting tired of converting to and Mohammad is such a lazy student that he doesn’t even bother doing it himself. Therefore, Adam decides to give Mohammad a much faster way to know the corresponding Celcius degree given any Fahrenheit temperature: a convenient MATLAB function. What does Adam have to write for this MATLAB function?

What’s your function Easy

Answer

Analysis:• Function template with output(s), inputs, and a function name• Output: Celcius (C)• Input: Fahrentheit (F)• Function name: temp (or anything without any space in between)

• Use the conversion formula:

Function:function C = temp(F)%Conversion of degF to degCC = 5*(F-32)/9end

What’s your function Medium

.3

What’s your function Medium

Answer

What’s your function Hard

Write a script to check if a number is divisible by 4 or not. Can your script be modified only slightly (within a few characters) to check for the divisibility of a number by 7? How about any divisor?

What’s your function Hard

Answer

And the Plot thickens Easy

Nicole is working on her math homework assignment. She is then stuck at the question that asks her to compare and . Nicole decides to seek help from her engineering-major friend Joey. Joey looks at the problem and is also having a hard time solving it. However, he quickly thinks of a powerful tool that he has learned recently: MATLAB. Joey opens MATLAB and attempts to visualize the problem because he believes graphical interpretations usually give many useful information. And his belief is well paid off with him and Nicole finally seeing the true behaviors of the two functions and being able to compare them with ease. What has Joey done?

And the Plot thickens Easy

Answer

Analysis:• Symbolic plot (ezplot) vs. numerical plot (plot)• Excel can be used as a reference

Script:x = 0:0.01:6;y1 = x.^3; %vs. x^3y2 = exp(x);plot(x,y1,x,y2)title(‘x^3 vs. e^x’) %note that ^ gives superscript effectxlabel(‘x’)ylabel(‘y’)legend(‘y1 = x^3’,’y2 = e^x’)

And the Plot thickens Medium

5.3

And the Plotthickens

Medium

Answer

And the Plot thickens Hard

5.18

And thePlot thickens

Hard

Answer