Contenu principal

Pattern-Based Substitution

R2026b
Since R2026b

Pattern-based substitution lets you rewrite or simplify symbolic expressions by matching and replacing specific expression patterns. Unlike general-purpose functions such as simplify or rewrite, pattern-based substitution with the subs function replaces terms only where an explicit pattern matches.

With pattern-based substitution, you can:

  • Replace specific parts of an expression that match a pattern.

  • Define conditions that control when to replace a matching term.

  • Apply mathematical identities based on how expressions are written.

  • Define and apply your own identities.

These capabilities are useful when you want more control over how expressions are rewritten or when the built-in simplification rules in Symbolic Math Toolbox™ are too general for your calculations.

Replace Specific Parts of Expression

Pattern-based substitution with the subs function lets you replace only the specific parts of an expression that match a pattern.

For example, create a symbolic expression that includes sine and cosine terms.

syms x
expr = 2*sin(x) + 3*cos(x)
expr = 3 cos(x)+2 sin(x)

Rewrite the original expression in terms of the exponential function by using the rewrite function. The function replaces the sine and cosine terms with their exponential forms.

exprnew = rewrite(expr,"exp")
exprnew = 

e-x i 32+i+ex i 32-i

By contrast, replace only the sine terms, leaving the cosine terms unchanged. First, create a symbolic variable to use as a wildcard, and define the pattern to match and its replacement.

syms w
match = sin(w)
match = sin(w)
replacement = rewrite(match,"exp")
replacement = 

e-w i i2-ew i i2

Perform pattern-based substitution using the subs function. The function replaces the sine term with its exponential form, while preserving the cosine term.

exprnew = subs(expr,match,replacement,Wildcards=w)
exprnew = 3 cos(x)+e-x i i-ex i i

Define Condition to Replace Subexpressions

Using pattern-based substitution with the subs function, you can define a condition that controls when to replace a matched term.

For example, create a symbolic expression.

syms x
expr = sin(x) + sym(pi) - sym(3)
expr = π+sin(x)-3

Define a condition for the wildcard to match any values in the interval (0,0.5).

syms w
condition = @(w) (0 < w) & (w < 0.5);

Replace the subexpressions in expr that satisfy this condition by using pattern-based substitution. The subs function performs as many independent replacements as possible on the matching subexpressions, starting with the smallest matching subexpressions. In this case, subs replaces the matching subexpression π-3 with y/2.

syms y
exprnew = subs(expr,w,y/2,Wildcards=w,Condition=condition)
exprnew = 

y2+sin(x)

For comparison, define another condition that matches values in the interval [-1,1]. Replace the subexpressions in expr that satisfy this condition by using pattern-based substitution. In this case, subs replaces the matching subexpression π-3, but not the term sin(x). The reason is that, by default, the symbolic variable x is complex, and therefore sin(x) does not necessarily lie within the specified interval.

condition = @(w) (-1 <= w) & (w <= 1);
exprnew = subs(expr,w,y/2,Wildcards=w,Condition=condition)
exprnew = 

y2+sin(x)

Assume that x is real. With this assumption, both π-3 and sin(x) satisfy the condition, so subs replaces both with y/2, resulting in the output y.

assume(x,"real")
exprnew = subs(expr,w,y/2,Wildcards=w,Condition=condition)
exprnew = y

Apply Mathematical Identities

You can use pattern-based substitution with the subs function to apply a specific mathematical identity only to matching patterns.

For example, create two symbolic expressions that involve trigonometric functions. Although written differently, these expressions are mathematically identical.

syms x y
expr1 = (sin(x)+cos(x))^2
expr1 = cos(x)+sin(x)2
expr2 = expand((sin(x)+cos(x))^2)
expr2 = cos(x)2+2 cos(x) sin(x)+sin(x)2

Simplify the original expressions by using the simplify function. The resulting expressions are identical.

expr1new = simplify(expr1)
expr1new = sin(2 x)+1
expr2new = simplify(expr2)
expr2new = sin(2 x)+1

By contrast, use pattern-based substitution to apply the identity sin2(θ)+cos2(θ)=1 to both expressions. First, create a symbolic variable to use as a wildcard, and define the pattern to match and its replacement.

syms w
match = sin(w)^2 + cos(w)^2;
replacement = 1;

Perform pattern-based substitution using the subs function on both expressions. The function applies the identity only to the second expression because it contains an exact match to the specified pattern.

expr1new = subs(expr1,match,replacement,Wildcards=w)
expr1new = cos(x)+sin(x)2
expr2new = subs(expr2,match,replacement,Wildcards=w)
expr2new = 2 cos(x) sin(x)+1

Define and Apply Your Own Identities

You can use pattern-based substitution with special-function identities to rewrite symbolic expressions. The identities used in this example relate the Airy functions to the modified Bessel function of the first kind, as documented in the NIST Digital Library of Mathematical Functions (DLMF), Section 9.6(i).

First, create a symbolic variable w, which is complex by default.

syms w

Define patterns for the Airy function of the first kind Ai(w) and the Airy function of the second kind Bi(w) using the symbolic airy function.

match_Ai(w) = airy(0,w)
match_Ai(w) = airy(0,w)
match_Bi(w) = airy(2,w)
match_Bi(w) = airy(2,w)

Define the auxiliary variable and the corresponding Bessel-function representations of the Airy functions.

g = 2/3*w^(3/2);
replacement_Ai(w) = w^(1/2)/3*(besseli(-1/3,g) - besseli(1/3,g))
replacement_Ai(w) = 

w Ibesseli-13(2 w3/23)-Ibesseli13(2 w3/23)3

replacement_Bi(w) = (w/3)^(1/2)*(besseli(1/3,g) + besseli(-1/3,g))
replacement_Bi(w) = 

Ibesseli-13(2 w3/23)+Ibesseli13(2 w3/23) w3

Verify that these identities are true by using isAlways. Because Symbolic Math Toolbox does not include all possible relationships between special functions, isAlways cannot prove that these identities are true.

isAlways(match_Ai==replacement_Ai)
Warning: Unable to prove 'airy(0, w) == (w^(1/2)*(besseli(-1/3, (2*w^(3/2))/3) - besseli(1/3, (2*w^(3/2))/3)))/3'.
ans = logical
   0

However, you can verify the identities numerically by evaluating both sides at selected values. For example:

val_Ai = vpa(match_Ai(2.567))
val_Ai = 0.0141
val_Ai = vpa(replacement_Ai(2.567))
val_Ai = 0.014
val_Bi = vpa(match_Bi(2.567))
val_Bi = 7.15
val_Bi = vpa(replacement_Bi(2.567))
val_Bi = 7.15

Next, apply these identities to the symbolic solution of a differential equation. Define the differential equation and solve it.

syms y(x)
eqn = diff(y,2) - x*y == 0
eqn(x) = 

∂2∂x2 y(x)-x y(x)=0

sol = dsolve(eqn)
sol = C1 airy(0,x)+C2 airy(2,x)

Use pattern-based substitution to replace the Airy functions with their Bessel-function representations, and then simplify the result.

solnew = subs(sol,match_Ai,replacement_Ai,Wildcards=w);
solnew = subs(solnew,match_Bi,replacement_Bi,Wildcards=w);
solnew = simplify(solnew)
solnew = 

x Ibesseli-13(2 x3/23) C1+3 C23-x Ibesseli13(2 x3/23) C1-3 C23

Next, define a higher-order differential equation with initial conditions, and solve the equation.

eqn = diff(y,3) - 4*x*diff(y) - 2*y == 0
eqn(x) = 

∂3∂x3 y(x)-4 x ∂∂x y(x)-2 y(x)=0

Dy = diff(y,x);
D2y = diff(y,x,x);
cond = [y(0)==0, Dy(0)==1, D2y(0)==0];
sol = dsolve(eqn,cond)
sol = 

π 3 airy(2,x)26-π 3 airy(0,x)22

Apply the same pattern-based substitutions and simplify the result. The final expression is written in terms of the modified Bessel function of the first kind.

solnew = subs(sol,match_Ai,replacement_Ai,Wildcards=w);
solnew = subs(solnew,match_Bi,replacement_Bi,Wildcards=w);
solnew = simplify(solnew)
solnew = 

2 π 3 x Ibesseli-13(2 x3/23) Ibesseli13(2 x3/23)9

See Also

Functions

Topics