Why cumtrapz does not give same results as my analytic solution?

3 views (last 30 days)
I wanted to test a numerical integration routine and used the following test function:
x=linspace(-10,10,100);
xs=x/30;
y=besselj(0,xs.*x).*x;
Yan=besselj(1,xs.*x(end)).*x(end)./xs;
Ynum=cumtrapz(x,y);
hold on
plot(xs,abs(Yan).^2,'b')
plot(xs,abs(Ynum).^2,'--r')
legend('Analytic result','Numeric result');
grid on
hold off
Unfortunately my results are not the same. I hope someone could give an explanation why they are not the same, my guess there is some scaling that goes wrong.

Accepted Answer

darova
darova on 15 Mar 2021
I think it happens because of limits of integration
x = linspace(-2,3);
y = x.^2;
c0 = x(1)^3/3;
plot(x,cumtrapz(x,y),'.-r')
line(x,x.^3/3-c0) % try without c0
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!