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

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

Related documents


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. 1 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 = 3.14159264760639 g = inline(’sin(x).*x.^P1’,1) g = Inline function: g(x,P1) = sin(x).*x.^P1 quad(g,0,pi,[],[],1) ans = 3.14159264760639 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_esm2074 \lectures\lecture_17\sin_lim.m <== line folded for printing fname = ’sin_lim’ fname = sin_lim quad(fname,0,pi) ans = 2.57078141191146 2
Docsity logo



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