Arrazy size problem at line 24

2 views (last 30 days)
Ahmad Zriek on 18 Mar 2023
Commented: Walter Roberson on 18 Mar 2023
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;
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)

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
Ahmad Zriek on 18 Mar 2023
could you please tell me how to rewrite it since this code isnt written by me but from the professor for an assignment that uses Matlab. Im not familiar at all with it. Thank you very much
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R2022b

Community Treasure Hunt

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

Start Hunting!