Borrar filtros
Borrar filtros

Code to generate matrix

2 visualizaciones (últimos 30 días)
Aswas
Aswas el 12 de Mayo de 2016
Comentada: Aswas el 12 de Mayo de 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 comentarios
Aswas
Aswas el 12 de Mayo de 2016
Editada: Walter Roberson el 12 de Mayo de 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 el 12 de Mayo de 2016
Editada: Guillaume el 12 de Mayo de 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 ?)

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 12 de Mayo de 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 el 12 de Mayo de 2016
Thanks Walter, much appreciated.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by