## Substitute Scalars with Matrices

Create the following expression representing the sine function.

syms w t f = sin(w*t);

Suppose, your task involves creating a matrix whose elements are sine functions with angular velocities represented by a Toeplitz matrix. First, create a 4-by-4 Toeplitz matrix.

W = toeplitz(sym([3 2 1 0]))

W = [ 3, 2, 1, 0] [ 2, 3, 2, 1] [ 1, 2, 3, 2] [ 0, 1, 2, 3]

Next, replace the variable `w`

in the expression `f`

with
the Toeplitz matrix `W`

. When you replace a scalar
in a symbolic expression with a matrix, `subs`

expands
the expression into a matrix. In this example, `subs`

expands ```
f
= sin(w*t)
```

into a 4-by-4 matrix whose elements are `sin(w*t)`

.
Then it replaces `w`

in that matrix with the corresponding
elements of the Toeplitz matrix `W`

.

F = subs(f, w, W)

F = [ sin(3*t), sin(2*t), sin(t), 0] [ sin(2*t), sin(3*t), sin(2*t), sin(t)] [ sin(t), sin(2*t), sin(3*t), sin(2*t)] [ 0, sin(t), sin(2*t), sin(3*t)]

Find the sum of these sine waves at `t = π`

, ```
t
= π/2
```

, `t = π/3`

, ```
t
= π/4
```

, `t = π/5`

, and ```
t
= π/6
```

. First, find the sum of all elements of matrix `F`

.
Here, the first call to `sum`

returns a row vector
containing sums of elements in each column. The second call to `sum`

returns
the sum of elements of that row vector.

S = sum(sum(F))

S = 6*sin(2*t) + 4*sin(3*t) + 4*sin(t)

Now, use `subs`

to evaluate `S`

for
particular values of the variable `t`

.

subs(S, t, sym(pi)./[1:6])

[ 0,... 0,... 5*3^(1/2), 4*2^(1/2) + 6,... 2^(1/2)*(5 - 5^(1/2))^(1/2) + (5*2^(1/2)*(5^(1/2) + 5)^(1/2))/2,... 3*3^(1/2) + 6]

You also can use `subs`

to replace a scalar
element of a matrix with another matrix. In this case, `subs`

expands
the matrix to accommodate new elements. For example, replace zero
elements of the matrix `F`

with a column vector `[1;2]`

.
The original 4-by-4 matrix `F`

expands to an 8-by-4
matrix. The `subs`

function duplicates each row
of the original matrix, not only the rows containing zero elements.

F = subs(F, 0, [1;2])

F = [ sin(3*t), sin(2*t), sin(t), 1] [ sin(3*t), sin(2*t), sin(t), 2] [ sin(2*t), sin(3*t), sin(2*t), sin(t)] [ sin(2*t), sin(3*t), sin(2*t), sin(t)] [ sin(t), sin(2*t), sin(3*t), sin(2*t)] [ sin(t), sin(2*t), sin(3*t), sin(2*t)] [ 1, sin(t), sin(2*t), sin(3*t)] [ 2, sin(t), sin(2*t), sin(3*t)]