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));
end
figure(1)
surf(A(:,:,20))
hold on
for k1 = 2:length(n)
surf(A(:,:,k1)+n(k1))
end
hold off
xlabel('X')
ylabel('Y')
zlabel('n')
Best Answer