Difference between revisions of "MATLAB:Inline Function"
m |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 21: | Line 21: | ||
indicating that the variable <code>c</code> 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: | indicating that the variable <code>c</code> 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: | ||
<source lang="matlab"> | <source lang="matlab"> | ||
− | + | SideThree = c(2, 3, pi/6) | |
</source> | </source> | ||
will return | will return | ||
<source lang="text"> | <source lang="text"> | ||
− | + | SideThree = | |
1.6148 | 1.6148 | ||
</source> | </source> | ||
Line 36: | Line 36: | ||
ylabel('Side 2'); | ylabel('Side 2'); | ||
zlabel('Side 3'); | zlabel('Side 3'); | ||
− | title('Triangle Third Side vs. Sides | + | title('Triangle Third Side vs. Sides Surrounding a 45^o Angle (mrg)') |
− | print - | + | print -depsc InlineExamplePlot |
</source> | </source> | ||
will produce the graph: | will produce the graph: | ||
Line 85: | Line 85: | ||
<references /> | <references /> | ||
− | [[Category:EGR | + | [[Category:EGR 103]] |
Latest revision as of 20:30, 8 June 2012
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.
Syntax
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 =
1.6148
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'.
Examples:
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)
Notes
- 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.
- Inline functions can only have one expression and can only return a single variable (though that variable can be a matrix).
Questions
Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.
External Links
- MATLAB Function Reference: inline, The MathWorks
References
- ↑ Quoted from MATLAB help file for
inline