This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Create symbolic functions




f = symfun(formula,inputs) creates the symbolic function f. The symbolic variables inputs represent its input arguments. The symbolic expression formula defines the body of the function f.


Create Symbolic Functions

Use syms to create symbolic variables. Then use symfun to create a symbolic function with these variables as its input arguments.

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

Call the function for x = 1 and y = 2.

ans =

Related Examples

Input Arguments

collapse all

Function body, specified as a symbolic expression, vector of symbolic expressions, or matrix of symbolic expressions.

Example: x + y

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

Example: [x,y]

Output Arguments

collapse all

Function, returned as a symbolic function (symfun data type).

Alternative Functionality

Alternative Approaches for Creating Symbolic Functions

  • Use the assignment operation to simultaneously create a symbolic function and define its body. The arguments x and y must be symbolic variables in the MATLAB® workspace, and the body of the function must be a symbolic number, variable, or expression. Assigning a number, such as f(x,y) = 1, causes an error.

    syms x y
    f(x,y) = x + y
  • Use syms to create an abstract symbolic function f(x,y) and its arguments. The following command creates the symbolic function f and the symbolic variables x and y. Using syms, you also can create multiple symbolic functions in one function call.

    syms f(x,y)

More About

collapse all


  • When you replace one or more elements of a numeric vector or matrix with a symbolic number, MATLAB converts that number to a double-precision number.

    A = eye(3);
    A(1,1) = sym('pi')
    A =
        3.1416         0         0
             0    1.0000         0
             0         0    1.0000

    You cannot replace elements of a numeric vector or matrix with a symbolic variable, expression, or function because these elements cannot be converted to double-precision numbers. For example, syms f(t); A(1,1) = f throws an error.

  • Symbolic functions are always scalars, therefore, you cannot index into a function. To access x^2 and x^4 in this example, use formula to get the expression that defines f, and then index into that expression.

    syms x
    f = symfun([x^2, x^4], x);
    expr = formula(f);
    ans =
    ans =

Introduced in R2012a

Was this topic helpful?