I have to create a 7 by 7 matrix as [1 -1 0 0 0 0 0; -1 2 -1 0 0 0 0; 0 -1 2 -1 0 0 0; 0 0 -1 2 -1 0 0; 0 0 0 -1 2 -1 0; 0 0 0 0 -1 2 -1; 0 0 0 0 0 -1 1]

i am combining 6 k local k matrix with values [1 -1; -1 1] to get the 7 by 7 matrix as mentioned. so some nodes coincide and need additions.
Could u please help me of how to write the code.

Answers (3)

If you have a 2 x 2 matrix, call it B, that you need to combine along the diagonal with adding, and you have to do it k times to produce a (k+1) by (k+1) matrix, then
B = [1 -1; -1 1];
k = 6;
d_1 = repmat(B(2,1),1,k);
d1 = repmat(B(1,2),1,k);
d0 = [B(1,1), repmat(B(1,1)+B(2,2),1,k-1), B(2,2)];
result = diag(d_1,-1) + diag(d0, 0) + diag(d1,+1);
This is general code for any 2 x 2 matrix B and any number of repetitions k
To create a tridiagonal matrix like that, the first tool I'd probably use is DIAG. Take a look at its help text for an example that creates a tridiagonal matrix. It shouldn't be too difficult to modify to create the specific matrix you want.
If it's always a 7 by 7 matrix, what's wrong with simply doing
m = [1 -1 0 0 0 0 0;
-1 2 -1 0 0 0 0;
0 -1 2 -1 0 0 0;
0 0 -1 2 -1 0 0;
0 0 0 -1 2 -1 0;
0 0 0 0 -1 2 -1;
0 0 0 0 0 -1 1];
Why make it any more complicated? If it's not a 7x7 matrix, then you should say so.

Categories

Asked:

on 14 Sep 2015

Answered:

on 15 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!