index must be a positive integer or logical

1 view (last 30 days)
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 :D

Accepted Answer

Star Strider
Star Strider on 4 Apr 2015
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')
  2 Comments
Star Strider
Star Strider on 4 Apr 2015
Christian Felske’s ‘Answer’ moved here...
hey ty for the fast help :D the problem is while reading what u typed i dont understand what u did :D
is there a solution close to mine which would work and im able to understand as a total newbie in matlab
Star Strider
Star Strider on 4 Apr 2015
My pleasure.
The solution is yours, with a few necessary refinements to make the code work.
I’ll step through it:
  1. I used your ‘x’, ‘y’, and ‘n’ vectors to define the ‘X’ and ‘Y’ matrices from meshgrid. MATLAB needs to create separate matrices for ‘X’ and ‘Y’ in order efficiently evaluate them (and to plot them), so your function has to be evaluated at each point separately for every value of ‘x’ and ‘y’. It is easiest to do that with the meshgrid matrices. If you look at ‘X’ and ‘Y’ you will see their structure and get an idea of how they work. Please see the documentation on Array vs. Matrix Operations for details on element-wise calculations.
  2. Creating the Anonymous Function ‘Afun’ out of your equation is convenient, because it takes advantage of MATLAB array calculations. I had to fully vectorise it to do the element-wise operations (evaluating the function at every value in the ‘X’ and ‘Y’ matrices) it requires. An anonymous function has the same properties as a function file, and is preferable to use in this instance. The alternative is using nested for loops, that are time-consuming and inefficient.
  3. I then created one for loop for ‘n’, since ‘X’ and ‘Y’ do not change. I stepped through it and created a different ‘page’ in the now three-dimensional ‘A’ matrix for each value of ‘n’.
That is the essence of my code.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!