Method on @symfun: f = symfun (expr, vars)

Define a symbolic function (not usually invoked directly).

A symfun can be abstract or concrete. An abstract symfun represents an unknown function (for example, in a differential equation). A concrete symfun represents a known function such as f(x) = sin(x).

A concrete symfun:

syms x
f(x) = sin(x)
  ⇒ f(x) = (symfun) sin(x)
f
  ⇒ f(x) = (symfun) sin(x)
f(1)
  ⇒ ans = (sym) sin(1)
f(x)
  ⇒ ans = (sym) sin(x)

An abstract symfun:

syms g(x)
g
  ⇒ g(x) = (symfun) g(x)

(Note this creates the sym x automatically if it does not already exist.)

Example: multivariable symfuns:

syms x y
g(x, y) = 2*x + sin(y)
  ⇒ g(x, y) = (symfun) 2⋅x + sin(y)
syms g(x, y)
g
  ⇒ g(x, y) = (symfun) g(x, y)

Example: creating an abstract function formally of two variables but depending only on x:

syms x y h(x)
h(x, y) = h(x)
  ⇒ h(x, y) = (symfun) h(x)

A symfun can be composed inside another. For example, to demonstrate the chain rule in calculus, we might do:

syms f(t) g(t)
F(t) = f(g(t))
  ⇒ F(t) = (symfun) f(g(t))
diff(F, t)
  ⇒ ans(t) = (symfun)
        d            d
      ─────(f(g(t)))⋅──(g(t))
      dg(t)          dt

It is possible to create an abstract symfun without using the syms command:

x = sym('x');
g(x) = sym('g(x)')
  ⇒ g(x) = (symfun) g(x)

(note the x must be included on the left-hand side.) However, syms is safer because this can fail or give unpredictable results for certain function names:

beta(x) = sym('beta(x)')
  -| ??? ... Error ...

whereas on more recent SymPy we get

beta(x) = sym('beta(x)')
  ⇒ beta(x) = (symfun) Β(x, x)

in either case, we’re not getting an abstract symfun but rather the beta function (see ‘@sym/beta’).

It is usually not necessary to call symfun directly but it can be done:

f = symfun(x*sin(y), [x y])
  ⇒ f(x, y) = (symfun) x⋅sin(y)
g = symfun(sym('g(x)'), x)
  ⇒ g(x) = (symfun) g(x)

See also: sym, syms.

Package: symbolic