How to store the results of a loop in matlab.

1 次查看(过去 30 天)
Actually, I need to solve consecutive eigen value problems which are time dependent; accordingly, I want to extract eigen value of each time step using a ''for loop'' in matlab; the point is, however, that results of each loop is needed to be a components of a vector associated with the specific time step; in other words, I want to write a loop for t=0:1:20 that in each loop, matlab calculate the associated eigen value and put it in the vector as a component of it. so at the end of the day we are supposed to have a vector of eigen values associated with vector of time. please find matlab code here, but when I run the code I encounter with the alarm which is showing ''Array indices must be positive integers or logical values'' and I cannot find a vector of eigenvalues in the workspace as a variable.
A = zeros(1,19);
for t = 0:1:19
m(t) = [1.0, 0.89*sin(0.17*t); 0, 120.0]
k(t) = [760, 0; -2.9*sin(0.19*t), 400]
c(t) = [0.44, 0; -0.96*sin(0.17*t), 140 ]
mk = -inv(m(t))*k(t)
mc = -inv(m(t))*c(t)
B(t) = [zeros(2), eye(2); mk(t), mc(t)]
C(t) = real(sqrt(eig(B(t))))
A(t) =C(1)
end
  1 个评论
Matt J
Matt J 2022-12-2
编辑:Matt J 2022-12-2
Why are you interested only in the first eigenvalue A(t)=C(1)? How do you know the other eigenvalue is irrelevant, seeing as the order of the eigenvalues can be random?

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2022-12-2
编辑:Matt J 2022-12-2
[B,C,m,k,c]= deal(cell(1,20));
A=nan(1,20);
for t = 1:20
z=t-1;
m{t}= [1.0, 0.89*sin(0.17*z); 0, 120.0];
k{t} = [760, 0; -2.9*sin(0.19*z), 400] ;
c{t} = [0.44, 0; -0.96*sin(0.17*z), 140 ];
mk = -m{t}\k{t};
mc = -m{t}\c{t};
B{t} = [zeros(2), eye(2); mk, mc];
C{t} = real(sqrt(eig(B{t})));
A(t) =C{t}(1);
end
A
A = 1×20
3.6978 3.6978 3.6978 3.6978 3.6978 3.6977 3.6977 3.6976 3.6976 3.6976 3.6976 3.6976 3.6977 3.6977 3.6977 3.6978 3.6978 3.6978 3.6978 3.6978

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by