Array not positive real value?

2 views (last 30 days)
Braden Kerr on 9 Nov 2020
Answered: Athul Prakash on 12 Nov 2020
Hello, I am having trouble plotting this data. I have the code below and I keep getting an error regarding "Array indices must be positive
integers or logical values."
syms x
fun = 1-(nu/2)*i*sin(x)-nu^2*sin(x/2)^2 -nu*sin(x/2)^2- nu/2*1i*sin(x)+(nu*(nu-1)/4)*(1-2*exp(-1i*x)+exp(-1i*2*x));
R = matlabFunction(real(fun), 'vars', x);
I = matlabFunction(imag(fun), 'vars', x);
range = [0:.01:pi];
z= [];
for i = 1:length(range)-1
j = i*.01;
R(i)
I(i)
z(1,i+1) = abs(fun(j)); %sqrt(R(j)^2+I(j)^2);
end
Any help would be apprecaited. Also, the commented out code is just a different method I was using earlier that does the same as the abs()
Geoff Hayes on 9 Nov 2020
Braden - which line of code is generating the error? Typically, the "Array indices must be positive integers or logical value" error is telling you that you are using an invalid index into an array. From your code, it looks like i is always an integer and so the use of R(i), I(i), and z(1,i+1) are probably valid. Depending upon what fun is (a function? or an array?) then given that j is not an integer, fun(j)) may be invalid. (I don't have the symbolic toolbox so it isn't clear to me what fun would be.)

Athul Prakash on 12 Nov 2020
As others have pointed out, this error message typically occurs when you're indexing an array with incompatible values (usually decimals, negatives or non-numeric types).
There seems to be a lot of indexing in the code attached; could you post the full error message, including the line where the error has occured?
At first glace, I'm not sure that 'fun' got assigned properly, there could be a bug. You may check that 'fun' works as expected before invoking 'fun(j)' later.

R2020a

Community Treasure Hunt

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

Start Hunting!