svd

Singular value decomposition of symbolic matrix

Syntax

``sigma = svd(A)``
``````[U,S,V] = svd(A)``````
``````[U,S,V] = svd(A,0)``````
``````[U,S,V] = svd(A,'econ')``````
``[___] = svd(___,outputForm)``

Description

example

````sigma = svd(A)` returns a vector `sigma` containing the singular values of a symbolic matrix `A`.```

example

``````[U,S,V] = svd(A)``` returns numeric unitary matrices `U` and `V` with the columns containing the singular vectors, and a diagonal matrix `S` containing the singular values. The matrices satisfy the condition `A = U*S*V'`, where `V'` is the Hermitian transpose (the complex conjugate transpose) of `V`. The singular vector computation uses variable-precision arithmetic. `svd` does not compute symbolic singular vectors. Therefore, the input matrix `A` must be convertible to floating-point numbers. For example, it can be a matrix of symbolic numbers.```

example

``````[U,S,V] = svd(A,0)``` returns an economy-size decomposition of matrix `A`. If `A` is an `m`-by-`n` matrix with `m > n`, then `svd` computes only the first `n` columns of `U` and `S` is an `n`-by-`n` matrix. For `m <= n`, this syntax is equivalent to `svd(A)`.```

example

``````[U,S,V] = svd(A,'econ')``` returns a different economy-size decomposition of matrix `A`. If `A` is an `m`-by-`n` matrix with `m >= n`, then this syntax is equivalent to `svd(A,0)`. For ```m < n```, `svd` computes only the first `m` columns of `V` and `S` is an `m`-by-`m` matrix.```

example

````[___] = svd(___,outputForm)` returns the singular values in the form specified by `outputForm` using any of the input or output arguments in previous syntaxes. Specify `outputForm` as `'vector'` to return the singular values as a column vector or as `'matrix'` to return the singular values as a diagonal matrix. (since R2021b) ```

Examples

collapse all

Compute the singular values of the symbolic 5-by-5 magic square. The result is a column vector.

```A = sym(magic(5)); sigma = svd(A)```
```sigma =  $\left(\begin{array}{c}65\\ \sqrt{5} \sqrt{\sqrt{1345}+65}\\ \sqrt{65} \sqrt{\sqrt{5}+5}\\ \sqrt{65} \sqrt{5-\sqrt{5}}\\ \sqrt{5} \sqrt{65-\sqrt{1345}}\end{array}\right)$```

Alternatively, specify the `'matrix'` option to return the singular values as a diagonal matrix.

`S = svd(A,'matrix')`
```S =  $\left(\begin{array}{ccccc}65& 0& 0& 0& 0\\ 0& \sqrt{5} \sqrt{\sqrt{1345}+65}& 0& 0& 0\\ 0& 0& \sqrt{65} \sqrt{\sqrt{5}+5}& 0& 0\\ 0& 0& 0& \sqrt{65} \sqrt{5-\sqrt{5}}& 0\\ 0& 0& 0& 0& \sqrt{5} \sqrt{65-\sqrt{1345}}\end{array}\right)$```

Compute singular values of a matrix whose elements are symbolic expressions.

```syms t real A = [0 1; -1 0]; E = expm(t*A)```
```E =  $\left(\begin{array}{cc}\mathrm{cos}\left(t\right)& \mathrm{sin}\left(t\right)\\ -\mathrm{sin}\left(t\right)& \mathrm{cos}\left(t\right)\end{array}\right)$```
`sigma = svd(E)`
```sigma =  $\left(\begin{array}{c}\sqrt{{\mathrm{cos}\left(t\right)}^{2}+{\mathrm{sin}\left(t\right)}^{2}}\\ \sqrt{{\mathrm{cos}\left(t\right)}^{2}+{\mathrm{sin}\left(t\right)}^{2}}\end{array}\right)$```

Simplify the result.

`sigma = simplify(sigma)`
```sigma =  $\left(\begin{array}{c}1\\ 1\end{array}\right)$```

For further computations, remove the assumption that `t` is real by recreating it using `syms`.

`syms t`

Create a 5-by-5 symbolic matrix from the magic square of order 6. Compute the singular values of the matrix using `svd`.

```M = magic(6); A = sym(M(1:5,1:5)); sigma = svd(A)```
```sigma =  ```

The `svd` function cannot find the exact singular values in terms of symbolic numbers. Instead, it returns them in terms of the `root` function.

Use `vpa` to numerically approximate the singular values.

`sigmaVpa = vpa(sigma)`
```sigmaVpa =  $\left(\begin{array}{c}91.903382299388875598380645217105\\ 41.667523645705677947038130902387\\ 33.389311761352625550607303429805\\ 7.6138651481371046117950798870896\\ 1.3299296132187199146053915272808\end{array}\right)$```

Compute the singular values and singular vectors of the 5-by-5 magic square.

```old = digits(10); A = sym(magic(5))```
```A =  $\left(\begin{array}{ccccc}17& 24& 1& 8& 15\\ 23& 5& 7& 14& 16\\ 4& 6& 13& 20& 22\\ 10& 12& 19& 21& 3\\ 11& 18& 25& 2& 9\end{array}\right)$```
`[U,S,V] = svd(A)`
```U =  $\left(\begin{array}{ccccc}0.4472135955& 0.5456348731& 0.5116672736& -0.1954395076& -0.4497583632\\ 0.4472135955& 0.4497583632& -0.1954395076& 0.5116672736& 0.5456348731\\ 0.4472135955& -1.547164189e-27& -0.632455532& -0.632455532& 1.213456644e-27\\ 0.4472135955& -0.4497583632& -0.1954395076& 0.5116672736& -0.5456348731\\ 0.4472135955& -0.5456348731& 0.5116672736& -0.1954395076& 0.4497583632\end{array}\right)$```
```S =  $\left(\begin{array}{ccccc}65.0& 0& 0& 0& 0\\ 0& 22.54708869& 0& 0& 0\\ 0& 0& 21.68742536& 0& 0\\ 0& 0& 0& 13.403566& 0\\ 0& 0& 0& 0& 11.90078954\end{array}\right)$```
```V =  $\left(\begin{array}{ccccc}0.4472135955& 0.4045164361& 0.2465648962& 0.6627260007& 0.3692782866\\ 0.4472135955& 0.005566159714& 0.6627260007& -0.2465648962& -0.5476942741\\ 0.4472135955& -0.8201651916& 1.767621593e-27& 9.706484055e-28& 0.3568319751\\ 0.4472135955& 0.005566159714& -0.6627260007& 0.2465648962& -0.5476942741\\ 0.4472135955& 0.4045164361& -0.2465648962& -0.6627260007& 0.3692782866\end{array}\right)$```
`digits(old)`

Compute the product of `U`, `S`, and the Hermitian transpose of `V` with the 10-digit accuracy. The result is the original matrix `A` with all its elements converted to floating-point numbers.

`vpa(U*S*V',10)`
```ans =  $\left(\begin{array}{ccccc}17.0& 24.0& 1.0& 8.0& 15.0\\ 23.0& 5.0& 7.0& 14.0& 16.0\\ 4.0& 6.0& 13.0& 20.0& 22.0\\ 10.0& 12.0& 19.0& 21.0& 3.0\\ 11.0& 18.0& 25.0& 2.0& 9.0\end{array}\right)$```

Calculate the full and economy-size decompositions of a rectangular matrix within 8-digit accuracy.

```old = digits(8); A = sym([1 2; 3 4; 5 6; 7 8])```
```A =  $\left(\begin{array}{cc}1& 2\\ 3& 4\\ 5& 6\\ 7& 8\end{array}\right)$```
`[U,S,V] = svd(A)`
```U =  $\left(\begin{array}{cccc}0.15248323& -0.82264747& -0.39450102& -0.37995913\\ 0.34991837& -0.42137529& 0.24279655& 0.80065588\\ 0.54735351& -0.020103103& 0.69790998& -0.46143436\\ 0.74478865& 0.38116908& -0.5462055& 0.040737612\end{array}\right)$```
```S =  $\left(\begin{array}{cc}14.269095& 0\\ 0& 0.62682823\\ 0& 0\\ 0& 0\end{array}\right)$```
```V =  $\left(\begin{array}{cc}0.64142303& 0.7671874\\ 0.7671874& -0.64142303\end{array}\right)$```
`[U,S,V] = svd(A,'econ')`
```U =  $\left(\begin{array}{cc}0.15248323& -0.82264747\\ 0.34991837& -0.42137529\\ 0.54735351& -0.020103103\\ 0.74478865& 0.38116908\end{array}\right)$```
```S =  $\left(\begin{array}{cc}14.269095& 0\\ 0& 0.62682823\end{array}\right)$```
```V =  $\left(\begin{array}{cc}0.64142303& 0.7671874\\ 0.7671874& -0.64142303\end{array}\right)$```
`digits(old)`

Since `A` is a 4-by-2 matrix, `svd(A,'econ')` returns fewer columns in `U` and fewer rows in `S` compared to a full decomposition. Extra rows of zeros in `S` are excluded, along with the corresponding columns in `U` that would multiply with those zeros in the expression `A = U*S*V'`.

Input Arguments

collapse all

Input matrix, specified as a symbolic matrix. For syntaxes with one output argument, the elements of `A` can be symbolic numbers, variables, expressions, or functions. For syntaxes with three output arguments, the elements of `A` must be convertible to floating-point numbers.

Since R2021b

Output format of singular values, specified as `'vector'` or `'matrix'`. This option allows you to specify whether the singular values are returned as a column vector or diagonal matrix. The default behavior varies according to the number of outputs specified:

• If you specify one output, such as `sigma = svd(A)`, then the singular values are returned as a column vector by default.

• If you specify three outputs, such as `[U,S,V] = svd(A)`, then the singular values are returned as a diagonal matrix, `S`, by default.

Output Arguments

collapse all

Singular values of a matrix, returned as a vector. If `sigma` is a vector of numbers, then its elements are sorted in descending order.

Singular vectors, returned as a unitary matrix. Each column of this matrix is a singular vector.

Singular values, returned as a diagonal matrix. Diagonal elements of this matrix appear in descending order.

Singular vectors, returned as a unitary matrix. Each column of this matrix is a singular vector.

Tips

• The second arguments `0` and `'econ'` only affect the shape of the returned matrices. These arguments do not affect the performance of the computations.

• Calling `svd` for numeric matrices that are not symbolic objects invokes the MATLAB® `svd` function.

• Matrix computations involving many symbolic variables can be slow. To increase the computational speed, reduce the number of symbolic variables by substituting the given values for some variables.

Compatibility Considerations

expand all

Behavior changed in R2021b