Function functions, MATLAB Programming

Assignment Help:

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])


Related Discussions:- Function functions

Aquifer simulation of the groundwater, An environmental consulting firm is ...

An environmental consulting firm is conducting a site investigation on an abandoned industrial site that is 200 m by 150 m in size (Fig. 1). A number of piezometers were installed

Function char - characters and encoding, Function char: The function c...

Function char: The function char does the opposite; it converts from any number type to the type char: >> char(numequiv) ans = a As the letters of the alphabet are

Compare performance between matlab and excel, This assignment is designed t...

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

Illustrations of if statement, Illustrations of if statement: illustra...

Illustrations of if statement: illustrations of running this script: >> sqrtifexamp Please enter a number: -4.2 The sqrt of 4.2 is 2.0 >> sqrtifexamp Please ent

Variable number of input arguments - function, Variable number of input arg...

Variable number of input arguments: For illustration, the below function areafori has a variable number of input arguments, either the 1 or 2. The name of the function stands

Missing commands, hey ! why the command sawtooth and square does not exist ...

hey ! why the command sawtooth and square does not exist in Matlab R2012a?

Linspace function, Linspace function: Likewise, the linspace function ...

Linspace function: Likewise, the linspace function generates a linearly spaced vector; linspace(x,y,n) generates a vector with n values in the inclusive range from x to y. For

Example of variables and assignment statements, Example of Variables and as...

Example of Variables and assignment statements: The expression is computed and then that value is stored in the variable. For illustration, this is the way it would appear in

Find the volume if pyramid, Write a script to determine the volume of a pyr...

Write a script to determine the volume of a pyramid, which is 1/3 * base * height, where the base is length * width. On time the user to enter values for the length, width, and th

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd