Trying to form specific block matrix...
Show older comments
I want a matrix like the following:
for mxm matrix T: -20 in main diagonal, and 4 on diagonal 1 and -1. For example, m=3 gives
T=[-20 4 0;
4 -20 4;
0 4 -20]
and a mxm matrix S with 4 on main diagonal, and 1 on diagonal 1 and -1. For example, m=3 gives
S=[4 1 0;
1 4 1;
0 1 4]
I need a matrix like
A=[T S 0 0 0 ... 0;
S T S 0 0 ... 0;
...............;
0 ..... 0 S T S;
0 ..... 0 0 S T]
I am trying to do this by modifying this code here.
% form matrix A:
I = speye(m);
e = ones(m,1);
T = spdiags([4*e -20*e 4*e],[-1 0 1],m,m);
S = spdiags([4*e e 4*e],[-1 0 1],m,m);
A = (kron(I,T) + kron(S,I))/ h^2;
1 Comment
Ryles2014
on 11 Feb 2018
Answers (1)
Geoff Hayes
on 11 Feb 2018
Edited: Geoff Hayes
on 11 Feb 2018
t = 10;
s = 4;
n = 5;
A = diag(repmat(t,n,1)) + diag(repmat(s,n-1,1),1) + diag(repmat(s,n-1,1),-1);
which will set A to be
A =
10 4 0 0 0
4 10 4 0 0
0 4 10 4 0
0 0 4 10 4
0 0 0 4 10
The first call to diag creates the diagonal matrix with t along the main diagonal. We add to this two other matrices which have the s set on the -1 and 1 diagonals.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!