Using Function Handles & Character Strings in Matlab Integration - Prof. E. Cliff, Study notes of Engineering

How to use different types of arguments, including inline functions, character strings, and function handles, to pass functions as arguments to matlab integration functions such as quad.m. Examples of each method and explains the advantages and limitations of each approach. It also discusses the concept of subfunctions and how they can be used in the context of function functions.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-ehp
koofers-user-ehp 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Function Functions in Matlab
AOE/ESM 2074
1 Introduction
Matlab functions are important elements in a strategy of dividing a complex
computational task into a sequence of simpler sub-tasks. These are vital if we
are to be able to re-use code or to use components from someone else’s codes.
Matlab has a number of built-in functions - these are part of the basic
kernel, and also has a number of toolboxes with an impressive array of useful
functions.
As an example, we consider quad.m which provides a numerical approxima-
tion to the evaluation of a definite integral, such as
I(f, a,b)=b
a
f(x)dx .
Note that Ievaluates to a number and that this value depends on three input
arguments:
1. the integrand function f,
2. the lower limit a,and
3. the upper limit b.
We are familiar with the use of actual arguments in the evaluation of functions,
so we can easily supply the real scalars aand bto the quad procedure, but how
do we tell it about the function f?Matlab uses the term function functions
to describe this type of situation, wherein the evaluation of a function (the
integral) depends on a user-supplied function (the integrand).
In Matlab 6 (R 12) there are three ways to tell the procedure about the
function argument:
1. as an inline function,
2. as a literal character string (or a variable that evaluates to such a character
string), and
3. as a function handle (new to R 12).
In case 2, the character string must name a function.m file that is in the current
matlabpath. The ability to evaluate a function using its name or handle is
provided by the Matlab function feval.
1
pf3
pf4

Partial preview of the text

Download Using Function Handles & Character Strings in Matlab Integration - Prof. E. Cliff and more Study notes Engineering in PDF only on Docsity!

Function Functions in Matlab

AOE/ESM 2074

1 Introduction

Matlab functions are important elements in a strategy of dividing a complex computational task into a sequence of simpler sub-tasks. These are vital if we are to be able to re-use code or to use components from someone else’s codes. Matlab has a number of built-in functions - these are part of the basic kernel, and also has a number of toolboxes with an impressive array of useful functions. As an example, we consider quad.m which provides a numerical approxima- tion to the evaluation of a definite integral, such as

I(f, a, b) =

∫ (^) b

a

f (x) dx.

Note that I evaluates to a number and that this value depends on three input arguments:

  1. the integrand function f ,
  2. the lower limit a, and
  3. the upper limit b.

We are familiar with the use of actual arguments in the evaluation of functions, so we can easily supply the real scalars a and b to the quad procedure, but how do we tell it about the function f? Matlab uses the term function functions to describe this type of situation, wherein the evaluation of a function (the integral) depends on a user-supplied function (the integrand). In Matlab 6 (R 12) there are three ways to tell the procedure about the function argument:

  1. as an inline function,
  2. as a literal character string (or a variable that evaluates to such a character string), and
  3. as a function handle (new to R 12).

In case 2, the character string must name a function.m file that is in the current matlabpath. The ability to evaluate a function using its name or handle is provided by the Matlab function feval.

2 inline Reference

Here we display two examples of the use of inline to construct a function argument. Note that in using g we do not surround this with quotation marks. The function reference is g and not the literal string character ‘’g”.

g = inline(’t.sin(t)’) g = Inline function: g(t) = t.sin(t) quad(g,0,pi) ans =

g = inline(’sin(x).x.^P1’,1) g = Inline function: g(x,P1) = sin(x).x.^P quad(g,0,pi,[],[],1) ans =

In the second instance we use the form that allows for parameters to appear in the function definition. The inline procedure is somewhat restrictive here: the name of the variable must be x and the parameters are P1, P2 and so on. The number of parameters is provided in the second argument to inline and this argument must evaluate to a positive integer.

3 Character String Reference

Here we display the use of a character string as a reference to the function. The string must evaluate to the name of a .m file that is in the current path. In this example, the sin lim function contains the single assignment

value = (0 < t & t < pi/2) .* sin(t) + (pi/2 < t);

We first show that the sin lim.m file is in the current path.

which sin_lim c:\Documents and Settings\localuser\My Documents\aoe_esm \lectures\lecture_17\sin_lim.m <== line folded for printing

fname = ’sin_lim’ fname = sin_lim quad(fname,0,pi) ans =

5 Subfunctions

A function .m file in Matlab can contain additional functions - subfunctions (or internal functions). These functions are placed at the bottom of the file, after the command sequence associated with the primary function. This capability is useful because the scope of these subfunctions is limited to the primary function. That is, such subfunctions are not visible to any script or function, other than the one in which they are embedded. This feature has impact on the use of function functions for such subfunctions. Consider the mass properties.m code:

function [mass, x_cm] = mass_properties(fname,a,b,sigma) % mass_properties(fname,a,b,sigma) evaluates the mass % and center-of-mass location for certain thin plates % % fname is a reference to a function that describes the % upper contour of the thin plate % a is the lower limit - the left edge of the plate % is bounded by x = a % b is the upper limit - the right edge of the plate % is bounded by x = b % The lower boundary is the portion of the x-axis % between x = a and x = b % sigma is the density of the plate (per unit area) % mass is the mass of the plate = sigma \int_a^b f(x) d x % x_cm is the x location of the center of mass % = \int_a^b x f(x) d x / mass % mass = sigmaquad(fname,a,b); fn_loc = @x_fx; x_cm = quad(fn_loc,a,b,[],[],fname); % % subfunctions go here % function xf = x_fx(x,fname) % x_fx(x,fname) evaluates the product of x and fname(x) xf = x.feval(fname,x);

In computing the mass we use quad to evaluate the integral of a user-specified function referenced by the dummy argument fname. The actual argument used to invoke mass properties can be supplied in any of the three ways enumerated in Section 1. In the second use of quad the required integrand is the product x · f (x), so we construct a subfunction (here x fx). Prior to Matlab 6 there was no way to reference such subfunctions for use by a function function. The work-around was to create a separate file that contained this function. With R 12 we use the function handle fn loc to refer to x fx. In this case the .type field returned by functions(fn loc) is subfunction.