Download Lecture 18: Anonymous Functions and Function Functions - Prof. Gregory Sawicki and more Study notes Biology in PDF only on Docsity! Lecture 18: Wednesday March 23, 2011: Announcements: Case 3 is due today. Case 4 will be posted by Friday afternoon and is due Wednesday April 14th. Please take advantage of Prof. Sawicki’s Office Hours on M’s and T’s from 4-5PM 4212C EB3 or by appt. Ben’s hours are still being held Thursday afternoons. Still have a couple of graded exams to be picked up. SH7 is up and will be due Th March 31st in lab. Exam 2 is scheduled for Wednesday, April 6th in-class. Practice exam 2 will be posted by afternoon of Wed. March 30th. We will review the practice exam Monday April 4th in class. Reading for Next Time: -Debugging Palm Chapter 4.8. -Ch 9.1 and 9.2 more on Numerical Differentiation and Integration. -Ch. 8 on Linear Algebraic Equations. *SH EC: Write two anonymous functions for h(z)= 6ez and g(y) = 3cosy. Then use them to plot 6e3cosy for 1<y<=20. *Today’s Goals: Examples similar to SH7 using Anonymous Functions and Function Functions. IN CLASS PROBLEMS (w GROUP HELP) 1. Palm Ch 3.2 p.129 T3.2-Problems 1, 2, 3 2. Palm Ch 9.2 p. 412 Problem 17. 3. Palm Ch 3.2 p.143 Problem 14. Rainfall Estimates. *I REMEMBER: turn on your diary to save your Command Window >>diary LectureMar23.m Anonymous Functions: (Palm Ch 3. pg. 132-133) Anonymous functions provide a quick, in-line method of defining and executing a function without having it in a separate m-file. This is useful for simple expressions like, exponential equations or polynomial equations. Syntax is as follows: function name = @(argument list) expression The function handle (i.e. reference) is just the name of your function. The argument list can be as long as you like, or even empty (see Palm page 133). The expression can only be a single executable MATLAB expresion. To call/execute the anonymous function: output variable name= function handle(argument list) For example, to define an anonymous function called sq that can compute the square of any input value x --> %Example of an anonymous function with one input %function handle = @(input argument(s)) function expression sq = @(x) x.^2; %This defines a function sq that computes the %square of the input value x (which can be an array - note %element wise definition) %Then, a bunch of ways to call it- y= sq(5) z= sq([5 6 8]) a=6, b=12, c=13; h=[a b c]; t=sq(h) Anonymous functions can be defined with multiple inputs. For example --> %Example of an anonymous function with two inputs sumsqrt= @(x,y) sqrt(x.^2 + y.^2); % This defines the function %sumsqrt % To call it: y= sumsqrt(4,5) Anonymous functions can call each other (Palm p. 133). For example --> %Example of anonymous functions calling each other. f= @(x) x.^3; g= @(x) 5*sin(x); h= @(x) g(f(x)); answer=h(2) Function Functions: Palm p124-129 (e.g. fzero, fminbnd, fminsearch, and fsolve) and Function Handles (p. 124).