Array indices must be positive integers or logical values.
조회 수: 1 (최근 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에서 General Applications에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!