Introductory Programming Fall 2006 For today you should have 1) finished the logmap and logchaos example from last time 2) finished Eval 3 or Eval 3b 3) worked on cumulate 4) prepared for an Evaluation For next time you should 1) finish the Chebyshev problem we started today logchaos -------- function logchaos clf hold on % each time through the loop we try a different value of r for r=2.4:0.01:4.0 % compute the first 50 values of the sequence V = logmap(r, 0.5, 50); % trim off the first 10 values V = trim(V, 10); % make another vector the same length, all equal to r R = make_vector(r, length(V)); % plot a column of points showing all the values in V plot(R, V, 'b.') end end function M = logmap(r, x1, n) % use the logistic map function to compute the % first (n) values of the sequence, starting wih % (x1) and using parameter (r) X(1) = x1; for i=1:n-1 X(i+1) = r * X(i) * (1 - X(i)); end M = X; end function W = trim(V, skip) % make a new vector that has all but the first (skip) % elements from (V) n = length(V); for i=1:n-skip W(i) = V(i+skip); end end function V = make_vector(value, len) % make a new vector with (len) elements all equal to (value) for i=1:len V(i) = value; end end Why functions? -------------- For many simple programs, functions are not necessary. And, many of the functions we have written are already in MATLAB. So, you might be wondering what the point is. 1) breaking a big problem into small, simple functions often makes the whole thing easier to do. 2) many of MATLAB's more useful features require you to write functions. logchaos was meant to be an example of #1. Today we'll look at an example of #2. Root-finding ------------ Root-finding is the general name for algorithms that find roots (zero-crossings) of (usually) non-linear equations. Examples include Newton's method, secant method, etc. fzero ----- If you type "help fzero", MATLAB prints something like this (I edited out the less important stuff): FZERO Scalar nonlinear zero finding. X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0, if X0 is a scalar. It first finds an interval containing X0 where the function values of the interval endpoints differ in sign, then searches that interval for a zero. FUN is a function handle. FUN accepts real scalar input X and returns a real scalar function value F, evaluated at X. The value X returned by FZERO is near a point where FUN changes sign (if FUN is continuous), or NaN if the search fails. If x0 is a vector of length two, fzero assumes x0 is an interval where the sign of fun(x0(1)) differs from the sign of fun(x0(2)). An error occurs if this is not true. Calling fzero with such an interval guarantees fzero will return a value near a point where fun changes sign. [X,FVAL]= FZERO(FUN,...) returns the value of the objective function, described in FUN, at X. Examples FUN can be specified using @: X = fzero(@sin,3) returns pi. FUN can also be an anonymous function: X = fzero(@(x) sin(3*x),2) We'll get to anonymous functions later; for now we will use named functions, which is what you have been writing. Root-finding Example -------------------- Let's say you want to find a root of the 6th Chebyshev polynomial: T_6(x) = 32 x^6 - 48 x^4 + 18 x^2 - 1 1) First you should write and test a function that evaluates T6(x). Call it cheby6 2) Test your function and then make it quiet. 3) Write a function called plot_cheby6 that takes two input variables, low and high, and that plots T6(x) over the range low <= x <= high. 4) Plot T6(x) over the range from 0.0 to 1.0 5) Choose two values of x [xlow, xhigh] that appear to bracket a root. 6) Use fzero to find the root. >> fzero (@cheby6, [xlow, xhigh]) 7) If you make cheby6 less quiet, you can see what values of x fzero checks. Can you figure out what algorithm fzero is using? 8) Use different values of [xlow, xhigh] to find the other two roots in [0, 1]