Why I get different values when I integrate a function (by my hand-Displacement Positive & Negative, cumtrapz-Displacement Positive)?

1 view (last 30 days)
Why I get different values when I integrate a function (by my hand, cumtrapz)?
As you can see below, there is a very big difference when I integrate the function with cumtrapz.
%% Simple
t = 0:1:5; % Time (s)
a = 10*cos(20*t); % Acceleration (m/s^2)
VALUES:
10
4,08082061813392
-6,66938061652262
-9,52412980415156
-1,10387243839048
8,62318872287684
v = sin(20*t)/2; % Velocity (m/s)
VALUES:
0
0,456472625363814
0,372556580239674
-0,152405310551108
-0,496944326961688
-0,253182820554879
x = -cos(20*t)/40; % Displacement (m)
VALUES:
-0,0250000000000000
-0,0102020515453348
0,0166734515413065
0,0238103245103789
0,00275968109597619
-0,0215579718071921
%% Cumtrapz
t = 0:1:5; % Time (s)
a_cumtrapz = 10*cos(20*t); % Acceleration (m/s^2) with Cumtrapz
VALUES:
10
4,08082061813392
-6,66938061652262
-9,52412980415156
-1,10387243839048
8,62318872287684
v_cumtrapz = cumtrapz(t,a_cumtrapz); % Velocity (m/s) with Cumtrapz
VALUES:
0
7,04041030906696
5,74613030987261
-2,35062490046448
-7,66462602173550
-3,90496787949232
x_cumtrapz = cumtrapz(t,v_cumtrapz); % Displacement (m) with Cumtrapz
VALUES:
0
3,52020515453348
9,91347546400327
11,6112281687073
6,60360270760734
0,818805756993430

Accepted Answer

Star Strider
Star Strider on 12 Apr 2016
When you evaluated your hand-calculated analytic integrals, you did not subtract the initial value of the function, as is necessary in evaluating integrals.
If you give cumtrapz a vector of smaller intervels, it performs quite well:
t = linspace(0, 5, 1E+6);
ad = 10*cos(20*t); % The ‘cumtrapz’ Evaluations
ade = ad(end)
vd = cumtrapz(t, ad);
vde = vd(end)
xd = cumtrapz(t, vd);
xde = xd(end)
va = sin(20*t)/2; % The Analytical Evaluations
vae = va(end)-va(1)
xa = -cos(20*t)/40;
xae = xa(end)-xa(1)
These yield:
vde =
-253.1828e-003
xde =
3.4420e-003
vae =
-253.1828e-003
xae =
3.4420e-003
Otherwise, it’s not a fair comparison. in your initial post Your analytic solutions assume infinitesimal intervals, and you’re asking cumtrapz to use very large intervals. Give cumtrapz a level playing field, and evaluate your analytic solutions correctly, and the two are comparable.

More Answers (0)

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!