Numerical Integration MATLAB vs Python/VBA
11 views (last 30 days)
Show older comments
I have a set of Acceleration-Time data which I am numerically integrating twice to get Velocity-Time and Displacement-Time. When using the cumtrapz function on Matlab it gives me the expected results from the data given. Upon doing the same thing on Python & VBA the results, specifically the displacement-time graph, keeps on rising and rising resulting in non-coinciding values, as shown in the graph below.
What could be the problem in the Python/VBA code for it to be rising up like that, and what does MATLAB do differently which achieves the correct value.
1 Comment
Torsten
on 9 Oct 2024
What could be the problem in the Python/VBA code for it to be rising up like that, and what does MATLAB do differently which achieves the correct value.
How could we know without seeing neither your data nor your code ? And wouldn't this be a question for a Python forum ?
Answers (1)
John D'Errico
on 9 Oct 2024
Edited: John D'Errico
on 9 Oct 2024
This is difficult to say, because we know virtually nothing.
First, does the problem arise from the use of subtly different data? Far too often, someone sees a difference between what they claim to be the identical mathematics in two different environments, but in fact, they truncated the data to only a few decimal digits when they moved the data. I know, you would NEVER have done that. Or would you?
Next, is this a question of the use of different integration methods. yes, I know people think the integral is the integral. BUT IT ISN'T! ANY numerical integration ends up being just an approximation at some level. Cumtrapz uses a cumulative trapezoidal rule. Did the python tool use a lower order integration (perhaps rectangle rule?) or a higher order integration (perhaps something like a Simpson's rule?)
We don't know any of this. And we are not given that information. The difference LOOKS like a method difference to my eyes, with a gradually growing bias. But if you truncated the accelerations to only a few digits, moving all the accelerations in essentially one direction, that too might have a similar impact.
And of course, we dont even have any data, So there is nothing we can even compare. If you want a better answer, then you need to provide your data. At the very least then we could test it out, changing the method, or truncating the numbers, to see if we could replicate that behavior. Until then, its just an educated guess.
2 Comments
Torsten
on 11 Oct 2024
Edited: Torsten
on 11 Oct 2024
Instead of using "cumtrapz", I suggest you try the following code in MATLAB.
If you get the same results as with VBA, you know that the software is not responsible for the different results in MATLAB and VBA, but your integration code.
% Set Initial Velocity & Displacement to zero
velocity = 0;
displacement = 0;
mass = data(2,3); % Mass of Jumper
for i = 2 : lastRow
time1 = data(i, 1);
force1 = data(i, 2).Value;
if i < lastRow
time2 = data(i + 1, 1);
force2 = data(i + 1, 2);
else
time2 = time1;
force2 = force1;
end
%Integrating acceleration to get velocity using trapezium rule
trapeziumArea = ((force1 + force2) / 2) * (time2 - time1) / mass;
velocity = velocity + trapeziumArea;
%Integrating velocity to get displacement using trapezium rule
trapeziumArea = ((velocity + (velocity - trapeziumArea)) / 2) * (time2 - time1);
displacement = displacement + trapeziumArea;
% Output velocity and displacement
data(i, 4) = velocity;
data(i, 5) = displacement;
end
See Also
Categories
Find more on Matrix Indexing 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!