How do I insert multiple vectors as diagonals in the same matrix?
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nicholas Boccella
am 16 Apr. 2019
Beantwortet: Kelly Kearney
am 16 Apr. 2019
I'm trying to use a forward-difference method to solve a parabolic PDE. I need a matrix with the main diagonal and both diagonals one spot off from the main diagonal to be defined and the rest 0. My current code is this:
v = lambda*ones(1,m-2);
v1 = (1-2*lambda)*ones(1,m-1);
A = diag(v,1);
disp(A);
A = diag(v1,0);
disp(A);
A = diag(v,-1);
disp(A);
lambda is just a scalar. This is what my intuition told me to do, but after each time I do A=diag, it overwrites the previous matrix. I need the vector v to be on the off diagonals -1 and 1, and the vector v1 to be on the main diagonal. This is probably pretty easy but I am just missing it. Any help would be appreciated.
0 Kommentare
Akzeptierte Antwort
Kelly Kearney
am 16 Apr. 2019
You're close... you just need to add the matrices together:
A = diag(v,1) + diag(v,-1) + diag(v1,0)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Geometry and Mesh finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!