vpaintegral() returns wrong answer for g(t) = (mod(t,5))^2

1 view (last 30 days)
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

Bjorn Gustavsson
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
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.
aaa
aaa on 2 Dec 2020
Thank you. So on R2020b, mod() is sometimes, but not always reduced. That could really cause a mess.

Sign in to comment.

More Answers (1)

Ameer Hamza
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
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
aaa on 2 Dec 2020
Thank you. It's a good workaround before the release of R2021a.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!