现在的很多程序中要想实现查询正态分布函数表,将几百条数据用数组存放起来 再在程序中查询是非常笨拙的方法,现在提供一种实现的算法(Java),可以避免这种笨拙的实现方式:
/**
* 根据分割积分法来求得积分值
* -3.89~3.89区间外的积分面积 小于 0.0001,
* 所以确定有效的积分区间为-3.89~3.89
* 在实现分割的时候精度定为0.0001,得到的结果和查表得到的结果误差在-0.0002~+0.0002之间(已经检验)
*
* @param u 积分上限
* @return 积分值
*/
private static float selfCaculate(float u){
float ret = 0;
if(u < -3.89){
return 0;
}
else if(u > 3.89){
return 1;
}
float temp = -3.89f;
while(temp <= u){
ret += 0.0001f * fx(temp);
temp += 0.0001f;
}
return ret;
}
/**
* 求被积函数的函数值 (1/(2 * PI)^(0.5))e^(-t^2/2)
* @param x 变量x
* @return 函数值
*/
private static float fx(float x){
float ret = 0;
double a = 1.0 / Math.sqrt(Math.PI * 2);
a = a * Math.pow(Math.E, -0.5 * Math.pow(x, 2));
ret = (float) a;
return ret;
}
/************************************/
/************************************/
上面的实现方式精度是定在0.0001的 若想提高精度 只要适当扩大积分的区间(-3.89~3.89
) 以及缩小分割区间(0.0001)就可以了