so, i want only a general matlab code to solve like this equation.
because i am using "finite difference method"
Best Answer
Why not just build it as a sparse matrix using spdiags, then solve using backslash? It will be quite fast for a tridiagonal matrix, and you won't need to write any solver at all.
For example, I won't bother to do more than create a random tridiagonal matrix, rather than building one directly from your equation, but the time is all that matters.
n = 100000;
A = spdiags(rand(n,3),-1:1,n,n);
b = rand(n,1);
tic,x = A\b;toc
Elapsed time is 0.023090 seconds.
So to solve a system with 1e5 unknowns took me .023 seconds, with virtually NO time invested to write anything.
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?
You can define anything you want to be sparse if you so desire. So sparse(ones(1000)) will produce a sparse matrix. ;-)
But seriously, this matrix really is reasonably sparse. Just read what is shown on the page you direct to!
We see that it is shown to be an 1899x1899 square matrix. There are 20296 non-zeros in the matrix.
20296/1899
ans =
10.688
So on average, roughly 10.7 non-zeros per row of a matrix, where each row has 1899 elements.
20296/1899/1899
ans =
0.0056281
Total density of around 0.6% non-zeros. Is that matrix sparse? Well, yes, it is. Not massively so, but it is sparse. Since I don't have that matrix, I'll do a little memory comparison on a random one with the same density.
As = sprand(1899,1899,0.0056);
Af = full(As);
whos As Af
Name SizeBytesClassAttributes
Af 1899x189928849608double
As 1899x1899337568doublesparse
So we see that storing the matrix as sparse gives me a decrese in memory of almost a factor of 100-1. (Sparse matrix storage also requires we store the location of those elements, so it is not quite as efficient as we might like.)
b = rand(1899,1);
timeit(@() Af*b)
ans =
0.0023482
timeit(@() As*b)
ans =
6.1858e-05
And working with the matrix with a matrix multiply does give a significant savings.
Much of the time, when you work with a sparse matrix, you will perform a decomposition of some sort. Odds are that will not produce a speed increase here, because the matrix itself is not a nicely tightly banded matrix. I think much of the time, people think of a sparse matrix in the form of something like a tridiagonal matrix. A matrix factorization of a tridiagonal matrix will produce no fill-in at all. On the matrix you show, if we tried to do a Cholesky or LU factoriation, for example, the result will be a virtually completely full triangular matrix.
tic,[L,U] = lu(Af);toc
Elapsed time is 0.205769 seconds.
tic,[L,U] = lu(As);toc
Elapsed time is 3.032499 seconds.
As you see, not all computations on such a matrix will see a gain in speed. Here, we see the result ends up as a nearly full triangular matrix for the factors. So treating that matrix as sparse, IF you intend to do a decomposition of the matrix will be a time loss, because the overhead from the sparse algorithms is too costly.
nnz(L)
ans =
1177842
nnz(U)
ans =
1208969
numel(L)
ans =
3606201
That is to be expected on this matrix. It does not mean the original was not sparse. It is indeed sparse. Just perhaps not as massively sparse as you may want a matrix to be. Sometimes sparse must be measured by the eye of the beholder, in proper context.
Best Answer