Numerical integration¶
In this example, we will study how to integrate a vector of velocity measurements and infer the distance travelled.
Let's study the initial data¶
Suppose we have a vector of measurements of car speed taken at different points in time.
vel = [ 0, .45, 1.79, 4.02, 7.15, 11.18, 16.09, 21.90, 29.05, 29.05,
29.05, 29.05, 29.05, 22.42, 17.9, 17.9, 17.9, 17.9, 14.34, 11.01,
8.9, 6.54, 2.03, 0.55, 0 ];
vTime = 0:24;
The data are recorded at equal time intervals (every second, 24 consecutive seconds). Let's draw a graph of the observed process:
gr()
plot( vTime, vel, title="Скорость автомобиля", markershape=:star8,
xlabel="Время, с", ylabel = "Скорость, м/с", legend=false )
The positive slope corresponds to the intervals when the car was accelerating, the flat horizontal graph corresponds to movement with constant speed, the negative slope corresponds to the intervals of braking. At the moment of time t=0
the car was stationary, the velocity at this moment was equal to 0 m/s. The car accelerated until it reached a maximum speed equal to 29.05 m/s at approximately t=8
. It maintained this speed for 4 seconds. It then began to slow down to 17.9, held this speed for three seconds, and continued to brake unevenly to a complete stop.
This curve is difficult to describe by a single continuous function because it has many discontinuities of the derivative.
Let us calculate the distance travelled¶
For numerical integration we will use the package Trapz
, which can be set up as follows:
]add Trapz
The function trapz
from this package integrates a discrete time series using the trapezoidal method. This method is well suited for data with clearly observable discontinuities and assumes that the dynamics of the system changed linearly between two points.
To illustrate the idea of this method, let's draw some trapezoids on the graph with our data:
plot( vTime, vel, title="Скорость автомобиля", markershape=:star8,
xlabel="Время, с", ylabel = "Скорость, м/с", legend=false )
plot!( vTime, vel, fillrange=zeros(length(vel)), fillcolor=:blue, label=:none, lw=2, lc=:black )
for i in 1:length(vTime)
plot!( [vTime[i],vTime[i]], [0,vel[i]], lw=2, lc=:black )
end
plot!()
The function trapz
will allow us to calculate the area under any piecewise linear curve, through the sum of the areas of the trapezoids under each interval.
Let's calculate the distance travelled by the car during the observation time (the area of the whole coloured area on the graph) using numerical integration.
using Trapz
distance = trapz( vTime, vel )
So, in 24 seconds the car has travelled 345.22 m.
Let's draw a graph of the distance travelled¶
In the library NumericalIntegration
there is one more function that will be useful for us - cumul_integrate
.
]add NumericalIntegration
The function cumul_integrate
also performs numerical integration, but returns not the area under the whole curve, but the accumulated sum at each observation point of the initial value.
using NumericalIntegration: cumul_integrate
cdistance = cumul_integrate( vTime, vel );
We have calculated how much of the distance the car travelled from the origin to each timestamp, now let's plot the graph.
plot( cdistance, title = "Расстояние, пройденное автомобилем",
xlabel = "Время, с", ylabel = "Расстояние (м)", legend=false )
Conclusion¶
We have applied a couple of simple functions to integrate discrete time series (vector of measured velocity) and obtained the distance travelled over the entire observation time and a graph of overcoming this for each time segment.