Getting a diag function

4 views (last 30 days)
Mark Loui
Mark Loui on 18 Mar 2021
Edited: Matt J on 18 Mar 2021
Hi there im stuck with this problem and really would like help here
n=length(x) %Let says is 20
x=rand(n,1);
y=rand(n,1);
RHS_empty=ones(ny,1);
iter_h=0;
for i=1:n-1
h(i)=x(i+1)-x(i);
iter_h=iter_h+1;
Ma_Sp=diag(1 h(i)) 2(h(i+1)*h(i))
end
I like to create a diagonal matrix with the interval with the specific things,
The diagonal matrix size is depending on the h(i), meaning h19, i like to get the matrix to be 20x20 with 1 at the first lane and h1 2h2*h1 h2 2h3*h2 hn 2h(i+1)*h(i) for the second coloum and it goes on for the third coloum and the fourth all the way to the 20th column, i am stuck on this matter and would appreciate your help
  3 Comments
Mark Loui
Mark Loui on 18 Mar 2021
ermm are you familar with Natural Splines Boundary Condition, my main goal is to create a matrix as such. If not
It means i like to create a matrix that is diagonal
in matlab code wise is
A=[1 0 0 0 ;0 h1 2(h2+h1) h2 2(h3+h2) ;0 0 h2 2(h3+h2); 0 0 0 h4] This is what i meant by the entire sentence
So if n=19, there should be a 20x20 matrix fill with the listed diagonal matrix above, this is what i mean. Hope this clears things up
Jan
Jan on 18 Mar 2021
Edited: Jan on 18 Mar 2021
The shown matrix A is not diagonal. Diagonal matrices contaoin non-zero elements on the diagonal only. You can create them with the command diag() .
There is a large, if not infitine number of ways, to fill a 20x20 matrix with a 4x4 matrix.
Maybe you are looking for the kron() command:
x = [1,2;3,4]
x =
1 2
3 4
kron(eye(3), x)
ans =
1 2 0 0 0 0
3 4 0 0 0 0
0 0 1 2 0 0
0 0 3 4 0 0
0 0 0 0 1 2
0 0 0 0 3 4

Sign in to comment.

Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!