help with a vibratonal analysis code

2 views (last 30 days)
hi am still getting a hang of MATLAB havent been using it for so long and i keep getting an error message about 'The size of the indicated variable or array appears to be changing with each loop iteration.' been trying to fix this with no luck. i am doing a free vibration analysis of a 2 dof cantilever beam may i get help with my code. below is my whole script
m1=53;% Mass
m2=62.096;
k=3.096e+006 ; % Stiffness
c=150; % Damping
% 4 x 4 matrices
disp('4 x 4 Mass matrix');
mt=[c,0,m1,0;0,0,0,m2;m1,0,0,0;0,m2,0,0];
disp('4 x 4 stiffness matrix');
kt=[-m1,0,0,0;0,m2,0,0;0,0,3*k,-2*k;0,0,-2*k,2*k];
Z=mt\kt;
[V,D]=eig(Z);
disp('Eigenvalues');
V;
disp('Initial conditions');
x0=[0;0;0.005;0];
disp('Integration constants');
S=V\x0;
tk=linspace(0,2,101);
% Evaluation of time dependent response
% Recall that x1=y3 and x2=y4
for k=1:101
t=tk(k);
for i=3:4
x(k,i-2)=0;
for j=1:4
x(k,i-2)=0;
x(k,i-2)=x(k,i-2)+(real(S(j))*real(V(i,j))-imag(S(j))*imag(V(i,j)))*cos(imag(D(j,j))*t);
x(k,i-2)=x(k,i-2)+(imag(S(j))*real(V(i,j))-real(S(j))*imag(V(i,j)))*sin(imag(V(i,j))*t);
x(k,i-2)=x(k,i-2)*exp(-real(D(j,j))*t);
end
end
end
plot(tk,x(:,1),'-',tk,x(:,2),':')
title('free vibration of a viscously damped 2 DOF')
xlabel('t[sec]')
ylabel('x(m)')
legend('x1(t)','x2(t)')

Accepted Answer

Stephan
Stephan on 18 Aug 2020
Edited: Stephan on 18 Aug 2020
m1=53;% Mass
m2=62.096;
k=3.096e+006 ; % Stiffness
c=150; % Damping
% 4 x 4 matrices
disp('4 x 4 Mass matrix');
mt=[c,0,m1,0;0,0,0,m2;m1,0,0,0;0,m2,0,0];
disp('4 x 4 stiffness matrix');
kt=[-m1,0,0,0;0,m2,0,0;0,0,3*k,-2*k;0,0,-2*k,2*k];
Z=mt\kt;
[V,D]=eig(Z);
disp('Eigenvalues');
V;
disp('Initial conditions');
x0=[0;0;0.005;0];
disp('Integration constants');
S=V\x0;
tk=linspace(0,2,101);
% Evaluation of time dependent response
% Recall that x1=y3 and x2=y4
% preallocate x
x = zeros(numel(tk),2);
for k=1:101
t=tk(k);
for i=3:4
x(k,i-2)=0;
for j=1:4
x(k,i-2)=0;
x(k,i-2)=x(k,i-2)+(real(S(j))*real(V(i,j))-imag(S(j))*imag(V(i,j)))*cos(imag(D(j,j))*t);
x(k,i-2)=x(k,i-2)+(imag(S(j))*real(V(i,j))-real(S(j))*imag(V(i,j)))*sin(imag(V(i,j))*t);
x(k,i-2)=x(k,i-2)*exp(-real(D(j,j))*t);
end
end
end
plot(tk,x(:,1),'-',tk,x(:,2),':')
title('free vibration of a viscously damped 2 DOF')
xlabel('t[sec]')
ylabel('x(m)')
legend('x1(t)','x2(t)')

More Answers (1)

Bruno Luong
Bruno Luong on 18 Aug 2020
"i keep getting an error message about 'The size of the indicated variable or array appears to be changing with each loop iteration."
This is NOT an error message, just a mlint warning message teling your code is has flawed.
However your code runs just fine.
If you want to remove it you need to preallocate the array x
x=zeros(101,2); % preallocation
for k=1:101
t=tk(k);
for i=3:4
x(k,i-2)=0;
...

Community Treasure Hunt

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

Start Hunting!