Array indices must be positive integers or logical values.

Hi everyone! I'm writing a code of a constraint for the fmincon function but i don't understand what i'm doing wrong with the index. This is my code:
-lcon.m
function [c,ceq] = lcon(u, x_i, y_i, theta_i, x_f, y_f, theta_f, alphamin, alphamax)
c = zeros([51*2, 1])
for (s=0:0.02:1)
k_i = u(1)
k_f = u(2)
c((2*s/0.02+1)) = alphamax * ((9*s^4*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))^2 + 4*s^2*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i))^2 + k_i^2*cos(theta_i)^2 + k_i^2*sin(theta_i)^2 + 9*s^4*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))^2 + 4*s^2*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))^2 - 12*s^3*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)) - 12*s^3*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) - 4*k_i*s*cos(theta_i)*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) - 4*k_i*s*sin(theta_i)*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)) + 6*k_i*s^2*cos(theta_i)*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i)) + 6*k_i*s^2*sin(theta_i)*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)))^(1/2)) -((6*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)) - 6*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)))*s^2 + (6*k_i*sin(theta_i)*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i)) - 6*k_i*cos(theta_i)*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)))*s - 2*k_i*sin(theta_i)*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) + 2*k_i*cos(theta_i)*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)))/(k_i^2*cos(theta_i)^2 - s*(4*k_i*sin(theta_i)*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)) + 4*k_i*cos(theta_i)*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))) + k_i^2*sin(theta_i)^2 + s^4*(9*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))^2 + 9*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))^2) - s^3*(12*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) + 12*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i))) + s^2*(4*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))^2 + 4*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i))^2 + 6*k_i*sin(theta_i)*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)) + 6*k_i*cos(theta_i)*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))))
c((2*s/0.02+2)) = alphamin * ((9*s^4*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))^2 + 4*s^2*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i))^2 + k_i^2*cos(theta_i)^2 + k_i^2*sin(theta_i)^2 + 9*s^4*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))^2 + 4*s^2*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))^2 - 12*s^3*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)) - 12*s^3*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) - 4*k_i*s*cos(theta_i)*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) - 4*k_i*s*sin(theta_i)*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)) + 6*k_i*s^2*cos(theta_i)*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i)) + 6*k_i*s^2*sin(theta_i)*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)))^(1/2)) - (-((6*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)) - 6*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)))*s^2 + (6*k_i*sin(theta_i)*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i)) - 6*k_i*cos(theta_i)*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)))*s - 2*k_i*sin(theta_i)*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) + 2*k_i*cos(theta_i)*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)))/(k_i^2*cos(theta_i)^2 - s*(4*k_i*sin(theta_i)*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i)) + 4*k_i*cos(theta_i)*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))) + k_i^2*sin(theta_i)^2 + s^4*(9*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))^2 + 9*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))^2) - s^3*(12*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i))*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i)) + 12*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i))*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i))) + s^2*(4*(3*x_i - 3*x_f + k_f*cos(theta_f) + 2*k_i*cos(theta_i))^2 + 4*(3*y_i - 3*y_f + k_f*sin(theta_f) + 2*k_i*sin(theta_i))^2 + 6*k_i*sin(theta_i)*(2*y_i - 2*y_f + k_f*sin(theta_f) + k_i*sin(theta_i)) + 6*k_i*cos(theta_i)*(2*x_i - 2*x_f + k_f*cos(theta_f) + k_i*cos(theta_i)))))
end
ceq=[];
end
The error i get:
Array indices must be positive integers or logical values.
What can i do?

 Accepted Answer

Here is a minimal working example of your problem
x = rand(1,15);
ind = 2*0.14/0.02+1; % occurs when s = 8
x(ind)
Error: Array indices must be positive integers or logical values
It would seem that the value of ind is 15, but it is not.
ind - 15
% ans = 1.77635683940025e-15
So you are trying to access x at a non-integer position. The easiest solution is to round (in some manner) to an integer.
x(round(ind))
% and this works!
Also see fix, ceil, and floor for other ways to round.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!