%% Substitute Elements in Symbolic Matrices
% Create a 2-by-2 matrix A with automatically generated elements using sym. The generated elements A_{1,1}, A_{1,2}, A_{2,1}, and A_{2,2} do not appear in the MATLAB® workspace.

A = sym('A',[2 2])

% Substitute the element A_{1,2} with a value 5. Assign the value directly by indexing into the matrix element.

A(1,2) = 5

% Alternatively, you can create a 2-by-2 matrix using syms. Create a matrix B using syms.

syms B [2 2]
B

% The generated elements B_{1,1}, B_{1,2}, B_{2,1}, and B_{2,2} appear as symbolic variables B1_1, B1_2, B2_1, and B2_2 in the MATLAB workspace. Use subs to substitute the element of B by specifying the variable name. For example, substitute B2_2 with 4.

B = subs(B,B2_2,4)

% You can also create a matrix by specifying the elements individually. Create a 3-by-3 circulant matrix M.

syms a b c
M = [a b c; b c a; c a b]

% Replace variable b in the matrix M by the expression a + 1. The subs function replaces all b elements in matrix M with the expression a + 1.

M = subs(M,b,a+1)

% Next, replace all elements whose value is c with a + 2. You can specify the value to replace as c, M(1,3) or M(3,1).

M = subs(M,M(1,3),a+2)

% To replace a particular element of a matrix with a new value while keeping all other elements unchanged, use the assignment operation. For example, M(1,1) = 2 replaces only the first element of the matrix M with the value 2.
% Find eigenvalues and eigenvectors of the matrix M.

[V,E] = eig(M)

% Replace the symbolic parameter a with the value 1.

subs(E,a,1)

%
% Copyright 2019 The MathWorks, Inc.