## Why I am not getting the same result for an integral of a piecewise function?

I set the following piecewise function to integrate it:

function lambda = weight(x,L1,L2,M)

% L1, L2, M are constants

if x < L1

lambda = (M ./ (4 .* (L1 + L2))) + ((3 .* M) / (2 .* L2 .^ 2)) .* (L1 – x);

else

lambda = M ./ (4 .* (L1 + L2)) .* ones(size(x));

end

end

Now, I am trying to integrate lambda between 0 and 15. Everything looks okay, but the results are different when I try to integrate the whole interval than when I do it by sections. In the following code, check should be 0 or near to 0, but I am getting 5.1042e+03.

lambda = @(x) weight(x,5,10,35000);

aux1 = integral(lambda,0,15)

aux2 = integral(lambda,0,5)

aux3 = integral(lambda,5,15)

check1 = aux1 – aux2 + aux3

Does anyone know what is the issue here?

I know it work fine by sections but I would like it to do it as a whole.I set the following piecewise function to integrate it:

function lambda = weight(x,L1,L2,M)

% L1, L2, M are constants

if x < L1

lambda = (M ./ (4 .* (L1 + L2))) + ((3 .* M) / (2 .* L2 .^ 2)) .* (L1 – x);

else

lambda = M ./ (4 .* (L1 + L2)) .* ones(size(x));

end

end

Now, I am trying to integrate lambda between 0 and 15. Everything looks okay, but the results are different when I try to integrate the whole interval than when I do it by sections. In the following code, check should be 0 or near to 0, but I am getting 5.1042e+03.

lambda = @(x) weight(x,5,10,35000);

aux1 = integral(lambda,0,15)

aux2 = integral(lambda,0,5)

aux3 = integral(lambda,5,15)

check1 = aux1 – aux2 + aux3

Does anyone know what is the issue here?

I know it work fine by sections but I would like it to do it as a whole. I set the following piecewise function to integrate it:

function lambda = weight(x,L1,L2,M)

% L1, L2, M are constants

if x < L1

lambda = (M ./ (4 .* (L1 + L2))) + ((3 .* M) / (2 .* L2 .^ 2)) .* (L1 – x);

else

lambda = M ./ (4 .* (L1 + L2)) .* ones(size(x));

end

end

Now, I am trying to integrate lambda between 0 and 15. Everything looks okay, but the results are different when I try to integrate the whole interval than when I do it by sections. In the following code, check should be 0 or near to 0, but I am getting 5.1042e+03.

lambda = @(x) weight(x,5,10,35000);

aux1 = integral(lambda,0,15)

aux2 = integral(lambda,0,5)

aux3 = integral(lambda,5,15)

check1 = aux1 – aux2 + aux3

Does anyone know what is the issue here?

I know it work fine by sections but I would like it to do it as a whole. integral, piecewise function, function, function handle MATLAB Answers — New Questions