英文:
Gaussian distribution function different answer for program and math
问题
以下是您要翻译的内容:
我写了一段代码来实现高斯分布函数。
代码块
import java.lang.Math.*;
public class gauss {
public static void main(String args[]){
gauss c = new gauss();
c.gu1(5.0,3.0,2.0);
}
public static double gu1(double mu,double sigm2,double x)
{
double a;
a=1/(Math.sqrt(2*Math.PI))*sigm2;
double b;
b= Math.exp(-0.5)*(Math.pow(((x-mu)/sigm2)),2);
double z= a*b;
System.out.println(z);
return(z);
}
}
输入:x = 2,μ = 5 和 σ = 3
输出:0.7259121735574301
然而,如果我使用纸和笔进行数学求解,得到的答案是0.0805。
我没有找到为什么编程答案和手动计算答案会有这样的差异?
高斯分布公式
英文:
I write down a code to implement Gaussian distribution function.
The code block
import java.lang.Math.*;
public class gauss {
public static void main(String args[]){
gauss c = new gauss();
c.gu1(5.0,3.0,2.0);
}
public static double gu1(double mu,double sigm2,double x)
{
double a;
a=1/(Math.sqrt(2*Math.PI))*sigm2;
double b;
b= Math.exp(-0.5)*(Math.pow(((x-mu)/sigm2)),2);
double z= a*b;
System.out.println(z);
return(z);
}
}
Input: x = 2, μ = 5 and σ = 3
Output: 0.7259121735574301
However, if I solve it mathematically using pen and paper I got 0.0805 as answer.
I did not find out why there is such a difference with programming answer and manual answer?
Gaussian distribution formula
答案1
得分: 1
a
和 b
的表达式是错误的。应为
a = 1 / (Math.sqrt(2 * Math.PI)) / sigm2;
b = Math.exp(-0.5 * Math.pow((x - mu) / sigm2, 2));
英文:
The expressions for a
and b
are wrong. Should be
a=1/(Math.sqrt(2*Math.PI))/sigm2;
b= Math.exp(-0.5*Math.pow((x-mu)/sigm2,2));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论