symfun
Create symbolic functions
Description
Examples
Create and Define Symbolic Functions
Define the symbolic function f(x,y) = x + y
. First, create the function by using syms
. Then define the function.
syms f(x,y)
f(x,y) = x + y
f(x, y) =
Find the value of f
at x = 1
and y = 2
.
f(1,2)
ans =
Define the function again by using the formal syntax.
syms x y f = symfun(x+y,[x y])
f(x, y) =
Return Body and Arguments of Symbolic Function
Return the body of a symbolic function by using formula
. You can use the body for operations such as indexing into the function. Return the arguments of a symbolic function by using argnames
.
Index into the symbolic function [x^2, y^4]
. Since a symbolic function is a scalar, you cannot directly index into the function. Instead, index into the body of the function.
syms f(x,y)
f(x,y) = [x^2, y^4];
fbody = formula(f);
fbody(1)
ans =
fbody(2)
ans =
Return the arguments of the function.
fvars = argnames(f)
fvars =
Combine Two Symbolic Functions
Create two symbolic functions.
syms f(x) g(x) f(x) = 2*x^2 - x; g(x) = 3*x^2 + 2*x;
Combine the two symbolic functions into another symbolic function with the data type symfun
.
h(x) = [f(x); g(x)]
h(x) =
Evaluate the function at and .
h(1)
ans =
h(2)
ans =
You can also combine the two functions into an array of symbolic expressions with the data type sym
.
h_expr = [f(x); g(x)]
h_expr =
Index into h_expr
to access the first and the second symbolic expressions.
h_expr(1)
ans =
h_expr(2)
ans =
Convert Symbolic Matrix Function to Symbolic Function
Since R2024b
Create 2
-by-1
and 2
-by-2
symbolic matrix variables to represent the matrices and .
syms X [2 1] matrix syms A [2 2] matrix
Create two symbolic matrix functions to represent the functions and . When creating the symbolic matrix functions, keep existing definitions of the symbolic matrix variables and in the workspace. The symbolic matrix functions require matrices of the same sizes as and as their input arguments.
syms F(X,A) [1 1] matrix keepargs syms dF(X,A) [2 1] matrix keepargs
Define the function and find its derivative . The resulting symbolic matrix functions are in matrix notation in terms of and .
F(X,A) = X.'*A*X
F(X, A) =
dF(X,A) = diff(F,X.')
dF(X, A) =
Convert the symbolic matrix functions from data type symfunmatrix
to symfun
. The resulting symbolic functions are in scalar notation in terms of the matrix elements of and . These functions accept scalars as their input arguments.
Fsymfun = symfun(F)
Fsymfun(X1, X2, A1_1, A1_2, A2_1, A2_2) =
dFsymfun = symfun(dF)
dFsymfun(X1, X2, A1_1, A1_2, A2_1, A2_2) =
Input Arguments
formula
— Function body
symbolic expression | vector of symbolic expressions | matrix of symbolic expressions
Function body, specified as a symbolic expression, vector of symbolic expressions, or matrix
of symbolic expressions that can be converted to the sym
data type.
Example: x + y
inputs
— Input argument or arguments of function
symbolic variable | array of symbolic variables
Input argument or arguments of a function, specified as a symbolic variable or array of symbolic variables, respectively.
Example: [x,y]
Data Types: sym
fM
— Symbolic matrix function to convert
symbolic matrix function
Since R2024b
Symbolic matrix function to convert, specified as a symbolic matrix function.
Alternatively, you can use symfunmatrix2symfun
to convert a symbolic matrix function to
a symbolic function.
Data Types: symfunmatrix
Output Arguments
f
— Symbolic function
symfun
object
Symbolic function, returned as a symfun
object.
While the data type of the function f
is
symfun
, the data type of the evaluated function, such
as f(1,2)
, is sym
.
Version History
Introduced in R2012aR2024b: Convert symbolic matrix function
You can convert a symbolic matrix function of type symfunmatrix
to a symbolic function of type symfun
by using
symfun
. For an example, see Convert Symbolic Matrix Function to Symbolic Function.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)