Substitute with subs

Use this code to find the eigenvalues and eigenvectors of a circulant matrix A:

syms a b c
A = [a b c; b c a; c a b];
[v,E] = eig(A)
v =
[ - (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2)/(a - c) - (a - b)/(a - c),...
    (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2)/(a - c) - (a - b)/(a - c), 1]
[   (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2)/(a - c) - (b - c)/(a - c),...
  - (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2)/(a - c) - (b - c)/(a - c), 1]
[    1,                                1,                                1]
 
E =
[ (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2),                                          0,         0]
[                                         0, -(a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2),         0]
[                                         0,                                          0, a + b + c]
 

    Note   MATLAB® might return the eigenvalues that appear on the diagonal of E in a different order. In this case, the corresponding eigenvectors, which are the columns of v, also appear in a different order.

Replace the rather lengthy expression (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2) throughout v and E:

syms S
v = subs(v,(a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2), S)
E = subs(E,(a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2), S)
v =
[ - S/(a - c) - (a - b)/(a - c),   S/(a - c) - (a - b)/(a - c), 1]
[   S/(a - c) - (b - c)/(a - c), - S/(a - c) - (b - c)/(a - c), 1]
[                             1,                             1, 1]
 
E =
[ S,  0,         0]
[ 0, -S,         0]
[ 0,  0, a + b + c]

Simplify v:

v = simplify(v)
v =
[ -(S + a - b)/(a - c),  (S - a + b)/(a - c), 1]
[  (S - b + c)/(a - c), -(S + b - c)/(a - c), 1]
[                    1,                    1, 1]

Note that subs does not assign (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2) to S:

S
S =
S

Assign this expression to S:

S = (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2);

Substitute variables a, b, and c in S with the values 1, 2, and 3:

subs(S, {a, b, c}, {1, 2, 3})
ans =
3^(1/2)

Substitute a, b, and c in v with the same values. Note that you must call subs twice. The first call, subs(v), replaces S in v with the expression (a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2). The second call replaces the variables a, b, and c in v with the values 1, 2, and 3:

subs(subs(v), {a, b, c}, {1, 2, 3})
ans =
[   3^(1/2)/2 - 1/2, - 3^(1/2)/2 - 1/2, 1]
[ - 3^(1/2)/2 - 1/2,   3^(1/2)/2 - 1/2, 1]
[                 1,                 1, 1]

These substitutions do not modify a, b, c, S, and v:

[a, b, c]
ans =
[ a, b, c]
S
S =
(a^2 - a*b - a*c + b^2 - b*c + c^2)^(1/2)
v
v =
[ -(S + a - b)/(a - c),  (S - a + b)/(a - c), 1]
[  (S - b + c)/(a - c), -(S + b - c)/(a - c), 1]
[                    1,                    1, 1]

To modify the original values S and v, assign the results returned by subs to S and v. This approach does not modify a, b, and c.

S = subs(S, {a, b, c}, {1, 2, 3})
S =
3^(1/2)
v = subs(subs(v), {a, b, c}, {1, 2, 3})
v =
[   3^(1/2)/2 - 1/2, - 3^(1/2)/2 - 1/2, 1]
[ - 3^(1/2)/2 - 1/2,   3^(1/2)/2 - 1/2, 1]
[                 1,                 1, 1]

Alternatively, you can assign values to the variables a, b, and c:

a = 1;
b = 2;
c = 3;

The new values of a, b, and c now exist in the MATLAB workspace:

[a, b, c]
ans =
     1     2     3

Use subs with one input argument to evaluate S and v for these values:

S = subs(S)
S =
3^(1/2)
v = subs(v)
v =
[   3^(1/2)/2 - 1/2, - 3^(1/2)/2 - 1/2, 1]
[ - 3^(1/2)/2 - 1/2,   3^(1/2)/2 - 1/2, 1]
[                 1,                 1, 1]
Was this topic helpful?