Spring 2001 CS100M Solutions to Exercise E5 1. function p = addpoly(p1,p2) p = p1 + p2; note: in p3.4 we always assume coefficients are stored right-to-left; canonical refers only to whether there are leading 0s in the polynomial, which corresponds to trailing zeros in the vector of coefficients. a) $addpoly([0 1], [0 -1])$ produces $[0 0]$ instead of $[0]$. (sum of $x$ and $-x$ is $0x+0$, which we want simplified to $0$.) or $addpoly([5], [0 1]$ produces $[5 6]$ instead $[5 1]$ because $[5]$=$5$ is a scalar added to a vector $[0 1]$. note: the sum of a non-canonical poly with a canonical poly can be canonical, e.g. [0 1] + [0 0] = [0 1], i.e. x + 0 = x. b) $addpoly([0 1], [0 0 1])$ tries to add two vectors of different sizes, which is an error in Matlab. (the answer should be $[0 1 1]$.) 2. loop invariant $X*Y = x*y + z$ holds at: (a), (b), (d), (f), (h), (k), (m) note: for questions like this, please provide a list, as above, not a list of true/false values (do that for partial credit, but not your final answer). snapshot: (a) (b) (c) (d) (e) (f) (g) (h) (i) (k) (l) (m) | | | | | | | | | | | | # 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 5 | 6 | 7 | 8 | X 6 | | | | | | | | | | | | Y 11 | | | | | | | | | | | | x 6 | | 11 | | 22 | | | | 44 | | | | y 11 | | | 6 | | 3 | | 2 | | 1 | | 0 | z 0 | | | | | | 22 | | | | 66 | | t | 6 | | | | | | | | | | | a student asked: > % PS The answers to this exercise are fully text, without any code. > % Why was there a need for us to submit our answers as an .m file? + to make sure people know how to use the matlab editor + to make sure people submit text files, not word documents