index exceeds the number of array elements?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Yazan
2021-8-11
The number of elements in the vector F is one less than that of the vector X. Fix that.
采纳的回答
Dave B
2021-8-11
编辑:Dave B
2021-8-11
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!