# MATLAB: Index must be a positive integer or logical

sequence

Hi in order for a student work i need to calculate a sequence for a velocity.
This velocity depends on x and y, those 2 variables are valued from x 0..1 and y -1..1.
But i cant calculate this for these values because of this error.
My code looks like this:
M=10;K=10;A=zeros(M,K);for x=0:0.1:1      for y=-1:0.2:1              for n=1:20                             %Berechnung eines Gliedes              A(x,y)=((-1^n)/( ((2*n-1)* pi)/2*0.83)^3 * cos(((((2*n-1)*...              pi)/2*0.83))*y)*((exp(((((2*n-1)* pi)/2*0.83))*(x-1))*exp(((-((2*n-1)*...              pi)/2*0.83))*(x+1)))/1+exp(-2*((2*n-1)* pi)/2*0.83)));              end;      end;end;
Maybe someone is able to help me. And pls use simple explanations because im not working long with matlab 😀

• The problem was using ‘x’ and ‘y’ as indices. Indices in MATLAB can only be integers greater than zero. You would have encountered other problems, but that was throwing the error you saw.
I created your equation for ‘A’ as an anonymous function ‘Afcn’ to make it easier to work with. I used meshgrid to create the ‘X’ and ‘Y’ matrices, then looped through ‘n’, creating ‘A’ as an (11x11x20) matrix. I also vectorised the entire equation in order to get it to work in the loop as a matrix.
Your plots have extremely low ‘z’ ranges (about 0.01 to 1E-49), so you will have to plot them each individually to see them. (I would also consider using linspace to increase the resolution of ‘x’ and ‘y’.) Here, they all look like stacked flat planes, but they all do plot:
x=0:0.1:1;y=-1:0.2:1;n=1:20;[X,Y] = meshgrid(x, y);Afcn = @(x,y,n) ((-1.^n)./(((2.*n-1).*pi)./2.*0.83).^3 .* cos(((((2.*n-1).*...              pi)./2.*0.83)).*y).*((exp(((((2.*n-1).*pi)./2.*0.83)).*(x-1)).*exp(((-((2.*n-1).*...              pi)./2.*0.83)).*(x+1)))./1+exp(-2*((2.*n-1).*pi)./2.*0.83)));A = [];for k1 = 1:length(n)    A(:,:,k1) = Afcn(X,Y,n(k1));endfigure(1)surf(A(:,:,20))hold onfor k1 = 2:length(n)    surf(A(:,:,k1)+n(k1))endhold offxlabel('X')ylabel('Y')zlabel('n')