Array indices must be positive integers or logical values

3 visualizaciones (últimos 30 días)
U0 = 0.1;
V0 = 0.5;
W = 1;
t = 1;
x = linspace(0,3); % create linear spacing in x-direction
y = linspace(0,3); % create linear spacing in y-direction
XX = zeros(length(x),length(y));
YY = zeros(length(x),length(y));
%looping in i and j-directions
for i = 1:length(x)
for j = 1:length(y)
%create x & y space
XX(i,j) = x(i);
YY(i,j) = y(j);
psi(i,j) = ((U0*V0)./W)*cos(W(t-YY(i,j)./V0))-V0*XX(i,j);
I get an error message of
Array indices must be positive integers or logical values.
Error in psi(i,j) = ((U0*V0)./W)*cos(W(t-YY(i,j)./V0))-V0*XX(i,j);

Respuesta aceptada

Daniel Pollard
Daniel Pollard el 12 de Abr. de 2021
You wrote
psi(i,j) = ((U0*V0)./W)*cos(W(t-YY(i,j)./V0))-V0*XX(i,j);
Try
psi(i,j) = ((U0*V0)./W)*cos(W*(t-YY(i,j)./V0))-V0*XX(i,j);
W is not a vector or matrix, so calling W(t-YY(i,j)./V0) won't return anything sensible. I think you mean to multiply W by the bracketed term in the cos.
Side note: i and j make terrible variable names in Matlab. They already have a built in value of the complex unit, so redefining that is likely to throw errors further down the line. I tend to use ii, jj or k for loop indices to avoid this problem.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by