Array indices must be positive integers or logical values.
2 ビュー (過去 30 日間)
古いコメントを表示
h = 0.2;
T = 4:h:8
X_s=[-5.87 -4.23 -2.55 -0.89 0.67 2.09 3.31 4.31 5.06 5.55 5.78 5.77 5.52 5.08 4.46 3.72 2.88 2.00 1.10 .23 -0.59]
for X=4:0.2:8 %%%need to take the first and last point off since central difffrencing doesnt work on them
k=[X];
x = round(k/0.2 - 4/0.2 + 1);
V = ((X_s((x)+1))-(X_s((x)-1)))/(2*h);
if X==4
k=[X];
x = round(k/0.2 - 4/0.2 + 1);
V = ((X_s((x)+1))-(X_s((x))))/(2*h);
end
if X==8
k=[X];
x = round(k/0.2 - 4/0.2 + 1);
V = ((X_s((x)))-(X_s((x-1))))/(2*h);
end
Velcoity=V
end
Hey there,
Im now trying to get the array to work with two end points where it can take them and redirect them to equations suited to solving them with the given data. However I find myself getting the error "Array indices must be positive integers or logical values". I've tried reorginizing my if statments to come first and that doesnt seem to work. If i change the start to 4.2 and the end to 7.8 it works like a charm but cant figure out why it wont accept the start as 4 and end as 8 and use the forward and backward diffrencing equations. Any help or knowledge helps and thank you for looking.
0 件のコメント
採用された回答
Walter Roberson
2021 年 7 月 10 日
h = 0.2;
X_s = [-5.87 -4.23 -2.55 -0.89 0.67 2.09 3.31 4.31 5.06 5.55 5.78 5.77 5.52 5.08 4.46 3.72 2.88 2.00 1.10 .23 -0.59]
nX = length(X_s);
for x = 1 : nX
if x == 1
V = ((X_s((x)+1))-(X_s((x))))/(2*h);
elseif x == nX
V = ((X_s((x)))-(X_s((x-1))))/(2*h);
else
V = ((X_s((x)+1))-(X_s((x)-1)))/(2*h);
end
Velocity(x) = V;
end
plot(1:nX, X_s, 'k-', 1:nX, Velocity, 'b--')
その他の回答 (1 件)
G A
2021 年 7 月 10 日
When x=1, then X_s(x-1) = X_s(0), which is not allowed in Matlab
x =
1
x =
1
x =
2
x =
3
x =
4
x =
5
x =
6
x =
7
x =
8
x =
9
x =
10
x =
11
x =
12
x =
13
x =
14
x =
15
x =
16
x =
17
x =
18
x =
19
x =
20
x =
21
x =
21
3 件のコメント
Walter Roberson
2021 年 7 月 10 日
Yes, but you have
V = ((X_s((x)+1))-(X_s((x)-1)))/(2*h);
before you test whether X == 4
参考
カテゴリ
Help Center および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!