Hauptinhalt

Substitute Values into Symbolic Expressions Using subs

The subs function in Symbolic Math Toolbox™ allows you to substitute values into symbolic expressions. The subs function is useful for evaluating, simplifying, and manipulating symbolic expressions. You can use subs to replace symbolic scalar variables, vectors, matrices, matrix variables, or systems of equations with specific numeric or symbolic values.

These examples show how to use subs for a variety of tasks, such as evaluating expressions at given values, simplifying results, verifying solutions to equations, assigning values to variables with different names, and substituting values into vectors and matrices.

Substitute Value into Expression

To evaluate a symbolic expression at a given value, substitute the value into the expression.

For example, create a symbolic expression using the symbolic variable x.

syms x
y = x*log(x) - 1;
expr = y/diff(y)
expr = 

xlog(x)-1log(x)+1

Evaluate the expression expr at x equal to 2. The result is an exact symbolic number.

expr_eval = subs(expr,x,2)
expr_eval = 

2log(2)-1log(2)+1

To convert the symbolic number to double precision, use the double function.

expr_double = double(expr_eval)
expr_double = 
0.2282

If you want to use variable-precision arithmetic, you can use vpa and digits. To change the output display format, use sympref. For more information, see Change Output Format of Symbolic and Variable-Precision Arithmetic.

Substitute and Automatically Simplify Result

When you substitute values in a symbolic expression, the subs function attempts to simplify the result.

For example, create a symbolic expression using the symbolic variables a, b, and x.

syms a b x
expr = (5*x - sqrt(3))/sqrt(a^2 + b^2) - x
expr = 

5x-3a2+b2-x

Substitute the variables a and b with the values 3 and 4, respectively.

expr_eval = subs(expr,[a b],[3 4])
expr_eval = 

-35

In this case, the result does not contain the variable x. The reason is that the subs function simplifies the evaluated expression by removing the terms with the variable x that cancel each other out.

Verify Solutions Using Substitution

You can solve a system of equations symbolically and then verify the solutions using substitution.

For example, create a system of two equations using the symbolic variables x, y, and a.

syms x y a
eqns = [x^2 + y^2 == a; x == y + a]
eqns = 

(x2+y2=ax=a+y)

Solve the equations for the variables x and y using the solve function.

sols = solve(eqns,[x y]);
sols.x
ans = 

(a2--aa-22a2+-aa-22)

sols.y
ans = 

(-a2--aa-22-aa-22-a2)

To verify the solutions, substitute the values in sols into the original system of equations.

eqns_eval = subs(eqns,sols)
eqns_eval = 

(σ1=aσ1=aσ2=σ2σ3=σ3)where  σ1=σ22+σ32  σ2=a2--aa-22  σ3=a2+-aa-22

Here, it is not obvious if the equalities hold true because the substituted expressions might require further simplification. To check if the equalities are always true, you can use the isAlways function. For more information, see Check Symbolic Equations, Inequalities, and Conditional Statements.

tf = isAlways(eqns_eval)
tf = 4×1 logical array

   1
   1
   1
   1

Use Different Variable Names During Substitution

When substituting values into symbolic expressions, use variables with names that are different from those in the original expression to assign specific values. This best practice helps avoid unintentionally replacing existing symbolic variables.

For example, create a symbolic function that includes the symbolic variables b and x.

syms b x
f(x) = 2*b^3*x^3 - b^2*x^2 + 3*b*x + 1
f(x) = 2b3x3-b2x2+3bx+1

To demonstrate the potentially confusing naming practice, assign the value 2 to b. The function f is still expressed in terms of both b and x.

b = 2;
f
f(x) = 2b3x3-b2x2+3bx+1

To substitute the assigned value of b into the function f, use subs(f).

f_eval = subs(f)
f_eval(x) = 16x3-4x2+6x+1

Because b has the value 2, any new function or expression you create using the variable b uses this assigned value.

g(x) = x + b
g(x) = x+2

Instead, the best practice is to assign the value to a new variable with a different name. For example, re-create the symbolic variables b and x, and the symbolic function f.

syms b x
f(x) = 2*b^3*x^3 - b^2*x^2 + 3*b*x + 1
f(x) = 2b3x3-b2x2+3bx+1

Specify a new variable bVal with the assigned value 2.

bVal = 2;

Substitute b in the function f with the value bVal.

f_eval = subs(f,b,bVal)
f_eval(x) = 16x3-4x2+6x+1

Now, if you create a new function or expression using the variable b, this variable still represents a symbolic variable.

g(x) = x + b
g(x) = b+x

Substitute Scalar Variables with Matrix Values

You can substitute symbolic scalar variables with matrix values in symbolic expressions. The subs function automatically expands the expressions and performs element-wise substitutions. If instead you want variables in a symbolic expression to accept matrix values, you can use symbolic matrix variables.

For example, create a symbolic expression using the symbolic scalar variables A, B, and t.

syms A B t
expr = B + exp(A*t)
expr = B+eAt

Substitute the variables A and B with 2-by-2 matrix values. To substitute these variables at the same time, specify the variables to substitute and their corresponding values as cell arrays. Here, subs automatically expands the original expression to a 2-by-2 size and performs element-wise substitution using the specified values.

Aval = [2 3; 1 2];
Bval = [0 1; 2 3];
expr_eval = subs(expr,{A B},{Aval Bval})
expr_eval = 

(e2te3t+1et+2e2t+3)

As an alternative, you can define A and B as symbolic matrix variables with a size of 2-by-2. Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. For more information, see syms and symmatrix.

syms A B [2 2] matrix
syms t
expr = B + exp(A*t)
expr = etA+B

Substitute the matrix variables A and B with 2-by-2 matrix values.

Aval = [2 3; 1 2];
Bval = [0 1; 2 3];
expr_eval = subs(expr,{A B},{Aval Bval})
expr_eval = 

etΣ1+Σ2where  Σ1=(2312)  Σ2=(0123)

When you use symbolic matrix variables of the symmatrix data type, the expression is displayed in compact matrix notation. To convert a symbolic matrix variable to a matrix of scalar values of the sym data type, use symmatrix2sym.

expr_eval = symmatrix2sym(expr_eval)
expr_eval = 

(e2te3t+1et+2e2t+3)

Substitute Multiple Vectors with Specified Values

You can substitute multiple vectors of symbolic scalar variables with their specified values by performing the substitutions one by one. To substitute multiple vectors at the same time, you can use symbolic matrix variables.

For example, create a symbolic expression using the arrays of symbolic scalar variables A, B, and t. Here, A is a 1-by-3 vector with elements [A1 A2 A3], and B is a 4-by-1 vector with elements [B1; B2; B3; B4].

syms A [1 3]
syms B [4 1]
syms t
C = [A t]*B
C = B4t+A1B1+A2B2+A3B3

Substitute A and B with their specified values. When you want to substitute multiple vectors of symbolic scalar variables, you must substitute one vector at a time. First, substitute the symbolic vector A.

Aval = [1 2 3];
Bval = [0; 1; 2; 3];
C_eval = subs(C,A,Aval)
C_eval = B1+2B2+3B3+B4t

Next, substitute the symbolic vector B.

C_eval = subs(C_eval,B,Bval)
C_eval = 3t+8

As an alternative, you can define A and B as symbolic matrix variables with sizes 1-by-3 and 4-by-1, respectively.

syms A [1 3] matrix
syms B [4 1] matrix
syms t
C = [A t]*B
C = (At)B

Substitute the matrix variables A and B with their specified values at the same time.

Aval = [1 2 3];
Bval = [0; 1; 2; 3];
C_eval = subs(C,{A B},{Aval Bval})
C_eval = 

(Σ1t)Σ2where  Σ1=(123)  Σ2=(0123)

Convert the symbolic matrix variables of type symmatrix to matrices of scalar values of type sym.

C_eval = symmatrix2sym(C_eval)
C_eval = 3t+8

See Also

| | | |

Topics