Array indices must be positive integers or logical values
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Aneesa Shahbaz
il 12 Apr 2021
Commentato: Aneesa Shahbaz
il 14 Apr 2021
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);
0 Commenti
Risposta accettata
Daniel Pollard
il 12 Apr 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!