Azzera filtri
Azzera filtri

Code to generate matrix

2 visualizzazioni (ultimi 30 giorni)
Aswas
Aswas il 12 Mag 2016
Commentato: Aswas il 12 Mag 2016
bold Hello,
I have this matrix, can someone please adv how to write the code:
A = [ 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
-1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 -1 5 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0;
-1 0 0 0 0 0 0 0 0 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0;
0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0;
0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0;
0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0 0;
0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0 0;
0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0 0;
0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 0;
0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 -1 ;
0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 5 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 3 -1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 -1 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 3 ];
b=[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;5;0;0;0;0;0;0;0;5];
  3 Commenti
Aswas
Aswas il 12 Mag 2016
Modificato: Walter Roberson il 12 Mag 2016
Sorry, but I'd like to get this matrix by running a code, so I can expand it to a much bigger matrix, for eg following generates 26 x 26 with zeros:
clc;
clear all;
L=1;
nx = 26;
p=linspace(0,L,nx);
dx=p(nx)-p(nx-1);
%==========Matrix Generation=========================
%Matrix with zeros
A=zeros(nx,nx)
rhs=zeros(nx,1)
Guillaume
Guillaume il 12 Mag 2016
Modificato: Guillaume il 12 Mag 2016
Well, then you have to explain the logic behind the matrix generation.
As far as I can tell b is just a zero column with 5 in two (random?) locations and A is a matrix that could be generated with toeplitz except that some (random?) -1 get replaced by 0 and the main diagonal is either 3, 4 or 5 (depending on ?)

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 12 Mag 2016
You can put it together using diag()
d0 = [repmat(4,1,8), 5, repmat(4,1,8), 5, repmat(3,1,8)];
d1 = ones(1, length(d0)-1);
d9 = ones(1, length(d0)-9);
A = diag(d0,0) + diag(d1, -1) + diag(d1, 1) + diag(d9, -9) + diag(d9, 9);
b = [zeros(1,17), 5, zeros(1, 7), 5];
  5 Commenti
Walter Roberson
Walter Roberson il 12 Mag 2016
d0 = [repmat(4,1,8), 5, repmat(4,1,8), 5, repmat(3,1,8)];
d9 = -ones(1, length(d0)-9);
d1 = [-ones(1,8), 0, -ones(1,8), 0, -ones(1,7)];
A = diag(d0,0) + diag(d1, -1) + diag(d1, 1) + diag(d9, -9) + diag(d9, 9);
For your purpose it might make sense to construct
d1 = one(1,length(d0)-1);
d1(9:9:end) = 0;
Aswas
Aswas il 12 Mag 2016
Thanks Walter, much appreciated.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by