フィルターのクリア

creating a diagonalmatrix with diag and add a value to it

9 ビュー (過去 30 日間)
Sergio Orasch
Sergio Orasch 2020 年 4 月 30 日
コメント済み: Ameer Hamza 2020 年 5 月 3 日
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]
  2 件のコメント
Sergio Orasch
Sergio Orasch 2020 年 5 月 3 日
thanks!
Ameer Hamza
Ameer Hamza 2020 年 5 月 3 日
I am glad to be of help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by