SDDSlib
Loading...
Searching...
No Matches
trapInteg.c File Reference

Provides functions for performing trapezoidal integration on data sets. More...

#include "mdb.h"

Go to the source code of this file.

Functions

long trapazoidIntegration (double *x, double *y, long n, double *integral)
 Computes the integral of a dataset using the trapezoidal rule.
 
long trapazoidIntegration1 (double *x, double *y, long n, double *integral)
 Computes the integral as a function of x using the trapezoidal rule.
 

Detailed Description

Provides functions for performing trapezoidal integration on data sets.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, R. Soliday

Definition in file trapInteg.c.

Function Documentation

◆ trapazoidIntegration()

long trapazoidIntegration ( double * x,
double * y,
long n,
double * integral )

Computes the integral of a dataset using the trapezoidal rule.

This function calculates the integral of the given data points using the trapezoidal rule.

Parameters
xPointer to the array of x-values.
yPointer to the array of y-values.
nThe number of data points.
integralPointer to store the computed integral.
Returns
Returns 1 on success, 0 on failure.

Definition at line 29 of file trapInteg.c.

29 {
30 double sum;
31 long i;
32
33 if (!x || !y || !integral || n <= 1)
34 return 0;
35 sum = y[n - 1] * x[n - 1] - y[0] * x[0];
36 for (i = 0; i < n - 1; i++)
37 sum += y[i] * x[i + 1] - y[i + 1] * x[i];
38 *integral = sum / 2;
39 return 1;
40}

◆ trapazoidIntegration1()

long trapazoidIntegration1 ( double * x,
double * y,
long n,
double * integral )

Computes the integral as a function of x using the trapezoidal rule.

This function calculates the integral at each x-value using the trapezoidal rule.

Parameters
xPointer to the array of x-values.
yPointer to the array of y-values.
nThe number of data points.
integralPointer to store the computed integral values.
Returns
Returns 1 on success, 0 on failure.

Definition at line 53 of file trapInteg.c.

53 {
54 long i;
55
56 if (!x || !y || !integral || n <= 1)
57 return 0;
58 integral[0] = 0;
59 for (i = 1; i < n; i++)
60 integral[i] = integral[i - 1] + (y[i] + y[i - 1]) * (x[i] - x[i - 1]) / 2;
61 return 1;
62}