creating a diagonalmatrix with diag and add a value to it

14 次查看(过去 30 天)
hi,
I'm trying to create a diagonal matrix, with two minor diagonals and one major diagonal. I already created the two minor, but I don't know how to create the main one.
Code:
%diagonalmatrix
D = diag((-1)*k_i,-1) + diag(k_i+k_(i+1)) + diag((-1)*k_i, 1);
diag((-1)*k_i,-1) and diag((-1)*k_i, 1) already work.
How to change diag(k_i+k_(i+1)) ?
Must I create a new matrix with the addition already included for the major diagonal? How to?
Thanks!
I included the concept in the attachment.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-4-30
Try this. It shows an example with symbolic variables to show that it creates the correct matrix. You can try it with k as a numeric vector
k = sym('k', [1, 8]);
A = diag(k(2:end-1),1) + diag(k(2:end-1),-1) + diag(k(1:end-1)+k(2:end));
Result
>> A
A =
[ k1 + k2, k2, 0, 0, 0, 0, 0]
[ k2, k2 + k3, k3, 0, 0, 0, 0]
[ 0, k3, k3 + k4, k4, 0, 0, 0]
[ 0, 0, k4, k4 + k5, k5, 0, 0]
[ 0, 0, 0, k5, k5 + k6, k6, 0]
[ 0, 0, 0, 0, k6, k6 + k7, k7]
[ 0, 0, 0, 0, 0, k7, k7 + k8]

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by