350 likes | 479 Views
What for. i f… then… when?. While do you?. 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.
E N D
What for if… then… when? While do you? 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 • 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 Answer
If… then… when? Medium Write a script to check if a number is divisible by 7 or not, using the following 2 methods. ‘mod’ functionthat gives the remainder of any division. ‘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 • 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 Answer
If… then… when? Hard 4.20 Optional
If… then… when? Hard • Analysis: • Two formulas for x, which to use depends on d. • Script: • functionx = package(W,k1,k2,d); • if W < k1*d • x = W/k1; • else • x = (W+2*k2*d)/(k1+2*k2); • end • disp(‘The distance traveled is:’) • disp(x) • end Answer
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 • 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 variables • V = 0; • I = 500; • %While loop • while I >= 200 • V = V + 0.2; • I = 500*exp(-0.353*V); • end • %Output result • fprintf('Total amount of iodine required: %0.1f',V) • fprintf(' mL \n'); Answer
While do you? Medium Approximate the sum of the infinite alternating seriesto an error of 0.01%:
While do you? Medium • Analysis: • The so-called alternating series has the sum of ln(2). • Create a ‘while’ loop to keep adding terms to the sum until the new sum is no more than 0.0001 difference compared to the previous sum. • Script: • %Initialize the variables • S= 0; • error = 1; • i = 1; • %While loop • whileerror >= 0.0001 • S = S + (-1)^(i+1)/i; • error = abs((-1)^(i+1)/i); • i = i + 1; • end • %Output result • fprintf('The sum of the alternating series is: %0.3f',S) Answer
While do you? Hard 4.38
While do you? Hard Answer
While do you? Hard Answer
While do you? Hard Answer
While do you? Hard Answer
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 • Analysis: • Find the maximum value using “for” loop • Compare the max to 10% • Script: • %For loop • Max = C(1); • fori = 1:500 • if C(i) >= Max • Max = C(i); • end • end • %Output result • ifMax < 10 • fprintf(‘Ready for clinical trial') • else • fprintf(‘Not ready for clinical trial’) • end Answer
What for Medium Compute the sum of the cubes of the first 100 positive integers using ‘for’ loop. Not using ‘for’ loop. Hint: use a built-in MATLAB function called ‘sum’ that computes the sum of all elements of a matrix along a dimension (either row or column)
What for Medium Analysis: Run a variable from 1 to 100. Cube it and add it up cumulatively every loop. Create a matrix A containing elements from 1 to 100 (how?). Row or column doesn’t matter. Cube the matrix A sum(A.^3) gives the answer Script: 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 • 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 degC • C = 5*(F-32)/9 • end Answer
What’s your function Medium 3.14
What’s your function Medium Answer
What’s your function Hard 3.17
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 • 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^3 • y2 = exp(x); • plot(x,y1,x,y2) • title(‘x^3 vs. e^x’) %note that ^ gives superscript effect • xlabel(‘x’) • ylabel(‘y’) • legend(‘y1 = x^3’,’y2 = e^x’) Answer
And the Plot thickens Medium 5.3
And the Plot thickens Medium Answer
And the Plot thickens Hard 5.18
And the Plot thickens Hard Answer