Search code examples
matlabsymbolic-math

Matlab integration and abs


In MATLAB (R2017b, online version) I found an issue during symbolic integration: try to calculate integral of f(t) = abs(sin(t)) . Due to the fact that f(t) is always positive, I expect that the integral in [a, b] is lower equal than the integral in [a, b'] if b < b'. But:

int( abs(sin(t)), t, 0, pi )   -> 2 % OK
int( abs(sin(t)), t, 0, 2*pi ) -> 2 % NO (should be 4)

In fact, if we plot the function that represent the integral from 0 to x, which should be monotonic, we found something different:

% for each value x(i) of x we will calculate integral from 0 to x(i)
x = 0 : pi/8 : 4*pi;
% actual computation
Z = zeros(length(x), 1);                            % create array
syms t;                                             % create symbolic variable t
calculate_int = @(n) int(abs(sin(t)), t, 0, n);     % integral function
for i = 1 : length(Z)
    Z(i) = calculate_int(x(i));
end
% plot result
figure;
plot(x, Z);

results in this obviously non monotonic function:

Plot

Non symbolic integration gives no problem:

x = 0 : 0.01 : pi;
f = abs(sin(x));
value = sum(f * 0.01) % 2

x = 0 : 0.01 : 2 * pi;
f = abs(sin(x));
value = sum(f * 0.01) % 4

Solution

  • It is a bug in Matlab R2017b. See @horchler comments for details.