# Solve tridiagonal matrix system by thomas algorithm

11 visualizzazioni (ultimi 30 giorni)
m m il 8 Mag 2019
Modificato: John D'Errico il 8 Mag 2019
hi
im trying to solve tridiagonal matrix system by thomas algorithm. my question is: it's obligatory to define the matrix elements or not ?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

John D'Errico il 8 Mag 2019
Modificato: John D'Errico il 8 Mag 2019
Is it obligatory to create a matrix with those specific elements? Of course not. As long as you know what elements have which value, then nothing is obligatory. You are the one who is writing the code, no?
If your goal is to solve a psecific problem that is not homework, then it is usully simpler to just use sparse matrices and use backslash. The virtue of using sparse matrices is you do not need to write code to solve the problem.
I would also point out that the decomposition function is provided in MATLAB, which allows you to specify a banded matrix. These are things you would need to test and compare the time required. But again, a sparse use of backslash is FAST.
For example, on a 1e6x1e6 tridiagonal problem, the time required for a solve is tiny:
n = 1000000;
A = spdiags(rand(n,3),1:1,n,n) + speye(n)*2;
B = rand(n,1);
timeit(@() A\B)
ans =
0.010033
Why would you want to write a looped code that probably runs more slowly?
Now, maybe your question is if you can apply the Thomas algorithm to a totally general tridiagonal matrix with symbolic coefficients. Well, yes, but why?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by