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!