erf() Function
The erf()
function computes the error function value for a given input, a mathematical function integral to probability theory, statistics, and partial differential equations. It is widely used in various scientific computations to evaluate integrals related to the Gaussian distribution.
Syntax of erf()
double erf(double x);
Parameters
Parameter | Description |
---|---|
x | Input value for which the error function is evaluated. |
Return Value
The function returns the error function value corresponding to the input.
It is important to note that the error function is related to the cumulative distribution function of the normal distribution, and its values range between -1 and 1. Additionally, the function exhibits odd symmetry, meaning that erf(-x) = -erf(x)
, which is useful in many analytical applications.
Examples for erf()
Example 1: Evaluating the Error Function for a Positive Input
This example demonstrates how to use erf()
to compute the error function for a positive input value.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = 1.0;
double result = erf(x);
printf("erf(%f) = %f\n", x, result);
return 0;
}
Explanation:
- The input value
x
is set to 1.0. - The
erf()
function is called to compute the error function for this value. - The result is printed to display the error function value for the positive input.
Program Output:
erf(1.000000) = 0.842701
Example 2: Evaluating the Error Function for a Negative Input
This example demonstrates the computation of the error function for a negative input, illustrating the odd symmetry property of the function.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = -1.0;
double result = erf(x);
printf("erf(%f) = %f\n", x, result);
return 0;
}
Explanation:
- The input value
x
is set to -1.0. - The
erf()
function is called to compute the error function for the negative input. - The output is printed, demonstrating that the result is the negative of the error function for the corresponding positive value.
Program Output:
erf(-1.000000) = -0.842701
Example 3: Comparing Error Function Values for a Range of Inputs
This example computes and compares error function values for a series of inputs, allowing observation of the function’s behavior across different values.
Program
#include <stdio.h>
#include <math.h>
int main() {
double values[] = {0.0, 0.5, 1.0, 1.5, 2.0};
int n = sizeof(values) / sizeof(values[0]);
for (int i = 0; i < n; i++) {
printf("erf(%f) = %f\n", values[i], erf(values[i]));
}
return 0;
}
Explanation:
- An array of values is defined to represent different inputs.
- A loop iterates over the array and computes the error function for each value using
erf()
. - The computed values are printed, which illustrates how the error function behaves as the input increases.
Program Output:
erf(0.000000) = 0.000000
erf(0.500000) = 0.520500
erf(1.000000) = 0.842701
erf(1.500000) = 0.966105
erf(2.000000) = 0.995322