SDDSlib
Loading...
Searching...
No Matches
trapInteg.c
Go to the documentation of this file.
1/**
2 * @file trapInteg.c
3 * @brief Provides functions for performing trapezoidal integration on data sets.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, R. Soliday
14 */
15
16#include "mdb.h"
17
18/**
19 * @brief Computes the integral of a dataset using the trapezoidal rule.
20 *
21 * This function calculates the integral of the given data points using the trapezoidal rule.
22 *
23 * @param x Pointer to the array of x-values.
24 * @param y Pointer to the array of y-values.
25 * @param n The number of data points.
26 * @param integral Pointer to store the computed integral.
27 * @return Returns 1 on success, 0 on failure.
28 */
29long trapazoidIntegration(double *x, double *y, long n, double *integral) {
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}
41
42/**
43 * @brief Computes the integral as a function of x using the trapezoidal rule.
44 *
45 * This function calculates the integral at each x-value using the trapezoidal rule.
46 *
47 * @param x Pointer to the array of x-values.
48 * @param y Pointer to the array of y-values.
49 * @param n The number of data points.
50 * @param integral Pointer to store the computed integral values.
51 * @return Returns 1 on success, 0 on failure.
52 */
53long trapazoidIntegration1(double *x, double *y, long n, double *integral) {
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}
long trapazoidIntegration1(double *x, double *y, long n, double *integral)
Computes the integral as a function of x using the trapezoidal rule.
Definition trapInteg.c:53
long trapazoidIntegration(double *x, double *y, long n, double *integral)
Computes the integral of a dataset using the trapezoidal rule.
Definition trapInteg.c:29