MATLAB: Do I get “array indices must be positive integers or logical values” when running this for loop

anglearray indices;for loop

i_theta_max =100;
for i_theta = 0:i_theta_max
theta = i_theta/(i_theta_max)*2*pi;
xcoord(i_theta)=1*sin(theta);
ycoord(i_theta)=1*cos(theta);
end

Best Answer

Hi Spencer,
The access of i_theta in xcoord and ycoord is the issue. In MATLAB, indexing is one based.
Try to update as folllowing:
i_theta_max =100;
for i_theta = 0:i_theta_max
theta = i_theta/(i_theta_max)*2*pi;
xcoord(i_theta+1)=1*sin(theta); % Added 1

ycoord(i_theta+1)=1*cos(theta); % Added 1
end
% or
i_theta_max =100;
for i_theta = 1:i_theta_max+1
theta = (i_theta-1)/(i_theta_max)*2*pi; % Subtract 1
xcoord(i_theta)=1*sin(theta);
ycoord(i_theta)=1*cos(theta);
end
Hope this helps.
Regards,
Sriram
Related Question