# Solve Ax = b tridiagonal matrix

**URL:** <https://discourse.julialang.org/t/solve-ax-b-tridiagonal-matrix/22629>\
**Category:** New to Julia\
**Created:** [April 2, 2019, 5:31am UTC](https://discourse.julialang.org/t/solve-ax-b-tridiagonal-matrix/22629 "2019-04-02T05:31:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![areida](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@areida](https://discourse.julialang.org/u/areida)\
**Post date:** [April 2, 2019, 5:31am UTC](https://discourse.julialang.org/t/solve-ax-b-tridiagonal-matrix/22629/1 "2019-04-02T05:31:40Z")

</div>

I’m trying to solve Ax = b in matrix.

```julia
using ToeplitzMatrices
function toeplitz(n)
    r = [4; -1; zeros(n-2)]
    Toeplitz(r, r)
end

```

and I’m using this function to generate matrix with n [10,100,200,300,400] and try to solve Ax = b (Cholesky factorization)  
I found that probably I can use LDL factorization

```julia
for a in [10, 100,200,300,400]
    A = toeplitz(a)
    LDLT = ldl(A)
    x = LDLT \b
end

```

and gave it `UndefVarError: ldl not defined

Stacktrace:  
[1] top-level scope at .\In[34]:3`

I’m not sure what I should change. Or maybe it’s not a good way to resolve this and there is another way ?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 2, 2019, 5:38am UTC](https://discourse.julialang.org/t/solve-ax-b-tridiagonal-matrix/22629/2 "2019-04-02T05:38:56Z")

</div>

Are you looking for

```julia
help?> ldlt
search: ldlt ldlt! LDLt foldl mapfoldl

  ldlt(S::SymTridiagonal) -> LDLt

  Compute an LDLt factorization of the real symmetric tridiagonal matrix S such that S =
  L*Diagonal(d)*L' where L is a unit lower triangular matrix and d is a vector. The main use of an
  LDLt factorization F = ldlt(S) is to solve the linear system of equations Sx = b with F\b.

```

in the `LinearAlgebra` module?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 2, 2019, 11:17am UTC](https://discourse.julialang.org/t/solve-ax-b-tridiagonal-matrix/22629/3 "2019-04-02T11:17:26Z")

</div>

Since the matrix is symmetric tridiagonal, you should take advantage of that by constructing the `SymTridiagonal` type, which has only O(n) storage and can be solved in O(n) time:

```julia
mymatrix(n) = SymTridiagonal(fill(4, n), fill(-1, n-1))

```

If you just do `mymatrix(n) \ b`, it [automatically uses the LDLᵀ factorization](https://github.com/JuliaLang/julia/blob/719c0620e4c74379585a86b0aac0eafba17320ee/stdlib/LinearAlgebra/src/tridiag.jl#L198), which for a tridiagonal matrix takes O(n) time.
