MATLAB:Inline Function

From PrattWiki
Jump to: navigation, search

MATLAB has a command that lets you develop an analytical expression of one or more inputs and assign that expression to a variable. 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 creating an MATLAB:Anonymous Function with some significant differences.


The syntax is to put the expression to be evaluated in single quotes, followed in order by the variables of the function with each of these also surrounded by single quotes. For example, if you want c(a,b,\theta), to return \sqrt{a^2+b^2-2ab\cos(\theta)}, you could create an inline function as follows:

c = inline('sqrt(a.^2+b.^2-2*a.*b.*cos(theta))', 'a', 'b', 'theta')

MATLAB will respond with:

c =
     Inline function:
     c(a,b,theta) = sqrt(a.^2+b.^2-2*a.*b.*cos(theta))

indicating that the variable c is now actually an inline function object that takes three arguments. You can now use the function by putting numbers in for the arguments - for example:

SideThree = c(2, 3, pi/6)

will return

SideThree =

You can also use that function to return entire matrices. For example, the commands:

[x,y] = meshgrid(0:.1:2, 0:.1:2);
mesh(x, y, c(x, y, pi/4));
xlabel('Side 1');
ylabel('Side 2');
zlabel('Side 3');
title('Triangle Third Side vs. Sides Surrounding a 45^o Angle (mrg)')
print -depsc InlineExamplePlot

will produce the graph:


MATLAB Help File

The MATLAB help file for inline is[1]

INLINE Construct INLINE object.
    INLINE(EXPR) constructs an inline function object from the
    MATLAB expression contained in the string EXPR.  The input
    arguments are automatically determined by searching EXPR
    for variable names (see SYMVAR). If no variable exists, 'x'
    is used.
    INLINE(EXPR, ARG1, ARG2, ...) constructs an inline
    function whose input arguments are specified by the
    strings ARG1, ARG2, ...  Multicharacter symbol names may
    be used.
    INLINE(EXPR, N), where N is a scalar, constructs an
    inline function whose input arguments are 'x', 'P1',
    'P2', ..., 'PN'.
      g = inline('t^2')
      g = inline('sin(2*pi*f + theta)')
      g = inline('sin(2*pi*f + theta)', 'f', 'theta')
      g = inline('x^P1', 1)


  1. Inline functions cannot access variables in the workspace at any time, even if those variables are global. Assume that the space between the quotes in the first argument exists in its own special MATLAB universe. This is different from anonymous functions, in that anonymous functions can see the workspace at the time they are created.
  2. Inline functions can only have one expression and can only return a single variable (though that variable can be a matrix).



External Links


  1. Quoted from MATLAB help file for inline