Why I get different values when I integrate a function (by my hand-Displacement Positive & Negative, cumtrapz-Displacement Positive)?
1 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160951/image.png)
%% 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160952/image.png)
0 个评论
采纳的回答
Star Strider
2016-4-12
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!