Tridiagonal matrix (thomas algorithm)
415 views (last 30 days)
i want to solve a second order, homogeneous differential equation by using tridiagonal matrix. can u help me?
so, i want only a general matlab code to solve like this equation.
because i am using "finite difference method"
John D'Errico on 11 Mar 2011
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.
More Answers (1)
Shantanu Vachhani on 24 Dec 2015
Edited: Walter Roberson on 24 Dec 2015
%of the form AX=B
n=input('enter the order for the matrix');
a(i,j)=input('enter the element of coefficient matrix');
r(i)=input('enter the RHS');