Int function gives strange integral

5 vues (au cours des 30 derniers jours)
Abhishek Routray
Abhishek Routray le 5 Sep 2017
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?

Réponse acceptée

David Goodmanson
David Goodmanson le 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 commentaire
Abhishek Routray
Abhishek Routray le 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!

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 5 Sep 2017
Use the simplify() function
  1 commentaire
Abhishek Routray
Abhishek Routray le 5 Sep 2017
I did not realize that was a possibility. Thank you!

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 5 Sep 2017
Modifié(e) : John D'Errico le 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 commentaire
Abhishek Routray
Abhishek Routray le 5 Sep 2017
Thank you! I never thought about applying simplify or distributing the int() function. That helps a lot.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by