Array indices must be positive integers or logical values.
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mark Prentice
el 10 de Jul. de 2021
Comentada: Mark Prentice
el 10 de Jul. de 2021
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 comentarios
Respuesta aceptada
Walter Roberson
el 10 de Jul. de 2021
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--')
Más respuestas (1)
G A
el 10 de Jul. de 2021
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 comentarios
Walter Roberson
el 10 de Jul. de 2021
Yes, but you have
V = ((X_s((x)+1))-(X_s((x)-1)))/(2*h);
before you test whether X == 4
Ver también
Categorías
Más información sobre General Applications en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!