diff options
Diffstat (limited to 'src/misc.c')
| -rw-r--r-- | src/misc.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -218,6 +218,26 @@ static struct { {100,363.73937555556347}, }; +double trapz(const double *y, double dx, size_t n) +{ + /* Returns the integral of `y` using the trapezoidal rule assuming a + * constant grid spacing `dx`. + * + * See https://en.wikipedia.org/wiki/Trapezoidal_rule. */ + size_t i; + double sum = 0.0; + + if (n < 2) return 0.0; + + sum = y[0]; + for (i = 1; i < n-1; i++) { + sum += 2*y[i]; + } + sum += y[n-1]; + + return sum*dx/2.0; +} + void get_path_length(double *pos1, double *pos2, double R, double *l1, double *l2) { /* Returns the path length inside and outside a circle of radius `R` for a |
