Review Questions for Prelim T2 Notes: + this is a set of review questions, not a sample prelim. + the specifications of some problems here are much longer than specifications will be on prelim T2. =============================================================================== 0. Study P2, P3, P4 and the other coursework covered by the exam. =============================================================================== 1. Without using $:$, write a function $mycolon(j,d,k)$ that takes 3 numbers and returns the vector $j:d:k$. a) Do use without using any pre-defined functions. b) Give an alternative function body that is vectorized. You still may not use $:$, but now you may use pre-defined functions, [3/13] e.g. $cumsum$. ******************************************************************************* Note: vectorization is on the exam, but will NOT be worth many points: If you have trouble with a vectorization question, do NOT spend a lot of time on it! ******************************************************************************* =============================================================================== 2. Background explanations are given after the questions. a) Write a function $checkisbn(isbn)$ that takes an arbitrary input and returns "$isbn$ is a string representing a valid ISBN number?", i.e. returns 1 if the answer to the question above is true, and returns 0 if the answer to the question above is false. Ignore case (upper versus lower case letters). Include a correct and useful loop invariant. b) For a few points, write a vectorized version of the function body. Note: since this count for only a few points, do not spend too much time on it. ---------------------------------------------------------------- Background: Every book has an ISBN number, a string of length ten consisting of 9 digits (characters in the range '0'..'9') and a *checksum* digit, which is 'X' or a digit. The checksum digit is computed as follows: 1. multiply the $i$-th digit, $i$=1..9, by $i$; add all the products together. let $star$ be the sum. 2. compute the remainder when $star$ is divided by 11: a. if the remainder is in the range 0..9, that is the checksum digit. b. if the remainder is 10, the checksum digit is 'X'. Example: the paperback edition of _Tam Lin_ by Pamela Dean has ISBN# 0812544501. the last digit '1' is the checksum digit. let us check that the algorithm above produces the correct answer. star = 0 8 1 2 5 4 4 5 0 * * * * * * * * * 1 2 3 4 5 6 7 8 9 + + + + + + + + = 1*0 + 2*8 + 3*1 + 4*2 + 5*5 + 6*4 + 7*4 + 8*5 + 9*0 = 144 144 / 11 = 13 remainder 1, and 1 is indeed the final, checksum digit. Hints: 1. digits are internally represented by Matlab by consecutive integers: char: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' representation: 48 49 50 51 52 53 54 55 56 57 2. The way Matlab knows if the numbers in a vector are to be thought of as numbers or characters is that there is a *flag* (think: "note") attached to each vector indicating what its "datatype" is. The numerical datatypes we care/know about are $double$, $char$, and $logical$. 3. You can use $ischar$ to test if a vector is a string, i.e. holds characters (instead of numbers or true/false values). 4. You can use $char$ to convert a vector (a scalar is a vector, too) of numbers into a string (a vector of chararacters). 5. You can use $double$ or the usual arithmetic operations ($+$, $-$, etc.) to convert a string (a single character is a string, too) into a vector of numbers. Example: try $double('081254450')$ and $'081254450' - '0'$ =============================================================================== 3. Let functions $f$ and $g$ be defined as follows: function x = f(a,b) x = a+b; if a >= b & b > 0 x = f(a-b, b); elseif a > 0 x = g(b,a); end function x = g(a,b) if a > b & b > 0 x = g(a-b, b); else x = f(b, a); end Assume the code below is run in the command window. Use box scope diagrams to trace the execution of the code. x = 3; a = 4; b = 7; a = g(b+2, x*5); =============================================================================== 4. The spring equation is $F = -k d$, where $F$ = the force exerted by a spring $d$ = the displacement of the spring from its rest length $k$ = the spring constant = how strong the spring is Write a function $spring(x,y,k,dt,T)$ to plot the evolution of the system described below for $T$ time steps, using a stepsize of $dt$: + vectors $x,y$ have same length $n>=2$. + there are $n$ balls, numbered $1..n$. + each ball has unit mass and radius 0 (is infinitely small) [3/13] and is initially at rest (velocity 0). + the initial position of ball $i$ is $(x(i),y(i))$, $i=1..n$ + ball $i$ is connected to ball $i+1$ ($i=1..n-1$) by a spring with rest length 0 and spring constant $k$: That is, if ball $i$ and ball $i+1$ are distance $d$ apart, the spring tries to draw the two balls together by exerting a force of magnitude $k*d$ along the line connecting the two balls: ball i spring ball i+1 | | | V V V *-\/\/\/\/\/\/\/\/\/\-* force k*d |--> <--| force k*d | <-- distance d --> | Hints/Further Instructions: + After each time step, plot the springs as lines and the balls as open circles. Make all plots show up on the same graph by using $hold$ appropriately. (Plotting is not on the exam, but it fits well with the rest of this question.) + The overall force of a ball connected to two springs is the sum of the forces from each spring. + Since the balls have unit mass, the equation $F = ma$ simplifies to $F = a$ (ignoring units), which together with $F = -k * d$ gives an equation for acceleration $a$. + Recall that acceleration = rate of change of velocity and velocity = rate of change of position. + Use the same approach to updating position that we used in $celestial$. =============================================================================== 5. Given a vector $v$ of non-negative integers pre-sorted in ascending order, write a script to read numbers from $v$ one at a time until 1000 appears as an element or all elements of $v$ have been processed and print out a horizontal histogram of the numbers: print one "*" for each occurrence of a number. [3/13] Note: original version sometimes used "score" instead of "number". Make the output look nice. Example: output for $v = [5 8 8 8 9 9 10 1000 1984 2001 2010 2061 3001]$: 5 * 6 7 8 *** 9 ** 10 * a) Write your code subject to these constraints: + Except for scalars and reading elements of $v$, do not use any vectors, including $:$. + Efficiency counts, but only for a few points, so do not spend too much time worrying about efficiency. + Do not use $break$ or $return$. b) Now suppose vectors are allowed and that there are no "gaps". (in the example above, 6..7 is a gap between 5 and 8.) Fill in the blanks below to complete a vectorized solution that is allowed to use and modify $v$: ____________________________ ; % concatenate 1000 to the end of $v$ while _______________________ % loop until done k = _________________________ ; % number to print stars for n = _________________________ ; % # of stars to print _____________________________ ; % print $k$ and $n$ stars _____________________________ ; % delete all occurrences of $k$ from $v$ end =============================================================================== 6. Suppose $sum(x) == 0$ is used to test whether all elements of vector $x$ are 0. Explain all the reasons that this doesn't work; give concrete examples to illustrate your points.