Inline and Anonymous Functions in MATLAB
Last Updated :
28 Apr, 2025
Methods are also popularly known as functions. The main aim of the methods is to reuse the code. A method is a block of code that is invoked and executed when it is called by the user.
Inline Function
Inline functions are the kind of functions which is defined in one line. that's why this type of function is called the one-linear function. Inline functions are classified into two categories:
- In-Built Inline function
- User Defined Inline function
So let's take some examples to understand the inline functions. For the inline function we have to create a new script file then write the code inside that file and run that code.
Example 1:
Matlab
% MATLAB program for inline function.
clc;
close all;
Inline_function = @(x,y) (x^2 + y^2);
result = Inline_function(2,4)
Output:
In this example, we will represent the graphical(plot), figure. using the inline function. so here we create a function and take some variables that help to make a plot in MATLAB.
Example 2:
Matlab
% MATLAB program to draw the
% plot using inline function.
clc;
close all;
Anonymous_Function = @(d,t,w) (exp(d*t).* sin(w*t));
time = linspace(0,2,201);
frequency = 100;
omega = 2 * pi * frequency;
digit = -1;
P = Anonymous_Function(digit, time, omega);
plot(time,P)
Output:
Anonymous Function
An Anonymous function is a kind of MATLAB function that is not stored program file location. or an Anonymous function is a single line of executable expression function that can accept multiple inputs and return one output.
An anonymous function can contain a single-line executable statement. these functions are not stored in program files, but it is connected with a data type function_handle (@).
Use of Anonymous Function
Example 1:
- Suppose we have an equation
- y = x2 - 1
So we could write this equation as an anonymous function in MATLAB with the following line of code. But remember the Anonymous function don't need any new script file, directly we can write and execute in the command window.
Matlab
% MATLAB program for perform
% the Anonymous function.
y = @(x) x^2-1;
% get the x value.
y(2)
In the above code, we y=@(x), where the @ is identified as a function handle what it does In MATLAB it's a data type that stores an association to a function.
Output:
As we know that for the Anonymous function we don't need any program file we can execute this program in Command Window in MATLAB, as you can see in the below image.
If you will change just one line in the above code you will get a different output.
Matlab
% MATLAB program for perform
% the Anonymous function.
y = @(x) x^2-1;
% get the x value.
y(0)
Output:
Let's look into another Example -
Example 2:
Suppose in this example I want to perform the Anonymous function that takes just a single line of command for execution. So, here we are defining the function name as cube and defining the x variable.
Matlab
% MATLAB program for perform the Anonymous function.
Cube = @(x) x.^3;
Cube(15)
Using the above command line we will find the cube of any number just using the one-line Anonymous function.
Output:
And also if we want to find the cube of others' numbers Simultaneously then we have to give the command as following and then hit enter.
Matlab
% MATLAB program for perform the Anonymous function.
Cube = @(x) x.^3;
x = [3 5 7]
x =
3 5 7
Cube(x)
Output:
Example 3:
Anonymous function with multiple input and output-
Matlab
% MATLAB program for perform the Anonymous function.
Anonymous_Function = @(a,b) (a^2 + b^2 + a*b);
a = 2;
b = 20;
x = Anonymous_Function(a,b)
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Software Development Life Cycle (SDLC) Software development life cycle (SDLC) is a structured process that is used to design, develop, and test good-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read