# symmlq

Solve system of linear equations — symmetric LQ method

## Syntax

``x = symmlq(A,b)``
``x = symmlq(A,b,tol)``
``x = symmlq(A,b,tol,maxit)``
``x = symmlq(A,b,tol,maxit,M)``
``x = symmlq(A,b,tol,maxit,M1,M2)``
``x = symmlq(A,b,tol,maxit,M1,M2,x0)``
``[x,flag] = symmlq(___)``
``[x,flag,relres] = symmlq(___)``
``[x,flag,relres,iter] = symmlq(___)``
``[x,flag,relres,iter,resvec] = symmlq(___)``
``[x,flag,relres,iter,resvec,resveccg] = symmlq(___)``

## Description

example

````x = symmlq(A,b)` attempts to solve the system of linear equations `A*x = b` for `x` using the Symmetric LQ Method. When the attempt is successful, `symmlq` displays a message to confirm convergence. If `symmlq` fails to converge after the maximum number of iterations or halts for any reason, it displays a diagnostic message that includes the relative residual `norm(b-A*x)/norm(b)` and the iteration number at which the method stopped.```

example

````x = symmlq(A,b,tol)` specifies a tolerance for the method. The default tolerance is `1e-6`.```

example

````x = symmlq(A,b,tol,maxit)` specifies the maximum number of iterations to use. `symmlq` displays a diagnostic message if it fails to converge within `maxit` iterations.```

example

````x = symmlq(A,b,tol,maxit,M)` specifies a preconditioner matrix `M` and computes `x` by effectively solving the system ${H}^{-1}A\text{\hspace{0.17em}}{H}^{-T}y={H}^{-1}b$ for y, where $y={H}^{T}x$ and $H={M}^{1/2}={\left({M}_{1}{M}_{2}\right)}^{1/2}$. The algorithm does not form H explicitly. Using a preconditioner matrix can improve the numerical properties of the problem and the efficiency of the calculation.```

example

````x = symmlq(A,b,tol,maxit,M1,M2)` specifies factors of the preconditioner matrix `M` such that ```M = M1*M2```.```

example

````x = symmlq(A,b,tol,maxit,M1,M2,x0)` specifies an initial guess for the solution vector `x`. The default is a vector of zeros.```

example

````[x,flag] = symmlq(___)` returns a flag that specifies whether the algorithm successfully converged. When `flag = 0`, convergence was successful. You can use this output syntax with any of the previous input argument combinations. When you specify the `flag` output, `symmlq` does not display any diagnostic messages.```

example

````[x,flag,relres] = symmlq(___)` also returns the residual error in the computed solution. If `flag` is `0`, then `relres <= tol`.```

example

````[x,flag,relres,iter] = symmlq(___)` also returns the iteration number `iter` at which `x` was computed.```

example

````[x,flag,relres,iter,resvec] = symmlq(___)` also returns a vector of the residual norm at each iteration, including the first residual `norm(b-A*x0)`.```

example

````[x,flag,relres,iter,resvec,resveccg] = symmlq(___)` also returns a vector of the conjugate gradients residual norms at each iteration.```

## Examples

collapse all

Solve a square linear system using `symmlq` with default settings, and then adjust the tolerance and number of iterations used in the solution process.

Create a sparse tridiagonal matrix `A` as the coefficient matrix. Use the dense row sums of $\mathit{A}$ as the vector for the right-hand side of $\mathrm{Ax}=\mathit{b}$ so that the solution $\mathit{x}$ is expected to be a vector of ones.

```n = 400; on = ones(n,1); A = spdiags([-2*on 4*on -2*on],-1:1,n,n); b = full(sum(A,2));```

Solve $\mathrm{Ax}=\mathit{b}$ using `symmlq`. The output display includes the value of the relative residual error $\frac{‖\mathit{b}-\mathrm{Ax}‖}{‖\mathit{b}‖}$.

`x = symmlq(A,b);`
```symmlq stopped at iteration 20 without converging to the desired tolerance 1e-06 because the maximum number of iterations was reached. The iterate returned (number 20) has relative residual 0.045. ```

By default `symmlq` uses 20 iterations and a tolerance of `1e-6`, and the algorithm is unable to converge in those 20 iterations for this matrix. Since the residual is on the order of `1e-2`, it is a good indicator that more iterations are needed. You also can use a larger tolerance to make it easier for the algorithm to converge.

Solve the system again using a tolerance of `1e-4` and 250 iterations.

`x = symmlq(A,b,1e-4,250);`
```symmlq converged at iteration 199 to a solution with relative residual 1.5e-14. ```

Examine the effect of using a preconditioner matrix with `symmlq` to solve a linear system.

Create a symmetric positive definite, banded coefficient matrix.

`A = delsq(numgrid('S',102));`

Define `b` so that the true solution to $\mathrm{Ax}=\mathit{b}$ is a vector of all ones.

`b = sum(A,2);`

Set the tolerance and maximum number of iterations.

```tol = 1e-12; maxit = 100;```

Use `symmlq` to find a solution at the requested tolerance and number of iterations. Specify six outputs to return information about the solution process:

• `x` is the computed solution to `A*x = b`.

• `fl0` is a flag indicating whether the algorithm converged.

• `rr0` is the relative residual of the computed answer `x`.

• `it0` is the iteration number when `x` was computed.

• `rv0` is a vector of the residual history for $‖\mathit{b}-\mathrm{Ax}‖$.

• `rvcg0` is a vector of the conjugate gradient residual history for $‖{\mathit{A}}^{\mathit{T}}\mathit{A}\text{\hspace{0.17em}}\mathit{x}-{\mathit{A}}^{\mathit{T}}\mathit{b}‖$.

```[x,fl0,rr0,it0,rv0,rvcg0] = symmlq(A,b,tol,maxit); fl0```
```fl0 = 1 ```
`rr0`
```rr0 = 0.0031 ```
`it0`
```it0 = 100 ```

`fl0` is 1 because `symmlq` does not converge to the requested tolerance `1e-12` within the requested 100 iterations.

To aid with convergence, you can specify a preconditioner matrix. Since `A` is symmetric, use `ichol` to generate the preconditioner $\mathit{M}=\mathit{L}\text{\hspace{0.17em}}{\mathit{L}}^{\mathit{T}}$. Specify the `'ict'` option to use incomplete Cholesky factorization with threshold dropping, and specify a diagonal shift value of `1e-6` to avoid nonpositive pivots. Solve the preconditioned system by specifying `L` and `L'` as inputs to `symmlq`.

```setup = struct('type','ict','diagcomp',1e-6,'droptol',1e-14); L = ichol(A,setup); [x1,fl1,rr1,it1,rv1,rvcg1] = symmlq(A,b,tol,maxit,L,L'); fl1```
```fl1 = 0 ```
`rr1`
```rr1 = 2.7572e-15 ```
`it1`
```it1 = 3 ```

The use of an `ichol` preconditioner greatly improves the numerical properties of the problem, and `symmlq` is able to converge quickly. The output `rv1(1)` is `norm(b)`, and the output `rv1(end)` is `norm(b-A*x1)`.

You can follow the progress of `symmlq` by plotting the relative residuals at each iteration. Plot the conjugate gradient residual history of each solution with a line for the specified tolerance.

```semilogy(0:length(rvcg0)-1,rvcg0/norm(b),'-o') hold on semilogy(0:length(rvcg1)-1,rvcg1/norm(b),'-o') yline(tol,'r--'); legend('No preconditioner','ICHOL preconditioner','Tolerance','Location','SouthEast') xlabel('Iteration number') ylabel('Relative residual')``` Examine the effect of supplying `symmlq` with an initial guess of the solution.

Create a tridiagonal sparse matrix. Use the sum of each row as the vector for the right-hand side of $\mathrm{Ax}=\mathit{b}$ so that the expected solution for $\mathit{x}$ is a vector of ones.

```n = 900; e = ones(n,1); A = spdiags([e 2*e e],-1:1,n,n); b = sum(A,2);```

Use `symmlq` to solve $\mathrm{Ax}=\mathit{b}$ twice: one time with the default initial guess, and one time with a good initial guess of the solution. Use 200 iterations and the default tolerance for both solutions. Specify the initial guess in the second solution as a vector with all elements equal to `0.99`.

```maxit = 200; x1 = symmlq(A,b,[],maxit);```
```symmlq converged at iteration 34 to a solution with relative residual 9.5e-07. ```
```x0 = 0.99*e; x2 = symmlq(A,b,[],maxit,[],[],x0);```
```symmlq converged at iteration 6 to a solution with relative residual 8.7e-07. ```

In this case supplying an initial guess enables `symmlq` to converge more quickly.

Returning Intermediate Results

You also can use the initial guess to get intermediate results by calling `symmlq` in a for-loop. Each call to the solver performs a few iterations and stores the calculated solution. Then you use that solution as the initial vector for the next batch of iterations.

For example, this code performs 100 iterations four times and stores the solution vector after each pass in the for-loop:

```x0 = zeros(size(A,2),1); tol = 1e-8; maxit = 100; for k = 1:4 [x,flag,relres] = symmlq(A,b,tol,maxit,[],[],x0); X(:,k) = x; R(k) = relres; x0 = x; end```

`X(:,k)` is the solution vector computed at iteration `k` of the for-loop, and `R(k)` is the relative residual of that solution.

Solve a linear system by providing `symmlq` with a function handle that computes `A*x` in place of the coefficient matrix `A`.

One of the Wilkinson test matrices generated by `gallery` is a 21-by-21 tridiagonal matrix. Preview the matrix.

`A = gallery('wilk',21)`
```A = 21×21 10 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 9 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 8 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 7 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 6 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 4 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 ⋮ ```

The Wilkinson matrix has a special structure, so you can represent the operation `A*x` with a function handle. When `A` multiplies a vector, most of the elements in the resulting vector are zeros. The nonzero elements in the result correspond with the nonzero tridiagonal elements of `A`. Moreover, only the main diagonal has nonzeros that are not equal to 1.

The expression $\mathrm{Ax}$ becomes:

$\mathrm{Ax}=\left[\begin{array}{cccccccccc}10& 1& 0& \cdots & & \cdots & & \cdots & 0& 0\\ 1& 9& 1& 0& & & & & & 0\\ 0& 1& 8& 1& 0& & & & & ⋮\\ ⋮& 0& 1& 7& 1& 0& & & & \\ & & 0& 1& 6& 1& 0& & & ⋮\\ ⋮& & & 0& 1& 5& 1& 0& & \\ & & & & 0& 1& 4& 1& 0& ⋮\\ ⋮& & & & & 0& 1& 3& \ddots & 0\\ 0& & & & & & 0& \ddots & \ddots & 1\\ 0& 0& \cdots & & \cdots & & \cdots & 0& 1& 10\end{array}\right]\left[\begin{array}{c}{\mathit{x}}_{1}\\ {\mathit{x}}_{2}\\ {\mathit{x}}_{3}\\ {\mathit{x}}_{4}\\ {\mathit{x}}_{5}\\ ⋮\\ \\ ⋮\\ \\ {\mathit{x}}_{21}\end{array}\right]=\left[\begin{array}{c}10{\mathit{x}}_{1}+{\mathit{x}}_{2}\\ {\mathit{x}}_{1}+9{\mathit{x}}_{2}+{\mathit{x}}_{3}\\ {\mathit{x}}_{2}+8{\mathit{x}}_{3}+{\mathit{x}}_{4}\\ ⋮\\ {\mathit{x}}_{19}+9{\mathit{x}}_{20}+{\mathit{x}}_{21}\\ {\mathit{x}}_{20}+10{\mathit{x}}_{21}\end{array}\right]$.

The resulting vector can be written as the sum of three vectors:

$\mathrm{Ax}=\left[\begin{array}{c}0+10{\mathit{x}}_{1}+{\mathit{x}}_{2}\\ {\mathit{x}}_{1}+9{\mathit{x}}_{2}+{\mathit{x}}_{3}\\ {\mathit{x}}_{2}+8{\mathit{x}}_{3}+{\mathit{x}}_{4}\\ ⋮\\ {\mathit{x}}_{19}+9{\mathit{x}}_{20}+{\mathit{x}}_{21}\\ {\mathit{x}}_{20}+10{\mathit{x}}_{21}+0\end{array}\right]$=$\left[\begin{array}{c}0\\ {\mathit{x}}_{1}\\ ⋮\\ {\mathit{x}}_{20}\end{array}\right]+\left[\begin{array}{c}10{\mathit{x}}_{1}\\ 9{\mathit{x}}_{2}\\ ⋮\\ 10{\mathit{x}}_{21}\end{array}\right]+\left[\begin{array}{c}{\mathit{x}}_{2}\\ ⋮\\ {\mathit{x}}_{21}\\ 0\end{array}\right]$.

In MATLAB®, write a function that creates these vectors and adds them together, thus giving the value of `A*x`:

```function y = afun(x) y = [0; x(1:20)] + ... [(10:-1:0)'; (1:10)'].*x + ... [x(2:21); 0]; end ```

(This function is saved as a local function at the end of the example.)

Now, solve the linear system $\mathrm{Ax}=\mathit{b}$ by providing `symmlq` with the function handle that calculates `A*x`. Use a tolerance of `1e-12` and 50 iterations.

```b = ones(21,1); tol = 1e-12; maxit = 50; x1 = symmlq(@afun,b,tol,maxit)```
```symmlq converged at iteration 10 to a solution with relative residual 4.5e-16. ```
```x1 = 21×1 0.0910 0.0899 0.0999 0.1109 0.1241 0.1443 0.1544 0.2383 0.1309 0.5000 ⋮ ```

Check that `afun(x1)` produces a vector of ones.

`afun(x1)`
```ans = 21×1 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 ⋮ ```

Local Functions

```function y = afun(x) y = [0; x(1:20)] + ... [(10:-1:0)'; (1:10)'].*x + ... [x(2:21); 0]; end```

## Input Arguments

collapse all

Coefficient matrix, specified as a symmetric matrix or function handle. This matrix is the coefficient matrix in the linear system `A*x = b`. Generally, `A` is a large sparse matrix or a function handle that returns the product of a large sparse matrix and column vector. You can use `issymmetric` to confirm that `A` is symmetric.

#### Specifying `A` as a Function Handle

You can optionally specify the coefficient matrix as a function handle instead of a matrix. The function handle returns matrix-vector products instead of forming the entire coefficient matrix, making the calculation more efficient.

To use a function handle, use the function signature `function y = afun(x)`. Parameterizing Functions explains how to provide additional parameters to the function `afun`, if necessary. The function call `afun(x)` must return the value of `A*x`.

Data Types: `double` | `function_handle`
Complex Number Support: Yes

Right-hand side of linear equation, specified as a column vector. The length of `b` must be equal to `size(A,1)`.

Data Types: `double`
Complex Number Support: Yes

Method tolerance, specified as a positive scalar. Use this input to trade-off accuracy and runtime in the calculation. `symmlq` must meet the tolerance within the number of allowed iterations to be successful. A smaller value of `tol` means the answer must be more precise for the calculation to be successful.

Data Types: `double`

Maximum number of iterations, specified as a positive scalar integer. Increase the value of `maxit` to allow more iterations for `symmlq` to meet the tolerance `tol`. Generally, a smaller value of `tol` means more iterations are required to successfully complete the calculation.

Preconditioner matrices, specified as separate arguments of matrices or function handles. You can specify a preconditioner matrix `M` or its matrix factors `M = M1*M2` to improve the numerical aspects of the linear system and make it easier for `symmlq` to converge quickly. You can use the incomplete matrix factorization functions `ilu` and `ichol` to generate preconditioner matrices. You also can use `equilibrate` prior to factorization to improve the condition number of the coefficient matrix. For more information on preconditioners, see Iterative Methods for Linear Systems.

`symmlq` treats unspecified preconditioners as identity matrices.

#### Specifying `M` as a Function Handle

You can optionally specify any of `M`, `M1`, or `M2` as function handles instead of matrices. The function handle performs matrix-vector operations instead of forming the entire preconditioner matrix, making the calculation more efficient.

To use a function handle, use the function signature ```function y = mfun(x)```. Parameterizing Functions explains how to provide additional parameters to the function `mfun`, if necessary. The function call `mfun(x)` must return the value of `M\x` or `M2\(M1\x)`.

Data Types: `double` | `function_handle`
Complex Number Support: Yes

Initial guess, specified as a column vector with length equal to `size(A,2)`. If you can provide `symmlq` with a more reasonable initial guess `x0` than the default vector of zeros, then it can save computation time and help the algorithm converge faster.

Data Types: `double`
Complex Number Support: Yes

## Output Arguments

collapse all

Linear system solution, returned as a column vector. This output gives the approximate solution to the linear system `A*x = b`. If the calculation is successful (`flag = 0`), then `relres` is less than or equal to `tol`.

Whenever the calculation is not successful (`flag ~= 0`), the solution `x` returned by `symmlq` is the one with minimal residual norm computed over all the iterations.

Convergence flag, returned as one of the scalar values in this table. The convergence flag indicates whether the calculation was successful and differentiates between several different forms of failure.

Flag Value

Convergence

`0`

Success — `symmlq` converged to the desired tolerance `tol` within `maxit` iterations.

`1`

Failure — `symmlq` iterated `maxit` iterations but did not converge.

`2`

Failure — The preconditioner matrix `M` or `M = M1*M2` is ill conditioned.

`3`

Failure — `symmlq` stagnated after two consecutive iterations were the same.

`4`

Failure — One of the scalar quantities calculated by the `symmlq` algorithm became too small or too large to continue computing.

`5`

Failure — Preconditioner matrix `M` is not symmetric positive definite.

Relative residual error, returned as a scalar. The relative residual error is an indication of how accurate the returned answer `x` is. `symmlq` tracks the relative residual and conjugate gradients residual at each iteration in the solution process, and the algorithm converges when either residual meets the specified tolerance `tol`. The `relres` output contains the value of the residual that converged, either the relative residual or the conjugate gradients residual:

• The relative residual error is equal to `norm(b-A*x)/norm(b)` and is generally the residual that meets the tolerance `tol` when `symmlq` converges. The `resvec` output tracks the history of this residual over all iterations.

• The conjugate gradients residual error is equal to ```norm(A'*A*x - A'*b)```. This residual causes `symmlq` to converge less frequently than the relative residual. The `resveccg` output tracks the history of this residual over all iterations.

Data Types: `double`

Iteration number, returned as a scalar. This output indicates the iteration number at which the computed answer for `x` was calculated.

Data Types: `double`

Residual error, returned as a vector. The residual error `norm(b-A*x)` reveals how close the algorithm is to converging for a given value of `x`. The number of elements in `resvec` is equal to the number of iterations. You can examine the contents of `resvec` to help decide whether to change the values of `tol` or `maxit`.

Data Types: `double`

Conjugate gradients residual norms, returned as a vector. The number of elements in `resveccg` is equal to the number of iterations.

Data Types: `double`

collapse all

### Symmetric LQ Method

The MINRES and SYMMLQ methods are variants of the Lanczos method that underpins the conjugate gradients method PCG. Like PCG, the coefficient matrix still needs to be symmetric, but MINRES and SYMMLQ allow it to be indefinite (not all eigenvalues need to be positive). This is achieved by avoiding the implicit LU factorization normally present in the Lanczos method, which is prone to breakdowns when zero pivots are encountered.

MINRES minimizes the residual in the 2-norm, while SYMMLQ solves a projected system using an LQ factorization and keeps the residual orthogonal to all previous ones .

## Tips

• Convergence of most iterative methods depends on the condition number of the coefficient matrix, `cond(A)`. When `A` is square, you can use `equilibrate` to improve its condition number, and on its own this makes it easier for most iterative solvers to converge. However, using `equilibrate` also leads to better quality preconditioner matrices when you subsequently factor the equilibrated matrix ```B = R*P*A*C```.

• You can use matrix reordering functions such as `dissect` and `symrcm` to permute the rows and columns of the coefficient matrix and minimize the number of nonzeros when the coefficient matrix is factored to generate a preconditioner. This can reduce the memory and time required to subsequently solve the preconditioned linear system.

 Barrett, R., M. Berry, T. F. Chan, et al., Templates for the Solution of Linear Systems: Building Blocks for Iterative Methods, SIAM, Philadelphia, 1994.

 Paige, C. C. and M. A. Saunders, "Solution of Sparse Indefinite Systems of Linear Equations." SIAM J. Numer. Anal., Vol.12, 1975, pp. 617-629.