蒙特卡罗积分的近似y = 2 ^(-0.5)x从0到1布尔不正常工作

时间:2022-09-06 16:32:04

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

在我的代码中满足“hitPointsCounter”的布尔条件的教授方程

In a Java class I'm tasked with solving several Monte Carlo approximations for geometric area. My first experiment with this I have a boolean called "conditional" running a do-while loop that is satisfied when (error < tolerance) - as of now when I run my program this seems to be yielding an infinite loop.

在Java类中,我的任务是解决几何区域的若干蒙特卡罗逼近。在我的第一个实验中,我有一个名为“条件”的布尔值,它运行一个while循环,当(error < tolerance))——到现在为止,当我运行我的程序时,它似乎产生了一个无限循环。

When I change my "while" statement from the boolean to a counter I can get it to run properly, so I know there's an issue with the boolean itself.

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

The condition that my professor gave me for incrementing my hitPointsCounter variable would work well if I was asked to solve for the area of the square with height 2^(-0.5) from 0 to 1, but not with the equation y=2^(-0.5)x - so I improvised and noticed that for a point to be on or under the line it must be satisfied by x/y >= 2^0.5.

递增的条件是我的教授给我hitPointsCounter变量如果我被要求解出广场的面积随高度2 ^(-0.5)从0到1,但不与方程y = 2 ^(-0.5)x -所以我临时,注意到一个点或线必须满足下x / y > = 2 ^ 0.5。

Could anyone give me some feedback for where I may have screwed up? My issue is somewhere between the switch case 1: and break.

有没有人能给我一些关于我可能把事情搞砸的反馈?我的问题是在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);
}

}

1 个解决方案

#1


0  

So i got the code to work, the issue was twofold - my code wasn't reevaluating the boolean 'condition' within the loop causing an infinite loop and the way I defined hitPointCounter previously wasn't correct. Thank you Damon for your help. When I added this block:

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

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

within my loop and changed

在我的循环中改变

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

            hitPointsCounter++;}
    }

to:

:

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

                hitPointsCounter++;}

The code worked like a charm.

代码运行得很好。

#1


0  

So i got the code to work, the issue was twofold - my code wasn't reevaluating the boolean 'condition' within the loop causing an infinite loop and the way I defined hitPointCounter previously wasn't correct. Thank you Damon for your help. When I added this block:

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

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

within my loop and changed

在我的循环中改变

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

            hitPointsCounter++;}
    }

to:

:

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

                hitPointsCounter++;}

The code worked like a charm.

代码运行得很好。