
Inline functions in Octave
In this lesson, I will explain how to create an inline function in Octave.
An inline function is a function of one or more variables, f(x), defined to quickly perform mathematical calculations. It is called an inline function because it is defined on a single line.
To create an inline function, write the function name, followed by the equals sign (=) and the inline() instruction. Put the mathematical expression of the function in parentheses.
function_name = inline("mathematical_expression")
Let's take a look at a practical example.
If you want to create a function f(x,y) = x2 + y2, you can do so by typing:
>> f=inline("x^2+y^2")
This command creates a function of two variables
$$ f(x,y) = x^2+y^2 $$
Note that in this case, the function is named "f," but any other name would work.
Now you can use the function you just created to perform calculations. For example, type f(2,3), where x=2 and y=3.
>> f(2,3)
The output is 13.
ans=13
This is because
$$ f(2,3) = 2^3+3^2 = 4 + 9 = 13 $$
Now type f(3,4) and press enter.
>> f(3,4)
The result is 25 because f(3,4)=32+42=9+16=25
ans=25
You can also create an inline function by creating an anonymous function.
For example, type:
>> g=@(x,y) x^2+y^2
This command creates an anonymous function, g=x2+y2
Now use the anonymous function g to perform calculations. For example, type g(2,3).
>> g(2,3)
The function g(x,y) takes the parameters x=2 and y=3 and returns the result.
ans = 13
This is another way to create an inline function.