cs249 lecture notes, Fall 2001 Week 7, Monday Quiz 5 Schedule exam... October 22 or 25? Vocabulary: array: a sequence of values (not necessarily numbers) element: one of the values in an array index: an integer used to indicate one of the values in an array Quiz 5 Solution --------------- Part 1: Most idiomatic version function result = sum (a) total = 0; for i = 1:length(a) total = total + a(i); end result = total; 1) length is a built-in function 2) a(i) uses i as an index to select an element of a 3) the accumulator pattern: initialize before the loop, modify inside the loop, use the result outside the loop Alternate version function result = sum (a) total = 0; for x = a total = total + x; end result = total; The latter version emphasizes the semantics of the for loop for variable = array Assigns each element of the array to the variable, in turn. Part 2: function result = selectOdd (a) index = 1:2:length(a) result = a(index) Two things here 1) an array constructor with an increment 2) using an array as an index Strictly, the elements of the index array should be integers. Array concatenation ------------------- Sections 5.2 and 5.3 lead right into Section 5.10 1) array concatenation looks like [head tail] 2) the empty array is [] 3) you can accumulate an array by concatenating onto the beginning or the end Willis equation --------------- 1) Using linspace and for loops to enumerate points in the complex plane. 2) Checking parity with the modulus operator. 3) How to use clf, hold and axis 4) a tiny bit about object handles function result = mandel2 (n) clf; hold on; f = gcf; set (f, 'DefaultLineMarkerSize', 2); willis (n) function willis (n) coords = linspace (-3, 3, n); for real = coords for imag = coords z = real + i * imag; a = iterate (z); if (a == -1) continue elseif (mod(a,2) == 0) plotPoint (z, 'rs', 'red'); else plotPoints (z, 'bs', 'blue'); end end end axis ([-3 3 -3 3]); function plotPoint (z, format, color) h = plot (z, format); set (h, 'MarkerSize', 2); set (h, 'MarkerFaceColor', color); function result = iterate (z0) z = z0; lastz = z0; for j = 1:100 z2 = z*z; f = z*z2 - 2*z - 5; fp = 3 * z2 - 2; z = z - f / fp; diff = z - lastz; if (abs (diff) < 1e-8) result = j; return end lastz = z; end result = -1; Array operations ---------------- + and - perform elementwise addition and subtraction We used that for vector arithmetic. array * scalar or scalar * array is elementwise array * array is dot product, which only works if the sizes of the array are compatible array / scalar is elementwise scalar / array and array / array are something else array ^. scalar is elementwise exponentiation Plotting -------- The second figure in Section 5.7 is wrong. Sorry! Euler's method -------------- Most general form dy/dt = f (t, y) For many physical problems, the rate is a function of "position" or "time", but not both. Constant acceleration dy/dt = gt "position" is position Cooling dy/dt = -r (y - Ts) "position" is temperature Draw quivers for these problems. Thursday: let's do acceleration of gravity with wind resistance and find terminal velocity.