Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture 17 - Functions: Sub-functions, Anonymous Functions, and Function Handles - Prof. G, Study notes of Biology

A lecture note from a mathematics class focusing on functions, specifically sub-functions, anonymous functions, and function handles. Examples, syntax, and usage of these functions, as well as instructions for in-class problems and reading materials for the next class session.

Typology: Study notes

2010/2011

Uploaded on 04/06/2011

goalie4eva
goalie4eva 🇺🇸

31 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Lecture 17 - Functions: Sub-functions, Anonymous Functions, and Function Handles - Prof. G and more Study notes Biology in PDF only on Docsity! Lecture 17: Monday March 21, 2011: Announcements:  Case 3 is due Wednesday March 23rd. 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 some graded exams to be picked up.  SH7 will be up by tomorrow afternoon.  Case 4 will be up late this week.  Exam 2 is scheduled for Wednesday, April 6th in-class. Reading for Next Time: -Debugging Palm Chapter 4.8. -Ch 9.1 and 9.2 more on Numerical Differentiation and Integration. -After that start looking at Ch. 8 on Linear Algebraic Equations. *Today’s Goals: . Discuss Sub-functions , Anonymous Functions and Function Functions IN CLASS GROUP PROBLEM: Do example in Palm Ch 4. page 216-217 Problem 48. Define and use a function to code up the Inventory Model. *I REMEMBER: turn on your diary to save your Command Window >>diary LectureMar21.m *II. More on Functions – Subfunctions: (Palm p. 134) You can nest subfunctions within primary functions - subfunctions can only be called from within the primary/parent function. For example: x=3, y=0, z=10; function [x, y, z] = computeThis2(x, y, z) % a simple function that takes 3 input values, % and returns the same 3 values that may or may not have been updated in %the function body. This is the parent/primary function. if x==y %use == to compare for equivalence y = z x = x^2; elseif x < y %less than y = x+1; else %if previous statements are false . . . [x, y, z]= subElse(x, y, z) end % terminate code block end % terminate parent function %A subfunction that can only be used by functions in this file function [l, m, n] = subElse(l, m, n) m = l^2; l = n; end In the above example, when computeThis2 uses the subElse function, and we overwrite the original values for x, y, and z since these are being returned inside the parent function. 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 -->
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved