index exceeds the number of array elements?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
jacob booth
am 11 Aug. 2021
Kommentiert: jacob booth
am 11 Aug. 2021
i keep getting the "index exceeds the number of array elements" error on my code it worked fine when i set the step size to 40 but when i change it to 20 i get the error.
close all
clear all
clc
X=0:20:520;
F=[0 139 298 433 685 1026 1279 1373 1490 1634 1800 1986 2417 2651 2915 3303 3516 3860 4216 4630 5092 5612 6184 6760 7327 7581];
h=X(2)-X(1);
n=length(X);
xForward=X(1:n-1);
dFForward=(F(2:n)-F(1:n-1))/h;
xBackward=X(2:end);
dFBackward=(F(2:n)-F(1:n-1))/h;
xCenteral=X (2:n-1);
dFCentral=(F(3:n)-F(1:n-2))/(2*h);
hold all
figure(1);plot (xCenteral,dFCentral,'r' )
figure(1);plot (xForward,dFForward, 'k')
figure(1);plot (xBackward,dFBackward,'g')
legend ('centeral','Forward','Backward')
title('NUMERICAL DIFFERENCING USING STEP SIZE 20s')
xlabel ('Time')
ylabel ('Acceleration')
grid
clear;
clear;
2 Kommentare
Yazan
am 11 Aug. 2021
The number of elements in the vector F is one less than that of the vector X. Fix that.
Akzeptierte Antwort
Dave B
am 11 Aug. 2021
Bearbeitet: Dave B
am 11 Aug. 2021
The line of code: dFForward=(F(2:n)-F(1:n-1))/h; looks for F ranging from 2:n. n is 27, length(F) is 26. You've requested the 27th element of a 26 element array.
X=0:20:520;
F=[0 139 298 433 685 1026 1279 1373 1490 1634 1800 1986 2417 2651 2915 3303 3516 3860 4216 4630 5092 5612 6184 6760 7327 7581];
n=length(X)
disp(length(F))
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!