Main Content

symfun

Create symbolic functions

Description

f(inputs) = formula creates the symbolic function f. For example, f(x,y) = x + y. The symbolic variables in inputs are the input arguments. The symbolic expression formula is the body of the function f.

example

f = symfun(formula,inputs) is the formal way to create a symbolic function.

example

f = symfun(fM) converts a symbolic matrix function fM of type symfunmatrix to a symbolic function f of type symfun. (since R2024b)

example

Examples

collapse all

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) = x+y

Find the value of f at x = 1 and y = 2.

f(1,2)
ans = 3

Define the function again by using the formal syntax.

syms x y
f = symfun(x+y,[x y])
f(x, y) = x+y

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 = x2
fbody(2)
ans = y4

Return the arguments of the function.

fvars = argnames(f)
fvars = (xy)

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 h(x) with the data type symfun.

h(x) = [f(x); g(x)]
h(x) = 

(2x2-x3x2+2x)

Evaluate the function h(x) at x=1 and x=2.

h(1)
ans = 

(15)

h(2)
ans = 

(616)

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 = 

(2x2-x3x2+2x)

Index into h_expr to access the first and the second symbolic expressions.

h_expr(1)
ans = 2x2-x
h_expr(2)
ans = 3x2+2x

Since R2024b

Create 2-by-1 and 2-by-2 symbolic matrix variables to represent the matrices X and A.

syms X [2 1] matrix
syms A [2 2] matrix

Create two symbolic matrix functions to represent the functions F(X,A) and F(X,A)/XT. When creating the symbolic matrix functions, keep existing definitions of the symbolic matrix variables X and A in the workspace. The symbolic matrix functions require matrices of the same sizes as X and A as their input arguments.

syms F(X,A) [1 1] matrix keepargs
syms dF(X,A) [2 1] matrix keepargs

Define the function F(X,A)=XTAX and find its derivative F(X,A)/XT. The resulting symbolic matrix functions are in matrix notation in terms of X and A.

F(X,A) = X.'*A*X
F(X, A) = XTAX
dF(X,A) = diff(F,X.')
dF(X, A) = AX+ATX

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 X and A. These functions accept scalars as their input arguments.

Fsymfun = symfun(F)
Fsymfun(X1, X2, A1_1, A1_2, A2_1, A2_2) = X1A1,1X1+A1,2X2+X2A2,1X1+A2,2X2
dFsymfun = symfun(dF)
dFsymfun(X1, X2, A1_1, A1_2, A2_1, A2_2) = 

(2A1,1X1+A1,2X2+A2,1X2A1,2X1+A2,1X1+2A2,2X2)

Input Arguments

collapse all

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

Input argument or arguments of a function, specified as a symbolic variable or array of symbolic variables, respectively.

Example: [x,y]

Data Types: sym

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

collapse all

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 R2012a

expand all