vpaintegral() returns wrong answer for g(t) = (mod(t,5))^2
Show older comments
Hi,
I tried both vpaintegral() and integral() to integrate
from 0 to 10.
integral() correctly returned 83.3333, but vpaintegral() strangely returned 333.333.
I don't know why this happened. Maybe there's a bug in vpaintegral()?
Any help is much appreciated.
Thank You in advance.
Code:
syms t real
g(t) = (mod(t,5))^2
fplot(g, [0 10])
vpaintegral(g, t, 0, 10)
g_mf = matlabFunction(g, 'vars', {t})
integral(g_mf, 0, 10)
Accepted Answer
More Answers (1)
Ameer Hamza
on 1 Dec 2020
This seems to be a bug. You can use the following workaround
syms t real
g(t) = (t-floor(t/5)*5)^2
fplot(g, [0 10])
vpaintegral(g, t, 0, 10)
g_mf = matlabFunction(g, 'vars', {t})
integral(g_mf, 0, 10)
3 Comments
Walter Roberson
on 1 Dec 2020
It is not a bug. In all released versions, mod() of a symbolic expression is "reduced" in a way that typically eliminates the mod. That is the definition of the operation. It is a pain most of the time.
Starting in R2021a, mod of a symbolic expression with respect to a constant will not be reduced. But in current releases it still is.
Bjorn Gustavsson
on 1 Dec 2020
The premature reduction is a bit "curious" to me, and I'd be confident to argue that it is a "feature" that by any other name ought to be called a bug, documented behaviour or not. The idea of the symbolic calculations is that the results should be correct for any value (positive, real or complex as they may be) of the variables, the instant reduction of mod fails that, surely? Good to hear that improvments haven't stopped.
aaa
on 2 Dec 2020
Categories
Find more on Number Theory 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!