Int function gives strange integral

27 ビュー (過去 30 日間)
Abhishek Routray
Abhishek Routray 2017 年 9 月 5 日
コメント済み: Abhishek Routray 2017 年 9 月 5 日
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?

採用された回答

David Goodmanson
David Goodmanson 2017 年 9 月 5 日
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 件のコメント
Abhishek Routray
Abhishek Routray 2017 年 9 月 5 日
Yes, I meant positive 2*sin(x). I completely forgot that it could be the constant term, thank you for the reminder!

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 9 月 5 日
Use the simplify() function
  1 件のコメント
Abhishek Routray
Abhishek Routray 2017 年 9 月 5 日
I did not realize that was a possibility. Thank you!

サインインしてコメントする。


John D'Errico
John D'Errico 2017 年 9 月 5 日
編集済み: John D'Errico 2017 年 9 月 5 日
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 件のコメント
Abhishek Routray
Abhishek Routray 2017 年 9 月 5 日
Thank you! I never thought about applying simplify or distributing the int() function. That helps a lot.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by