This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Anonymous functions allow us to create simple functions without using Mfile. The format is Fhandle=@(arglist) expression For example, assume that you need to calculate the derivative of arcsin(x) for different values of x and you do not remember the analytical formula. So you will calculate it by finite differences Eq. (4.23) f'(x) is approximately equal to (f(x+h)f(x))/h. As we will learn in Chapter 4, this formula is not very accurate for large value of the increment x or for very small values. So we will try different values of h and look for increments where the result is not very sensitive to h. For that we will define the anonymous function >> asindif=@(x,h) (asin(x+h)asin(x))/h asindif = @(x,h) (asin(x+h)asin(x))/h Then we use it for a few values of h >> asindif(0.5,0.1) ans = 1.1990 >> asindif(0.5,0.001) ans = 1.1551 >> asindif(0.5,1e5) ans = 1.1547 >> asindif(0.5,1e13) ans = 1.1546 >> asindif(0.5,1e15) ans = 1.1102 Function functions are functions that operate on other functions and that pass the name of the function in their argument list. One of the most useful builtin function functions is fplot(fun, lims) that plots the function fun in the interval defined by lims. Based on the previous study, we could use an interval of 1.e5 for the finite difference step size and define the approximate derivative based on this interval >> asinder=@(x) asindif(x,1.e5) asinder = @(x) asindif(x,1.e5) We could then plot the derivative in the interval [0.5 0.5] as >> fplot(asinder,[0.5,0.5]) >> xlabel('x') >>ylabel('d/dx arcsin(x)') To get 1.16 1.14 1.12 1.1 1.08 1.06 1.04 1.02 1 0.5 d/dx arcsin(x) 0.4 0.3 0.2 0.1 0 x 0.1 0.2 0.3 0.4 0.5 If instead of differentiating the arcsin function we wanted to differentiate a more general function we may consider the fact that this function may have other arguments besides x. To take care of that we can use the varargin (stands for variable arguments in) structure. It allows us to transmit arguments from the calling function to the function that will be called from the function function. So let us say we want to construct a general function that performs forward differences. So we create the following Mfile: function der=forward(f,x,h,varargin) %Calculates the derivative of the function f with respect to x using a %forward difference approximation with step size h. varargin includes %variables used in the call to f der=(f(x+h,varargin{:})f(x,varargin{:}))/h; The varargin indicates that we can specify any number of arguments when we call der, and they will be passed to the function f. For example, lets say that we wanted to calculate the derivative of the function arcsin(bx), so that we would need to transmit the parameter b. We first define arcsin(bx) as an anonymous function >> asinb=@(b,x) asin(b*x) asinb = @(x,b) asin(b*x) Now we can call it der=forward(asinb,0.5,1.e5,1) der = 1.1547 Or >> der=forward(asinb,0.5,1.e5,0.5) der = 0.5164 Or >> der=forward(asinb,0.5,1.e5,2.5) der = 0 3.3332i Why did we get an imaginary result? If we now to evaluate the derivative for a range of x values, we can do it as follows: >> x=0.5:0.1:0.5; der=forward(asinb,x,1.e5,1) der = 1.1547 1.0911 1.0483 1.0206 1.0050 1.0000 1.0050 1.0206 1.0483 1.0911 1.1547 Which could generate the figure that we previously generated with the fplot command. ...
View Full
Document
 Spring '09
 RAPHAELHAFTKA

Click to edit the document details