MATLAB cannot integrate trigonometry in the form of degree

11 vues (au cours des 30 derniers jours)
Jianming Wang
Jianming Wang le 26 Nov 2020
Commenté : James Tursa le 27 Nov 2020
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.
ans = 2
However, if you use degree for this calculation, the codes should be written as below.
syms x
f = cosd(x); % x is in degree
int(f,-90,90) % from -90 degrees to 90 degrees
The answer given is 360/pi, which is 114.65. A big difference. This answer is incorrect compare with what we expected.
ans = 360/pi
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 commentaires
Jianming Wang
Jianming Wang le 26 Nov 2020
Hi Bruno, thank you for your comment. You are right!
I am not saying that MATLAB or math is wrong, but this is an interesting calculation that everyone should be aware of.
For me, when I calculate by hand, I know I am using degree so I keep my mind using degree throughout the calculation, but machine cannot recognize that I want to get the result using degree, but follows the basic mathematical rule.
I would like to see what people think about it, how they discuss it, and how to solve this, but thank you!

Connectez-vous pour commenter.

Réponses (2)

KSSV
KSSV le 26 Nov 2020
Note that at the end, your answer will be in degrees....you can convert it to raidians and check.
syms x
f = cosd(x); % x is in degree
v = int(f,-90,90) % from -90 degrees to 90 degrees
double(v)*pi/180

John D'Errico
John D'Errico le 26 Nov 2020
Modifié(e) : John D'Errico le 26 Nov 2020
I think you do not understand integration, IF your expectation really is the two should be the same. That is, you think the integral of the function cosd(x), over limits -90,90 should be 2.
What does an integral mean? We can interpret an integral as an area. So perhaps we should plot the two functions on the same set of axes? Yes, you think of them as the same function, but they are not really the same. Strongly related, yes, via a very simple transformation. But they are not the same.
fplot(@(x) cos(x),[-pi/2,pi/2],'r')
hold on
fplot(@(x) cosd(x),[-90,90],'g')
Now, we have your claim that the two curves contain the same area underneath the curve, and above the x axis? Hmm. Really?
Maybe it is just me, but the green curve would seem to contain just a little more area. I might be mistaken though. :)
The point is real though. The definite integral of the red curve
syms x
int(cos(x),[-pi/2,pi/2])
ans = 
2
is indeed not the same as the integral of the green curve.
int(cosd(x),[-90,90])
ans = 
The difference is not MATLAB's "internal" algorithm. Just basic mathematics. So where did you go wrong?
You think it true that int(cosd(x)) is sind(x). In fact, that is wrong. sind and cosd are functions defined in terms of sin and cos. But sin and cos are functions of RADIANS. If you want to think of them in terms of degrees, you can, but you need to recognise that the true function is defined in terms of radians.
syms x
int(cosd(x))
ans = 
We could convert that back into a function using sind. But that leaves us a factor of 180/pi on the outside. Thus...
int(cosd(x)) == sind(x)*180/pi
ans = 
So your claim the integral is still 2 is just false. Again, basic mathematics. Perhaps think about an integral in terms of area, and it should become more clear.
  1 commentaire
Jianming Wang
Jianming Wang le 26 Nov 2020
Nice answer! I agree with you. Clearly explained and very detailed. Thanks!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by