symvar

Find symbolic variables in symbolic input

Description

example

symvar(s) returns a vector of all symbolic variables in s. The variables are in alphabetical order with uppercase letters preceding lowercase letters.

example

symvar(s,n) chooses the n symbolic variables in s that are alphabetically closest to x and returns them in alphabetical order. If s is a symbolic function, symvar(s,n) returns the input arguments of s before other variables in s.

Examples

collapse all

Find all symbolic variables in an expression. symvar returns the variables in alphabetical order.

syms wa wb yx ya
sum = wa + wb + ya + yx;
symvar(sum)
ans =
[ wa, wb, ya, yx]

Find the first three symbolic variables in an expression. symvar chooses variables that are alphabetically closest to x and returns them in alphabetical order.

syms a x y b
f = a*x^2/(sin(3*y-b));
symvar(f,3)
ans =
[ b, x, y]

Find all symbolic variables in this function. For a symbolic function, symvar returns the function inputs before other variables.

syms x y a b
f(x,y) = a*x^2/(sin(3*y-b));
symvar(f)
ans =
[ x, y, a, b]
g(x,y) = 1;
symvar(g)
ans =
[ x, y]

Find the first three symbolic variables in f.

symvar(f,3)
ans =
[ x, y, b]

When a symbolic function, such as solve, needs to find the default independent variable in symbolic, the function uses symvar. Find the default independent variable for symbolic expressions.

syms v z
g = v + z;
symvar(g,1)
ans =
z
syms aaa aab
g = aaa + aab;
symvar(g,1)
ans =
aaa
syms X1 x2 xa xb
g = X1 + x2 + xa + xb;
symvar(g,1)
ans =
x2

When differentiating, integrating, substituting, or solving equations, MATLAB® uses the variable returned by symvar(s,1) as a default variable. For a symbolic expression or matrix, symvar(s,1) returns the variable closest to x. For a function, symvar(s,1) returns the first input argument of s.

Input Arguments

collapse all

Input, specified as a number, vector, matrix, or array, or a symbolic number, variable, array, function, or expression.

Number of variables, specified as an integer or Inf. If n exceeds the number of variables in s, then symvar returns the number of variables in s.

Tips

  • symvar treats the constants pi, i, and j as variables.

  • If there are no symbolic variables in s, symvar returns the empty vector.

Algorithms

When symvar sorts the symbolic variables alphabetically, all uppercase letters have precedence over lowercase: 0 1 ... 9 A B ... Z a b ... z.

Introduced in R2008b