Int function gives strange integral

조회 수: 5 (최근 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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by