Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- User Defined Functions in MATLAB Pa... by Shameer Ahmed Koya 786 views
- Matlab Script - Loop Control by Shameer Ahmed Koya 1049 views
- MATLAB Programming - Loop Control P... by Shameer Ahmed Koya 907 views
- User defined Functions in MATLAB Pa... by Shameer Ahmed Koya 1136 views
- User Defined Functions in MATLAB pa... by Shameer Ahmed Koya 883 views
- MATLAB Scripts - Examples by Shameer Ahmed Koya 1672 views

1,248 views

Published on

The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. It is similar to an Anonymous Function

Published in:
Education

No Downloads

Total views

1,248

On SlideShare

0

From Embeds

0

Number of Embeds

373

Shares

0

Downloads

18

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Anonymous and Inline Functions Shameer A Koyahttp://electricalenggtutorial.blogspot.com 1
- 2. • MATLAB's anonymous functions provide an easy way to specify a function. • An anonymous function is a function defined without using a separate function file • It is a MATLAB feature that lets you define a mathematical expression of one or more inputs and either assign that expression to a function. • This method is good for relatively simple functions that will not be used that often and that can be written in a single expression. http://electricalenggtutorial.blogspot.com 2
- 3. • An anonymous function of any number of variables can be created by giving the @ symbol, followed by a variable list, followed by the MATLAB expression. • Anonymous function can be written in Command Window, script file, or inside user-defined function. • It is similar to an Inline Function with some significant differences. • Anonymous functions can only have one expression and can only return a single variable http://electricalenggtutorial.blogspot.com 3
- 4. • To give the anonymous function a name, simply put the function's name on the left side of an equal sign and the anonymous function on the right. • NAME = @(ARGLIST)EXPRESSION • NAME is name of the function. (using rules for names of user- defined functions) • @ - a function handle, an object that has information about the function • ARGLIST is the input arguments (a comma-separated list). • The body of the function, to the right of the parentheses, is a single line MATLAB expression. • Execute the function by calling it by means of the given name same way as user-defined functions. http://electricalenggtutorial.blogspot.com 4
- 5. >> z = @(x, y) x.^3-y.^3 z = @(x, y) x.^3-y.^3 >> z( 3, 2) ans = 19 >> triarea = @(a, h) 1/2*a*h triarea = @(a, h) 1/2*a*h >> triarea(2,2) ans = 2 >> x = 5; >> y = 4; >> area = triarea(x, y) area = 10 http://electricalenggtutorial.blogspot.com 5
- 6. • An anonymous function expression can use a variable that have been defined before the function is defined. • Value of the variable when the function is defined will be used for evaluating the expression. • The changes made after the expression is defined will not be reflected in evaluating the function. • >> f = 5o; • >> sinewave = @(t) sin( 2*pi*f.*t ); • >> x=[0:0.001:0.2]; • >> y=sinewave(x); • >> plot(x,y) http://electricalenggtutorial.blogspot.com 6
- 7. • Anonymous function will be unaffected by any variable change after the function is defined >> a = 2; >> b = 3; >> sample = @(x,y) a*x.^2 + b*y^2; >> c = sample( 2, 1 ) c = 11 >> a = 5 a = 5 >> b = 0 b = 0 >> c = sample( 2, 1 ) c = 11 http://electricalenggtutorial.blogspot.com 7
- 8. • The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. • This method is good for relatively simple functions that will not be used that often and that can be written in a single expression. • It is similar to an Anonymous Function http://electricalenggtutorial.blogspot.com 8
- 9. • Name = inline(‘expression‘)creates an inline function object from the expression. • The input arguments are automatically determined by searching the expression for variable names. • If no variable exists, 'x' is used. • The expression to be evaluated is defined in single quotes, followed in order by the variables of the function also surrounded by single quotes. • Name = inline(‘expression', 'arg1', ‘arg2') http://electricalenggtutorial.blogspot.com 9
- 10. >> z = inline('x.^3-y.^3', ‘x', ‘y') z = Inline function: z(x,y) = 'x.^3-y.^3 >> z( 3, 2) ans = 19 >> sinewave=inline('sin(2*pi*f.*t)') sinewave = Inline function: sinewave(f,t) = sin(2*pi*f.*t) >> sinewave=inline('sin(2*pi*f.*t)','f','t'); http://electricalenggtutorial.blogspot.com 10

No public clipboards found for this slide

Be the first to comment