MATLAB: Index exceeds matrix dimensions

index exceeds matrix dimensions

I have to do a MATLAB code for the numerical evaluation if the frequency what I did is the following but I can't figure out why I'm getting this error: the acc.dat is a file I created on notepad with acc=[0,1,0,-1,0,1,0] and time = [0,1,2,3,4,5,6]
%Steady State Response
load acc.dat
load time.dat
fs=10; T=6.4;
N=fs*T;
w0=(2*pi)/T;
m=31744.6;
k=5000000;
chi=0.0511;
P=(-m)*(acc);
p=fft(P);
wn=12.57;
for j=1:N-1
if j>=0 & j<=N/2
w(j)=j*w0;
elseif j>N/2 & j<=N-1
w(j)=-(N-1)*w0;
end
hw(j)=(1/k)*(1/((1-(w(j)/wn)^2)+(i*(2*chi*(w(j)/wn)))));
uw(j+1)=hw(j)*P(j+1);
vw(j+1)=i*w(j)*hw(j)*P(j+1);
end
Index exceeds matrix dimensions.

Best Answer

The thrown error:
Index exceeds matrix dimensions.
Error in ... (line ###)
uw(j+1)=hw(j)*P(j+1);
Looking at your variables, N=64 so ‘j’ will go from 1 to 63. The length of ‘P’ is 7. So when ‘j’ increments to 7, the ‘(j+1)’ index overwrites the length of ‘P’, and the loop and code stops with that error.