Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

symfun

Create symbolic functions

Syntax

Description

example

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.

Examples

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.

f(1,2)
ans =
3

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

Tips

  • 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);
    
    expr(1)
    expr(2)
    ans =
    x^2
     
    ans =
    x^4

Introduced in R2012a

Was this topic helpful?