Code covered by the BSD License  

Highlights from
(Block) tri-diagonal matrices

5.0

5.0 | 11 ratings Rate this file 46 Downloads (last 30 days) File Size: 42.21 KB File ID: #10603
image thumbnail

(Block) tri-diagonal matrices

by John D'Errico

 

01 Apr 2006 (Updated 14 Dec 2007)

Generate (block) tridiagonal matrices

| Watch this File

File Information
Description

Every once in a while I need to generate a tridiagonal matrix with replicated elements down the diagonals. Sometimes its nice to generate block tridiagonal matrices of the same form.

full(blktridiag(2,-1,-1,5))
ans =
     2 -1 0 0 0
    -1 2 -1 0 0
     0 -1 2 -1 0
     0 0 -1 2 -1
     0 0 0 -1 2

full(blktridiag(ones(2),2*ones(2),3*ones(2),3))
ans =
     1 1 3 3 0 0
     1 1 3 3 0 0
     2 2 1 1 3 3
     2 2 1 1 3 3
     0 0 2 2 1 1
     0 0 2 2 1 1

This can of course be easily extended to handle general blocks, so since I saw an interest from a user, this release does that too.

Amd = reshape(1:16,[2,2,4]);
Asub = reshape(101:112,[2,2,3]);
Asup = reshape(201:212,[2,2,3]);
A = blktridiag(Amd,Asub,Asup);

full(A)
ans =
     1 3 201 203 0 0 0 0
     2 4 202 204 0 0 0 0
   101 103 5 7 205 207 0 0
   102 104 6 8 206 208 0 0
     0 0 105 107 9 11 209 211
     0 0 106 108 10 12 210 212
     0 0 0 0 109 111 13 15
     0 0 0 0 110 112 14 16

I've also seen a request to allow non-square blocks. This too is now accommodated in the latest release.

A = blktridiag([1;2],[3;4],[5;6],4);
full(A)
ans =
     1 5 0 0
     2 6 0 0
     3 1 5 0
     4 2 6 0
     0 3 1 5
     0 4 2 6
     0 0 3 1
     0 0 4 2

MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements I've intentionally kept this code accessible to earlier releases of matlab.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (16)
03 Apr 2006 Kong Zuor

Nice work John. Very usefull indeed. This is a very useful code, especially CFD.

11 Apr 2006 hicham laanaya

Thank you John.

13 Jun 2006 Miguel de Vega

Wonderful code, and very useful. Thanks John

02 Oct 2006 Adrian Verhoef

Very useful code....
Could you please show us the code for the extension too? Thanks in advance.

13 May 2007 Tim Davis

Great example of the best way to create a sparse matrix. Nice demo (including a demo is very important in a usable submission).

One minor comment: your submission as of May 11 includes ".DS_Store" files in each folder, which only tell a Mac how to size the window when viewing the folders. Those files are visible to the Windows and Unix/Linux users. They can be deleted.

13 Dec 2007 Antoine Tambue

Many thank for the function, it work very well for square blocks.
Can you have any idea for tridiagonal matrix with not square blocks?
One again many thanks
Antoine

14 Dec 2007 Antoine Tambue  
14 Jan 2008 Jun Seo

Good example for making a sparse matrix.

19 Jul 2011 Bert Xer

Nice work! Thanks so much!

19 Jan 2012 Silvia Sasheva

Hello, can you help with this error:??? Undefined function or method 'blktridiag' for input arguments of type
'double'.
Probably, it is a stupid question, but I really need help! Thank you in advance!
Best Regards!

19 Jan 2012 John D'Errico

It means you have not placed the function in a directory on your search path. Do NOT put it in the matlab toolbox directories, but in some other directory that you should reserve for your own functions. Then make sure that directory is on the search path. Use the functions addpath, savepath and pathtool to set up your search path.

01 Feb 2012 Silvia Sasheva

Hello,
can please help me to find out if it's possible, using "blktridiag", to generate a block tridiagonal matrix A, whose main diagonal (Amd) to starts and ends with different copies of Amd. I.e. AmdFirst and AmdLast~= Amd...
example:
[AmdF Asup 0 0 0;
Asub Amd Asup 0 0;
0 Asub Amd Asup 0;
0 0 Asub Amd Asup;
0 0 0 Asub AmdL]

I hope that the explanation was clear enought! Thank you in advance!
Regards

01 Feb 2012 John D'Errico

Of course it is possible, since you can supply the individual blocks along the main diagonal. Since the diagonal blocks can be supplied as a pxqxn array in Amd, use repmat to replicate the middle blocks, then replace the first and last blocks in Amd as you wish. Then its just a call to blktridiag.

01 Feb 2012 Silvia Sasheva

Thank you for the quick and complete answer. Unfortunately, I'm not familiar with a "repmat" and how to replicate only the middle blocks and replace the first and the last one.
I tried this:
 v = repmat(AmdF(1),n,1);
  v = repmat(Amd(:),n,1);
  v = repmat(AmdL(n),n,1);
but of course it doesn't work....
Have you got a clue how to call it?
Thank you in advance and appreciation again for the code!!!

03 Feb 2012 Silvia Sasheva

Hello, John,
I did it in other way. I defined Amdb (A main diag. boundary blocks) in your code and then applied this:
 s=repmat(Amdb(:),1,1);
 v=[s;repmat(Amd(:),n-2,1);s];
It works and now I will try to go further with the solution of the examined problem. Thank you and regards!

07 Feb 2012 Dany  
Please login to add a comment or rating.
Updates
03 Oct 2006

Added the ability to handle general, distinct blocks, also added demos and a screenshot.

03 Oct 2006

Added test subdirectory for Select nom

11 Oct 2006

Cleaned up the code a bit, removed replicated code blocks and a spare call to ndgrid. Also special cased scalar tridiagonals using spdiags. (Thanks to Jos for pointing these items out.)

14 May 2007

Update to remove the .DS_Store files in the zip file. (My thanks to Tim, and of course, to the gipper.)

14 Dec 2007

(Release 4) Extend the tool to allow non-square blocks

Contact us at files@mathworks.com