Borrar filtros
Borrar filtros

How to store the results of a loop in matlab.

1 visualización (últimos 30 días)
Elyar Ghaffarian Dallali
Elyar Ghaffarian Dallali el 2 de Dic. de 2022
Editada: Matt J el 2 de Dic. de 2022
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 comentario
Matt J
Matt J el 2 de Dic. de 2022
Editada: Matt J el 2 de Dic. de 2022
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?

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 2 de Dic. de 2022
Editada: Matt J el 2 de Dic. de 2022
[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

Categorías

Más información sobre Linear Algebra en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by