Arrazy size problem at line 24
1 view (last 30 days)
Show older comments
I cant get the this code to work
Acc.txt is just 0,1,0,-1,0,1
Time.txt is just 1,2,3,4,5,6. It keeps giving me
Index exceeds the number of array elements. Index must not exceed 7.
Error in Q2 (line 23)
uw(j+1) = hw(j)*p(j+1);
Any help is greatly appreciated
clc;
clear all;
close all;
load Acc.txt
load Time.txt
fs=10; T=6.4;
N=fs*T;
w0=(2*pi)/T;
m=44302.43;
k=7*10^6;
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)+ (1i*(2*chi*(w(j)/wn))))); %#ok<*SAGROW>
uw(j+1) = hw(j)*p(j+1);
vw(j+1)= 1i*w(j)*hw(j)*p(j+1);
end
us=ifft(uw);
vs=ifft(vw);
plot(Time, us)
0 Comments
Answers (1)
Walter Roberson
on 18 Mar 2023
P= (-m)*(Acc);
p=fft(P);
m is a scalar and Acc has 6 elements so P has 6 elements. fft with one parameter returns something the same size so p has 6 elements.
You then loop j to 63-ish and ask for p(j+1) but only up to 6 exists
2 Comments
Walter Roberson
on 18 Mar 2023
Why rewrite it? It is doing what it is designed to do, which is to deliberately create an error message when it is run. The error message is the purpose of the code, and it does not need to be fixed.
See Also
Categories
Find more on Matrix Indexing 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!