Trying to form specific block matrix...
Ältere Kommentare anzeigen
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 Kommentar
Ryles2014
am 11 Feb. 2018
Antworten (1)
Geoff Hayes
am 11 Feb. 2018
Bearbeitet: Geoff Hayes
am 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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!