MATLAB: How do i correct this error, “subscript indices must either be real positive integers or logicals”

This is my code so far…
theta = 10:0.5:80
Function_x(theta) = 981./cosd(theta)
Function_y(theta) = 981./sind(theta)
Function_total(theta) = sqrt(Function_x.^2 + Function_y.^2)
The error is in line 2, but it will happen for all three since they are all defined as the same variable. How do i fix this. The problem asks for the x and y pieces of tension at angle theta, which is between 10 and 80 degrees at a 0.5 degree increments. But i dont know how to fix the error about subscript indices must be real positive integers or logicals. Any help would be fantastic.

Best Answer

  • Jess - you have correctly initialized theta according to the problem but you do not have to use it on the left side of the assignment (and leads to the above error when you try to since theta contains many values that are not integers). If you want Function_x to be a vector of elements that depend upon theta then just initialize this variable as
    Function_x = 981./cosd(theta);
    The same would be true for Function_y and Function_total. (I'm a little unclear on the naming of all three variables so you may want to rename them to something that more appropriate to their purpose.)
    Try the above and see what happens!