• Notes
• 32
• 100% (1) 1 out of 1 people found this document helpful

This preview shows page 1 - 8 out of 32 pages.

Aduvanced use of quad Numerical differentiation Numerical Differentiation Dhavide Aruliah UOIT MATH 2070U c D. Aruliah (UOIT) Numerical Differentiation MATH 2070U 1 / 34
Aduvanced use of quad Numerical differentiation Numerical Differentiation 1 More advanced application of quad Functions and function functions Integrals with parameters 2 Numerical differentiation (finite-difference) formulas Numerical derivative approximations from Taylor’s theorem Numerical differentiation of numerical data Numerical differentiation in M ATLAB : diff and gradient c D. Aruliah (UOIT) Numerical Differentiation MATH 2070U 2 / 34
Aduvanced use of quad Numerical differentiation Functions and function functions Reminder about functions y = f ( x ) The distinction between x , y , and f is important! x is the value of independent variable/input y is the value of dependent variable/output f is the function itself c D. Aruliah (UOIT) Numerical Differentiation MATH 2070U 4 / 34
Aduvanced use of quad Numerical differentiation Functions and function functions Function m -files Traditional M ATLAB way to define a function foo.m function y = foo(x) y = x.^3 .*exp(-2*x) + cos(x).^2./(1+x.^4); Identifiers foo and y should be distinct Output variable y must be assigned somewhere in m -file Function m -files can have many input/output variables Other variables created are local to function workspace [Input variables generally passed by value] [Also possible to have subfunctions, nested functions, etc.] c D. Aruliah (UOIT) Numerical Differentiation MATH 2070U 5 / 34
Aduvanced use of quad Numerical differentiation Functions and function functions Anonymous functions Anonymous function defined at command line foo = @(x) x.^3 .*exp(-2*x) + cos(x).^2./(1+x.^4); Anonymous functions do not require creating m -file to use Anonymous functions can return only one output variable Anonymous functions must be short (one-line statement) Anonymous functions cannot use control flow (loops) Anonymous functions can use vectorised operators c D. Aruliah (UOIT) Numerical Differentiation MATH 2070U 6 / 34
Aduvanced use of quad Numerical differentiation Functions and function functions Function functions Fundamental requirement for scientific computing: functions that accept other functions as input e.g., to find a zero x * such that f ( x * ) = 0 given f e.g., to compute integral R b a f ( x ) dx given f , a , b Function handle : data type (denoted @ ) for functions (like pointer) Function function : a function that operates on another function (i.e., accepts a function handle as an input argument) We have seen numerous function functions (e.g., fzero , quad ) f = @(z) z-cos(z); % Create anonymous function x = fzero(f,[0,1]) % Find zero of function f in [0,1] I = quad(f,0,1) % Compute integral of f on [0,1] c D. Aruliah (UOIT) Numerical Differentiation MATH 2070U 7 / 34
Aduvanced use of quad Numerical differentiation Functions and function functions Important: syntax of function functions When passing a function foo to a function function: I Anonymous function foo : do not use @ in call to function function I Built-in/ m -file function: @ required in call to function function
• • • 