MATLAB: Tan function giving an error

MATLABtan

x = (-pi/2)+0.01:0.01:(pi/2)-0.01; plot(x,tan(x)), grid on
gives an error: Array indices must be positive integers or logical values. but changing tan to either sin or cos does not give an error. Code above is from MATLAB.

Best Answer

Your code works when I run it.
That you are getting the error: ‘Array indices must be positive integers or logical values.’ leads me to believe that you have a variable named ‘tan’ in your workspace. This is called ‘overshadowing’ a function.
To find out, run this from your Command Window or a script:
which tan -all
If the first result is:
tan is a variable.
You have found the problem. You then have to rename the ‘tan’ variable to something meaningful, and so the name does not ‘overshadow’ any MATLAB functions.