Function functions, MATLAB Programming

Function Functions:

The one reason for using function handles is to be able to pass functions to the other functions-these are known as function functions.

For illustration, let's say we have a function which generates an x vector. The y vector is generated by computing a function at each of the x points, and then such points are plotted.

 

1114_Function Functions.png

What we want is to be able to pass the function to be the value of funh, such as sin, cos, tan, and so forth. It doesn't work to merely pass the name of the function:

>> fnfnexamp(sin)

??? Error using ==> sin

Not enough input arguments.

 

Rather than, we have to pass the handle of the function:

>> fnfnexamp(@sin)

which generates the y vector as sin(x) and then brings up the plot as shown in figure.

 

753_Function Functions1.png

Passing rather the handle to the cos function would graph cosine rather than of sine:

>> fnfnexamp(@cos)

We can also pass the handle of any user-defined or anonymous function to the fnfnexamp function.

The other way of doing this is to use the built-in function str2func which will convert a string into a function handle:

 

2434_Function Functions2.png

This would also be called by passing a string to the function, and again would generate the similar plot:

>> fnstrfn2('sin')

There is also a function func2str which will convert a function handle into a string. This can be used to put the name of the function in the plot as shown in above figure.

Though these techniques also work, it seems easier and cleaner to merely pass the function handle to the function.

The MATLAB has few built-in function functions. The one built-in function is fplot that plots a function between the limits which are specified. The format of the call for fplot is:

fplot(fnhandle, [xmin xmax])

Posted Date: 10/22/2012 1:09:24 AM | Location : United States







Related Discussions:- Function functions, Assignment Help, Ask Question on Function functions, Get Answer, Expert's Help, Function functions Discussions

Write discussion on Function functions
Your posts are moderated
Related Questions
the cost t in hundreds of dollars of tuition and fees at many community college can be approximated by t=1/2c+5 where c is the number of credits for which a student registers. ( 2

This assignment is designed to compare performance between Matlab and Excel for performing nonlinear regression analysis of a set of data. There are two data sets in the accompa

Writing data to a File: The save function can be used to write a data from the matrix to the data file, or to append a data file. The format is as shown below: save filenam

Read the document and quote me ... https://www.dropbox.com/s/pvq4f45fpwajibi/Course_wk%20_2013_.pdf


how to design a digital fir filter using anfis in matlab.get the code for that.

An electric overhead traveling crane, consisting of a girder, trolley, and wire rope, is shown in figure. The girder has a flexural rigidity (EI) of 6x10 12 lb.in 2 and a span (L

Produce a random real number: To produce a random real number in the range from low to high, at first create the variables low and high. And then, use the expression rand*(hig

Introduction to File Input/output (LOAD and SAVE): In most cases, the input to a script will come from a data file which has been generated by the other source. It is also use

Solve the optimality condition for each P equation against M according to the following relation: Condition for Optimality:         ∇ M = λ ∇ P      with respect to C and T.