【问题标题】:Monte Carlo approximation of the integral of y=2^(-0.5)x from 0 to 1 boolean isn't working properly从 0 到 1 布尔值的 y=2^(-0.5)x 积分的蒙特卡洛近似不能正常工作
【发布时间】:2017-02-27 21:40:39
【问题描述】:

Professor's equation for the boolean condition that satisfies "hitPointsCounter" in my code

在一个 Java 类中,我的任务是解决几何面积的几个蒙特卡罗近似。我的第一个实验是,我有一个名为“条件”的布尔值运行一个 do-while 循环,当(错误

当我将“while”语句从布尔值更改为计数器时,我可以让它正常运行,所以我知道布尔值本身存在问题。

如果我被要求求解高度为 2^(-0.5) 从 0 到 1 的正方形面积,但不使用方程 y=,教授给我的增加 hitPointsCounter 变量的条件会很好2^(-0.5)x - 所以我即兴创作并注意到对于在线上或在线下的点,它必须满足 x/y >= 2^0.5。

谁能给我一些反馈,说明我可能在哪里搞砸了?我的问题在 switch case 1: 和 break 之间。

    public static void main(String[] args){




    double tolerance = 1E-9, referenceArea =0.0, exactArea =0.0, x =0.0,     y =0.0, error = 0.0, approximateArea = 0.0;
    int  totalPointsCounter =0, hitPointsCounter =0;
    char firstCharacter ;
    String titleText, messageText;

    titleText = "The Monte Carlo Method";

    int userInput = JOptionPane.showConfirmDialog(null, "Run a Monte Carlo Experiment?", titleText, JOptionPane.YES_NO_OPTION);
    if (userInput == 1){
        JOptionPane.showMessageDialog(null, "The program terminates \nGood Bye!", titleText,  JOptionPane.WARNING_MESSAGE);
    System.exit(0);

    }


    String userExperimentInput = JOptionPane.showInputDialog(null, "Please Enter\n1 for Experiment 1\n2 for Experiment 2\n3 for Experiment 3\n4 for Experiment 4", titleText,  JOptionPane.QUESTION_MESSAGE).trim();

      if(userExperimentInput == null || userExperimentInput.equals("") ){
        JOptionPane.showMessageDialog(null, "No input received\nThe program terminates", titleText,  JOptionPane.WARNING_MESSAGE);
        System.exit(0);     
      }
char theCase = userExperimentInput.charAt(0);



switch (theCase){
    case '1': 
        exactArea = Math.pow(2, -0.5)*1/2; 
        referenceArea = 1.0;


         boolean condition = error < tolerance;

         double percentHit = (((double)hitPointsCounter/(double)totalPointsCounter));
   approximateArea =(percentHit)*(referenceArea);
error = exactArea - Math.abs(approximateArea);

int counter = 0;

do {
        x= Math.random();
        y= Math.random();
        totalPointsCounter++;

        if (y==0 && x==0){hitPointsCounter++;}
        if (y>0){
        if ((x/y)>=Math.pow(2, 0.5)){

                hitPointsCounter++;}
        }

        exactArea = Math.pow(2, -0.5)*1/2; 
        referenceArea = 1.0;




        counter++;

        }

while(condition);

  percentHit = (((double)hitPointsCounter/(double)totalPointsCounter));
   approximateArea =(percentHit)*(referenceArea);
error = exactArea - Math.abs(approximateArea);



messageText ="Experiment #1" + ": \n\nMC needed " + totalPointsCounter + " random points for tolerance " + tolerance + "\nThe approximate area is " + approximateArea;

JOptionPane.showMessageDialog(null, messageText, titleText, JOptionPane.INFORMATION_MESSAGE);

break;



    case 2: 
        exactArea = Math.PI;
        referenceArea = 4.0;







    case 3:
        exactArea = (1.0/3.0);
        referenceArea = 2.0;







    case 4:
        exactArea = 2;
        referenceArea = Math.PI;





    default: 
        System.out.println("Wrong character for case number, program terminates");
        System.exit(0);
}

}

【问题讨论】:

  • 我不是 Java 程序员,但我敢猜测循环是无限的,因为循环中没有更新 condition 变量。还要记住,使用do ... while 循环意味着代码块将至少执行一次,无论条件如何 - 确保这是您想要的。
  • 你说得对,这是我问题的很大一部分。我发现了另一个问题,即使我解决了这个问题:我将 hitPointsCounter 定义为 (x/y) >= sqrt(2) 的方法不允许我进入我的错误容忍度范围内。我和我的教授谈过,他只是在图片上的布尔方程中留下了一个“x”。所以它是 y

标签: java math boolean do-while montecarlo


【解决方案1】:

所以我让代码工作,问题是双重的 - 我的代码没有重新评估循环内的布尔“条件”,导致无限循环,而且我之前定义 hitPointCounter 的方式不正确。谢谢达蒙的帮助。当我添加这个块时:

           percentHit =   (((double)hitPointsCounter/(double)totalPointsCounter));
           approximateArea =(percentHit)*(referenceArea);
           error = Math.abs(exactArea - approximateArea);
           condition = error > tolerance;

在我的循环中并改变了

     if (y==0 && x==0){hitPointsCounter++;}
     if (y>0){
     if ((x/y)>=Math.pow(2, 0.5)){

            hitPointsCounter++;}
    }

到:

if (y<(Math.pow(2,-0.5)*x)){

                hitPointsCounter++;}

代码就像一个魅力。

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 2017-10-28
    • 2018-10-20
    • 1970-01-01
    • 2020-07-05
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多