Search code examples
matlabnumerical-methodsintegral

Does MATLAB support double integration on single variable numerically?


I have a long vector

acceleration=[6.45, 6.50, 6.52, 6.32, .... , 4.75]

Already obtained from a simulink (takes a very long time to run the simulink model again). And assume the step time is constant dt=0.1

Is there any way to find the final position numerically?

I know there is a trapz function an I know that MATLAB supports 2D integration and I know that integration inside simulink is an option. However, is there any way to take double integration:

final_position= integrate integrate acceleration dt^2

Numerically, with a precision not worse than trapezoidal method?

I prefer to avoid loop based solution.


Solution

  • Using the trapezoidal rule:

    vinit = 0;                                    % initial velocity
    pinit = 0;                                    % initial position
    
    velocity = zeros(size(acceleration)) + vinit; % velocity vector
    position = zeros(size(acceleration)) + pinit; % position vector
    
    velocity(2:end) = velocity(2:end) + 0.5 * dt * cumsum(acceleration(2:end)+acceleration(1:end-1));
    position(2:end) = position(2:end) + 0.5 * dt * cumsum(    velocity(2:end)+    velocity(1:end-1));
    

    I'm using cumsum() (cumulative sum) to calculate the integral at each point, not just the overall sum; this means that the velocity can be integrated again to get the position. The final position is obviously position(end).