Arrazy size problem at line 24
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2023-3-18
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 个评论
Walter Roberson
2023-3-18
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!