Calculating the error sum of two vectors
There are different possibilities to calculate the numerical error between a target value and an actual value. Measuring the difference between signals consisting of many data points usually involves loops and subtraction of corresponding data points, and so on.
One simple formula to calculate this error between a signal a
and a signal b
is the following:
For every i, it calculates a[i] - b[i], squares that difference (this way, negative and positive differences become comparable), and, finally, sums those values up. This is again a situation where one could use a loop, but for fun reasons, we will do it with an STL algorithm. The good thing is that we get data-structure independence for free this way. Our algorithm will work on vectors and on list-like data structures, where no direct indexing is possible.
How to do it...
In this section, we are going to create two signals and calculate their error sum:
- As always, the include statements come first. Then...