Array indices must be positive integers or logical values.

3 visualizaciones (últimos 30 días)
Mark Prentice
Mark Prentice el 9 de Jul. de 2021
Editada: DGM 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.2:0.2:7.8
k=[X]
x=(((k/(0.2))-(4/(0.2))))+1
V = ((X_s((x)+1))-(X_s((x)-1)))/(2*h)
end
Trying to get the for loop to spit out an array of answers for a central diffrencing problem but cannot get the loop to run without an array error. when I do the math I'm seeing whole positive integers being imputed. Not sure whats going on and would appreciate help.

Respuesta aceptada

Matt J
Matt J el 9 de Jul. de 2021
Editada: Matt J el 9 de Jul. de 2021
Since x is not an integer, you cannot use it to look up an entry in a vector X_s(x). Perhaps the following is what you really want?
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];
V=(X_s(3:end)-X_s(1:end-2))/(2*h)
V = 1×19
-21.0500 8.3500 8.0500 7.4500 6.6000 5.5500 4.3750 3.1000 1.8000 0.5500 -0.6500 -1.7250 -2.6500 -3.4000 -3.9500 -4.3000 -4.4500 -4.4250 -4.2250
  2 comentarios
Mark Prentice
Mark Prentice el 9 de Jul. de 2021
you fixed it however I'm not sure what you did. Would you mind explaining what the 3 in X_s(3:end) and why -2 has been added, im assuming the end is till the end value of X_s. you've actually reduced it so much so I'm not sure how the start and end points (4 to 8) is being defined. Any explination would be greatly valued as I'm trying to learn how to reproduce this with other equations.
DGM
DGM el 10 de Jul. de 2021
Editada: DGM el 10 de Jul. de 2021
First note that in your loop, you're not actually calculating a vector V. You're calculating V as a scalar and discarding all the results except the last one. It's assumed that's not what you intend.
Nothing in the loop depends on prior results, so the loop isn't necessary. We can look at the vectors that are being generated and look for a pattern:
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];
k=4.2:0.2:7.8
k = 1×19
4.2000 4.4000 4.6000 4.8000 5.0000 5.2000 5.4000 5.6000 5.8000 6.0000 6.2000 6.4000 6.6000 6.8000 7.0000 7.2000 7.4000 7.6000 7.8000
x=k/0.2 - 4/0.2 + 1
x = 1×19
2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000
So x is ostensibly integer-valued, varying from 2 to 20. Since x is used as the basis for indexing in X_s:
V = (X_s(x+1) - X_s(x-1))/(2*h)
... and the length of X_s is 21, then the above indices are equivalent to
V = (X_s((2+1):(20+1)) - X_s((2-1):(20-1)))/(2*h)
% or
V = (X_s(3:end) - X_s(1:end-2))/(2*h)

Iniciar sesión para comentar.

Más respuestas (1)

G A
G A el 9 de Jul. de 2021
Editada: G A el 9 de Jul. de 2021
floating point problem;
try to use x = round(k/0.2 - 4/0.2 + 1)
k =
4.200000000000000
x =
2
k =
4.400000000000000
x =
3
k =
4.600000000000001
x =
4
k =
4.800000000000001
x =
5.000000000000004
k =
5
x =
6
k =
5.200000000000000
x =
7
k =
5.400000000000000
x =
8
k =
5.600000000000001
x =
9
k =
5.800000000000001
x =
10.000000000000004

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by