MATLAB cannot integrate trigonometry in the form of degree
11 views (last 30 days)
Show older comments
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.
Answers (2)
KSSV
on 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
on 26 Nov 2020
Edited: John D'Errico
on 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])
is indeed not the same as the integral of the green curve.
int(cosd(x),[-90,90])
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))
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
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.
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!