This is very clearly a bug, and I've already submitted a bug report with The MathWorks. You can confirm it by plotting the integrand and noting it is always positive over the range [0 d]
, thus assuring that the integral should yield a positive value:
h = [];
for d = 1:5,
y = linspace(0, d, 1000);
h = [h; plot(y, f(y, d))];
hold on;
end
legend(h, strcat({'d = '}, int2str((1:5).')));
xlabel('y');
ylabel('f(y)');
title('f(y) = y^2*sqrt(d*y - y^2)');
Update #1:
A response from The MathWorks suggest that this may be an issue with the MuPad command limit
. Below is the indefinite integral found in MuPad:
Evaluating this at y=d
gives the correct result, but evaluating it in the limit as y
approaches 0 gives different results based on whether d
is substituted before or after the limit calculation. Here's an example with d=1
:
Note the change in sign of the first term. In this case, substituting for d
before the limit calculation results in a positive (and correct) evaluation of the integral. MATLAB therefore appears to be substituting for d
after the limit calculation, giving the erroneous negative result for the definite integral.
Update #2:
I received a follow-up response stating that this bug has now been addressed in the latest release, R2018b. I was able to confirm in the R2018b pre-release that the two limit calculations above produce the same result, and that the integration result now has the proper sign:
syms y d
assume(d >= 0)
int(y^2*sqrt(-y^2+d*y), y, 0, d)
ans =
(5*pi*d^4)/128