I am getting error, "array indices must be positive or logical integer values". Please help

1 view (last 30 days)
Mehak S
Mehak S on 23 Feb 2022
Commented: Mehak S on 23 Feb 2022
[X,Y] = meshgrid(-4:0.2:4);
n=2;
k=1;
%n=1, k=1
w1=-sqrt(k.^2+2*n+1);
w2=sqrt(k.^2+2*n+1);
w3=k./(k.^2+2*n+1);
phi_func = @(n,Y) exp(-0.5*Y.^2).*hermiteH(n,Y);
phi_x=exp(1i*k.*X);
v=1i*(w1.^2-k.^2).*phi_func(n,Y).*phi_x;
u=(0.5*(w1-k).*phi_func(n+1,Y)+n(w1+k).*phi_func(n-1,Y)).*phi_x;
z=(0.5*(w1-k).*phi_func(n+1,Y)-n(w1+k).*phi_func(n-1,Y)).*phi_x;
U=real(u);
V=real(v);
Z=real(z)
contour(X,Y,Z,10)
colorbar
hold on
quiver(X,Y,U,V,'r')

Answers (1)

Alan Stevens
Alan Stevens on 23 Feb 2022
You need multiplication signs between n and (w1+k) in calculations of u and z:
[X,Y] = meshgrid(-4:0.2:4);
n=2;
k=1;
%n=1, k=1
w1=-sqrt(k.^2+2*n+1);
w2=sqrt(k.^2+2*n+1);
w3=k./(k.^2+2*n+1);
phi_func = @(n,Y) exp(-0.5*Y.^2).*hermiteH(n,Y);
phi_x=exp(1i*k.*X);
v=1i*(w1.^2-k.^2).*phi_func(n,Y).*phi_x;
u=(0.5*(w1-k).*phi_func(n+1,Y)+n*(w1+k).*phi_func(n-1,Y)).*phi_x;
z=(0.5*(w1-k).*phi_func(n+1,Y)-n*(w1+k).*phi_func(n-1,Y)).*phi_x;
U=real(u);
V=real(v);
Z=real(z);
contour(X,Y,Z,10)
colorbar
hold on
quiver(X,Y,U,V,'r')

Community Treasure Hunt

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

Start Hunting!