Int function gives strange integral
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Abhishek Routray
el 5 de Sept. de 2017
Comentada: Abhishek Routray
el 5 de Sept. de 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?
0 comentarios
Respuesta aceptada
David Goodmanson
el 5 de Sept. de 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.
Más respuestas (2)
John D'Errico
el 5 de Sept. de 2017
Editada: John D'Errico
el 5 de Sept. de 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.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!