Chapter3.5

# Chapter3.5 - Anonymous functions allow us to create simple...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Anonymous functions allow us to create simple functions without using Mfile. The format is [email protected](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 >> [email protected](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 >> [email protected](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 >> [email protected](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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern