getting error when using diag command
2 views (last 30 days)
Show older comments
hello i am doing simulation related to duct to control noise and i have structure of matrices, for example i am creating Anr matrices the An and then i want to create Atilta which contains all these matrices according formulas but when i am using diag it gaves me error related to scalar what should i do ?
the formulas are following
*********************
Nmod = 5; % modes in duct 1 to 5
L = 1.2; % length of duct
K = 0.051; % impedance at one end
zi = 0.315;
zm = 0.775;
c = 340 ;
roh = 1.225;
S = 1;
eta =1 ;
betan = -(1:Nmod)*pi/L;
alfa = 1/(2*L)*log((1-K)/(1+K));
Atilta = []; % final matrices
for i = 1:Nmod
%
Anr = c * [alfa betan; -betan alfa]; % formula for Anr matrices
%
Ani = c*eta*[1 0;0 1];
%
An = [Anr -Ani ; Ani Anr]; % Anr and Ani matrices pack in An
Atilta = diag(Atilta,An);
end
***************
from this i want to create state space matrices
and my Atilta matrix will be like this
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
but when i am using diag command it will give me error as following
Error using diag
K-th diagonal input must be an integer scalar.
Error in modeNmatrices (line 41)
Atilta = diag(Atilta,An);
Answers (1)
Walter Roberson
on 5 May 2013
When you use the two-argument form of diag(), the second argument needs to be the number of the diagonal. You cannot use diag() to directly modify an existing array.
I am confused: your error message uses diag() but your code uses blkdiag() which can take an array as its second argument.
4 Comments
See Also
Categories
Find more on Dynamic System Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!