Introductory Programming Fall 2006 For today you should have 1) finished Evaluation 3 2) worked on the exercise in notes06 (findit) Next time (Friday) we meet in AC109 at 9am or 10am! 1) work on the exercise below (cumulate) 2) no evaluation Quiet Functions --------------- What does this line of code do? >> y = cos(pi); 1) Evaluates the argument (pi) 2) Calls the function (cos) 3) When the function is done, the return value is -1 4) Assigns the return value to y Does anything get displayed? Most functions don't display anything; they just compute and return a value. I'll call them "quiet functions" Exercise: Write a quiet function that takes an input variable named x and computes sin^2(x). Call your function from the interpreter and assign the result to a variable. Evaluation 3 ------------ Top 10 Points of Difficulty: 1) the mechanics of writing a script/function and running it It is easy to put a .m file somewhere MATLAB won't find it. Always start by a) create a new .m file; put something trivial into it (x=5) b) save it and put it in the default location c) go to the interpreter and run it 2) too much MATLAB GUI getting in the way MATLAB displays too much _stuff_ Arrange your windows so you have two things visible, your program and the interpreter. Ignore everything else. 3) I'm really not as sadistic as you think I am. The exercises and evaluations can be done using only the tools we have covered and practiced. If you find yourself inventing radically new stuff, you are probably off the map. Also, keep the easy things easy. If I say to write a function named logmap that takes two input variables, you should be able to write the first line without thinking. 4) keep track of what is a vector and what is a scalar (and use upper/lower case to remind yourself) function V = logmap(r, x1) X(1) = x1 x1 is the value of x during year 1, so it's a scalar. (a source of confusion -- when we translate from math to MATLAB, we sometimes make different choices about what's a scalar and what's a vector) 5) functions can return vectors. Some of you got off to a good start and wrote function V = logmap(r, x1) X(1) = x1 for i=1:19 x(i+1) = r * x(i) * (1 - x(i)) end % NOW WHAT?!? And then got confused. 6) off by one errors! See http://en.wikipedia.org/wiki/Off-by-one_error "An off-by-one error is a logical error involving the discrete equivalent of a boundary condition. It often occurs in computer programming when an iterative loop iterates one time too many or too few. Usually this problem arises when a programmer fails to take into account that a sequence starts at zero rather than one (as with array indexes in some languages), or makes mistakes such as using "is less than or equal to" where "is less than" should have been used in a comparison. This can also occur in a mathematical context." 7) indentation MATLAB doesn't enforce proper indentation, but I wish it did. Indentation helps you see your program clearly, which helps you spot errors (or avoid them in the first place) 8) input variables when you write a function, you don't know what the input variables will be. I don't wanna see this: function V = logmap(r, x1) r = 3.9 x1 = 0.5 end The input variables get their values when you call the function (not when you define it) V = logmap(3.9, 0.5) The first thing MATLAB does is assign 3.9 to r and 0.5 to x1. When you are writing the function, assume that the input variables will have values, but you don't know what they are. 9) output variables The last thing a function does is assign a value to the output variable function V = logmap(r, x1) X(1) = x1 for i=1:19 x(i+1) = r * x(i) * (1 - x(i)) end V = X end Displaying a value on the screen (what I insist on calling "printing") is not the same thing as giving a value to the output variable (which is what I mean when I say that the function "returns a value") 10) when you call a function, assign the return value to a variable V = logmap(3.9, 0.5) plot (V) One note: on evaluations, please follow the instructions as well as you can. If you do more than I ask for, I can't tell whether you are showing off or confused. Generalization -------------- One way to develop programs is to start with a function that does the same thing every time, and gradually generalize it by adding new input variables. Exercise: generalize logmap so that it takes an (additional) input variable named n and returns a vector with length n. findit ------ Write a function named "findit" that takes two input variables, a vector and a target value. Your function should search the vector and return the index of the first location where the target value appears in the vector, or -1 if the target value does not appear in the vector. Start with this: function index = findit(V, target) index = -1; end And then call it from the interpreter: >> i = findit(1:10, 5) Now what? HINT: the return statement immediately ends a function call and "returns" to wherever the function was invoked. cumulate -------- Write a function named cumulate that takes a vector as an input variable and returns a vector as an output variable. The return value should be the cumulative sum of the elements in the input vector; that is, the ith element of the output should be the sum of the first i elements of the input. For example: >> X = 1:5 X = 1 2 3 4 5 >> Y = cumulate(X) Y = 1 3 6 10 15 Note: MATLAB has a built-in function called cumsum that does the same thing.