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

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

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

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.
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)

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

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.
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.
Thank you. It's a good workaround before the release of R2021a.

Sign in to comment.

Products

Release

R2020b

Asked:

aaa
on 1 Dec 2020

Commented:

aaa
on 2 Dec 2020

Community Treasure Hunt

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

Start Hunting!