vpaintegral() returns wrong answer for g(t) = (mod(t,5))^2
1 view (last 30 days)
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)
0 Comments
Accepted Answer
Bjorn Gustavsson
on 1 Dec 2020
Peculiar. If you look at g(t) you will see (well in my version of matlab):
g(t) = (mod(t,5))^2
g(t) =
t^2
Which shows that the mod is instantly evaluated. Further it didn't work to define five as a symbolic 5 either, the mod was determined. Peculiarly it did not work to include it explicitly in the vpaintegral either:
vpaintegral(mod(t,5)^2, t, 0, 10)
ans =
333.333
This was an interesting "feature".
2 Comments
Ameer Hamza
on 1 Dec 2020
On R2020b, it doesn't instantly evaluate mod; it shows the correct expression
>> g
g(t) =
mod(t, 5)^2
Even fplot() shows correct plot. But for some reason vpaintegral() ignores the mod() function.
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
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.
See Also
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!