An interesting problem in MATLAB.
Normally, when doing hand-calculation for trigonometry, we can either calculate the equations in degree or in radian. However, the integral of trigonometry in MATLAB (other math-softwares as well) is a different story.
Lets start with a simple example, the integral of "cos(x)" from -pi/2 to pi/2.
If calculate by hand, you will find the answer is "sin(pi/2) - sin(-pi/2)", which equals to 2. Also, if using degree, it becomes "sind(90) - sind(-90) = 2". (I use "sind" to distinguish them, but in real-world when we calculate by hand, we still use "sin" when it is degree)
In MATLAB, you can write codes to calculate this using functions such as "integral", "int" and "vpaintegral". I use "int" here, as shown below.
syms x
f = cos(x);
int(f,-pi/2,pi/2)
Then, MATLAB will gives you the correct answer, which is 2.
However, if you use degree for this calculation, the codes should be written as below.
syms x
f = cosd(x);
int(f,-90,90)
The answer given is 360/pi, which is 114.65. A big difference. This answer is incorrect compare with what we expected.
What happened?
After discovery, I found that this issue is caused by MATLAB's internal algorithm when dealing with trigonometry in degree (cosd, sind, tand etc.)
MATLAB turns "cosd(x)" into "cos(x*pi/180)" when you use the degree form of trigonometry.
After integral cos(x*pi/180) becomes (pi/180)*sin(x*pi/180). Therefore, the calculation over [-90 90] becomes
(180/pi)* ((sin(90*pi/180))-(sin(-90*pi/180)))
If you just look at the second polynomial, it gives you 2, which is the answer it should be, but there is a (180/pi) at the front, which leads to an incorrect answer. At this time, the result is 180*2/pi.
Overall, the conclusion is that, NEVER calculate integral of trigonometry with degree, but RADIAN instead. For other complicated non-integral calculations, it is also better to use RADIAN as the form of trigonometry.
In addition, I think MATLAB can solve this problem so that we can use degree to do this calculation without error.
3 Comments
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/663588-matlab-cannot-integrate-trigonometry-in-the-form-of-degree#comment_1162308
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/663588-matlab-cannot-integrate-trigonometry-in-the-form-of-degree#comment_1162308
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/663588-matlab-cannot-integrate-trigonometry-in-the-form-of-degree#comment_1162438
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/663588-matlab-cannot-integrate-trigonometry-in-the-form-of-degree#comment_1162438
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/663588-matlab-cannot-integrate-trigonometry-in-the-form-of-degree#comment_1163763
Direct link to this comment
https://it.mathworks.com/matlabcentral/answers/663588-matlab-cannot-integrate-trigonometry-in-the-form-of-degree#comment_1163763
Sign in to comment.