SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Anonymous
and Inline
Functions
Shameer A Koyahttp://electricalenggtutorial.blogspot.com 1
• 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
• 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
• 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
>> 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
• 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
• 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
• 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
• 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
>> 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

Weitere ähnliche Inhalte

Was ist angesagt?

4 pages from matlab an introduction with app.-2
4 pages from matlab an introduction with app.-24 pages from matlab an introduction with app.-2
4 pages from matlab an introduction with app.-2
Malika khalil
 
5.3 areas, riemann sums, and the fundamental theorem of calaculus
5.3 areas, riemann sums, and the fundamental theorem of calaculus5.3 areas, riemann sums, and the fundamental theorem of calaculus
5.3 areas, riemann sums, and the fundamental theorem of calaculus
math265
 

Was ist angesagt? (20)

MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...
MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...
MODELLING, ANALYSIS AND SIMULATION OF DYNAMIC SYSTEMS USING CONTROL TECHNIQUE...
 
1 review on derivatives
1 review on derivatives1 review on derivatives
1 review on derivatives
 
EM3 mini project Laplace Transform
EM3 mini project Laplace TransformEM3 mini project Laplace Transform
EM3 mini project Laplace Transform
 
4 pages from matlab an introduction with app.-2
4 pages from matlab an introduction with app.-24 pages from matlab an introduction with app.-2
4 pages from matlab an introduction with app.-2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
16 partial fraction decompositions x
16 partial fraction decompositions x16 partial fraction decompositions x
16 partial fraction decompositions x
 
Dcs lec02 - z-transform
Dcs   lec02 - z-transformDcs   lec02 - z-transform
Dcs lec02 - z-transform
 
Seminar on MATLAB
Seminar on MATLABSeminar on MATLAB
Seminar on MATLAB
 
Matlab
MatlabMatlab
Matlab
 
Modelos matematicos de sistemas
Modelos matematicos de sistemasModelos matematicos de sistemas
Modelos matematicos de sistemas
 
Laplace transform and its applications
Laplace transform and its applicationsLaplace transform and its applications
Laplace transform and its applications
 
MATLAB Scripts - Examples
MATLAB Scripts - ExamplesMATLAB Scripts - Examples
MATLAB Scripts - Examples
 
Over view of Laplace Transform and its Properties
Over view of Laplace Transform and its Properties Over view of Laplace Transform and its Properties
Over view of Laplace Transform and its Properties
 
Power power electronics (solution manual) by M.H.Rashid.pdf
Power power electronics (solution manual) by M.H.Rashid.pdfPower power electronics (solution manual) by M.H.Rashid.pdf
Power power electronics (solution manual) by M.H.Rashid.pdf
 
Modern control engineering 5th ed solution manual (2010)
Modern control engineering 5th ed solution manual (2010)Modern control engineering 5th ed solution manual (2010)
Modern control engineering 5th ed solution manual (2010)
 
Note on fourier transform of unit step function
Note on fourier transform of unit step functionNote on fourier transform of unit step function
Note on fourier transform of unit step function
 
5.3 areas, riemann sums, and the fundamental theorem of calaculus
5.3 areas, riemann sums, and the fundamental theorem of calaculus5.3 areas, riemann sums, and the fundamental theorem of calaculus
5.3 areas, riemann sums, and the fundamental theorem of calaculus
 
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
 
Laplace transform
Laplace transformLaplace transform
Laplace transform
 
Laplace transformation
Laplace transformationLaplace transformation
Laplace transformation
 

Andere mochten auch

Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
TUOS-Sam
 
Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]
azroyyazid
 

Andere mochten auch (20)

User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
 
Learning C# (Vietnamese)
Learning C# (Vietnamese)Learning C# (Vietnamese)
Learning C# (Vietnamese)
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output CommandsMATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Matlab time series example
Matlab time series exampleMatlab time series example
Matlab time series example
 
Introduction to Matlab Scripts
Introduction to Matlab ScriptsIntroduction to Matlab Scripts
Introduction to Matlab Scripts
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
 
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Circuit analysis i with matlab computing and simulink sim powersystems modelingCircuit analysis i with matlab computing and simulink sim powersystems modeling
Circuit analysis i with matlab computing and simulink sim powersystems modeling
 
Do While and While Loop
Do While and While LoopDo While and While Loop
Do While and While Loop
 
Cruise control simulation using matlab
Cruise control simulation using matlabCruise control simulation using matlab
Cruise control simulation using matlab
 
Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]Reduction of multiple subsystem [compatibility mode]
Reduction of multiple subsystem [compatibility mode]
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Matlab solving rlc circuit
Matlab solving rlc circuitMatlab solving rlc circuit
Matlab solving rlc circuit
 

Ähnlich wie Anonymous and Inline Functions in MATLAB

Ähnlich wie Anonymous and Inline Functions in MATLAB (20)

Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Java 8
Java 8Java 8
Java 8
 
Functions
FunctionsFunctions
Functions
 
MatlabIntro (1).ppt
MatlabIntro (1).pptMatlabIntro (1).ppt
MatlabIntro (1).ppt
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
 
Mbd dd
Mbd ddMbd dd
Mbd dd
 
DataStructures.pptx
DataStructures.pptxDataStructures.pptx
DataStructures.pptx
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
Java8
Java8Java8
Java8
 
Matlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - IMatlab Tutorial for Beginners - I
Matlab Tutorial for Beginners - I
 
Python functions
Python functionsPython functions
Python functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Anonymous and Inline Functions in MATLAB

  • 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