Int function gives strange integral

5 views (last 30 days)
I was experimenting with the int() function which gave me a strange integral.
syms x;
f2 = @(x) sin(x) + 2*cos(x);
integral = int(f2, x)
which gave me:
integral =
-2*cos(x/2)*(cos(x/2) - 2*sin(x/2))
Any idea why this is the case? I expected something along the lines of -cos(x) - 2*sin(x); is this the same expression written differently or is my code written incorrectly, or is something else going on?

Accepted Answer

David Goodmanson
David Goodmanson on 5 Sep 2017
Hi Abhishek,
I believe you meant -cos(x) + 2*sin(x) is expected. The answer you got is equal to -cos(x) + 2*sin(x) -1, so the answer included a constant of integration of -1. It might be strange, but it's not wrong. Disappointing, let's say.
  1 Comment
Abhishek Routray
Abhishek Routray on 5 Sep 2017
Yes, I meant positive 2*sin(x). I completely forgot that it could be the constant term, thank you for the reminder!

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 5 Sep 2017
Use the simplify() function

John D'Errico
John D'Errico on 5 Sep 2017
Edited: John D'Errico on 5 Sep 2017
This is a basic problem of symbolic methods. Sometimes they generate a result that while technically correct, it looks strange to our eyes.
Here, it seems clear to recognize that each piece of the integrand is trivially integrated, and that integration is a linear operator, so we can integrate each piece simply. So this is a problem that can be done using paper and pencil on the back of an envelope, or just in your head.
syms x
simplify(int(sin(x) + 2*cos(x)) - (-cos(x) + 2*sin(x)))
ans =
-1
Since we know what answer we expect, we can subtract it from the result that int gives, and then apply simplify. Simplify is a powerful tool, that often people forget to apply to their results. Here is shows us the difference is a constant, and of course, we know that an integral is only know to within an additive constant of integration.
In fact, if we do part of the work for MATLAB, we will get the answer we expect to see.
int(sin(x)) + int(2*cos(x))
ans =
2*sin(x) - cos(x)
Sometimes we need to direct a computation down the correct pathways. My guess is in the first case, int sees that hey, it can transform the problem into a different one, where some direct rule applies to compute the entire integral. Of course, we know that here that seems silly, but it did yield a technically correct result.
  1 Comment
Abhishek Routray
Abhishek Routray on 5 Sep 2017
Thank you! I never thought about applying simplify or distributing the int() function. That helps a lot.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!