0
$\begingroup$

I am making a Java function to calculate $\int_{0}^{\pi/2}x/sin(x) dx$ using Simpsons rule. When the function equals $0$ or $0/\sin(0)$ should I just add $1$ (limit) or $10^{-10}$ (limit). My professor said to use $10^{-10}$ why would this be?

public static double simpsonsRuleFunction1(double valueN, double valueA, double valueB, double valueDx) {

    double e = 0.0;
    double simpsonsRule = 0.0;
    double valueHolder = 0.0;

    valueN = 2;
    valueA = 0;
    valueB = (Math.PI)/2; 

    for(int i = 1; i<=valueN+1 ; i++){
        valueDx = (valueB-valueA)/valueN;
        e = valueA + ((i-1)*valueDx);

        if (i==1) {
        // Limit as x -> 0
        simpsonsRule += Math.pow(10,-10);

        }
        else if ((i % 2 == 0) && ( i > 1) && (i < valueN+1 ))  {
        simpsonsRule += 4*(e/((Math.sin(e))));
        }
        else if ((i % 2 != 0) && ( i > 1) && (i < valueN+1 )) {
        simpsonsRule += 2*(e/((Math.sin(e))));   
        }
        else if (i == valueN+1 ) {
            simpsonsRule += (e/((Math.sin(e))));

        }

    }
    simpsonsRule = simpsonsRule *((valueDx)/3);

    System.out.println("\nsimpsonsRule" + simpsonsRule);

     while(Math.abs(valueHolder - simpsonsRule) > Math.pow(10,-6)) {
           System.out.println("\nValueHolder" + valueHolder);
           valueHolder = simpsonsRule;
           valueN +=2;
           valueDx = (valueB-valueA)/valueN;
           simpsonsRule = 0;
    for(int i = 1; i<=valueN + 1; i++){
        e = valueA + ((i-1)*valueDx);

        if (i==1) {
        // Limit as x -> 0
        simpsonsRule += Math.pow(10,-10);

        }
        else if (i % 2 == 0) {
        simpsonsRule += 4*(e/((Math.sin(e))));
        }
        else if ((i % 2 != 0) && ( i > 1) && (i < valueN + 1)) {
        simpsonsRule += 2*(e/((Math.sin(e))));   
        }
        else if (i == valueN + 1) {
            simpsonsRule += (e/((Math.sin(e))));

        }

    }
    simpsonsRule = simpsonsRule *((valueDx)/3);


    }
    return valueN;
}
$\endgroup$
  • 3
    $\begingroup$ Perhaps your professor meant to use the value obtained by substituting $x=10^{-10}$ ? $\endgroup$ – Paul Nov 3 '15 at 15:53
1
$\begingroup$

You can simplify your code by removing redundant checks in if statements. You can also pull out the valueDx which is constant in the loop:

double e = 0.0;
double simpsonsRule = 0.0;
double valueHolder = 0.0;

valueN = 2;
valueA = 0;
valueB = (Math.PI)/2; 

valueDx = (valueB-valueA)/valueN;
simpsonsRule = 1.0 + Math.PI/2;
for(int i = 2; i<=valueN ; i++){
    e = valueA + ((i-1)*valueDx);

    if (i % 2 == 0)  {  //even
        simpsonsRule += 4*(e/((Math.sin(e))));
    }
    else { //odd
        simpsonsRule += 2*(e/((Math.sin(e))));   
    }
}

simpsonsRule = simpsonsRule *((valueDx)/3);

Also it looks like you repeat this same loop twice in your code for some reason. I havn't run this so there may still be a few bugs especially in the indices etc but this is homework so I wont do it all for you =). You can look at a python or c variant which does what you want on Wikipedia

The python implementation is particularly close to yours. To answer your question though, you are evaluating between 0 and $\pi/2$. Thus the line:

simpsonsRule = 1.0 + (Math.PI)/2

corresponds to adding $f(0) + f(\pi/2)$ where $f(x)=\frac{x}{\sin(x)}$. In the limit $f(0)->1$ while $f(\pi/2)=\pi/2$.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Oh so I would use the limit of f(0) -> 1 When I == 1 instead of 10^-10? $\endgroup$ – Julius A. Nov 3 '15 at 3:13
  • $\begingroup$ Yes because f(0)->1. Is it possible your professor misunderstood you when you showed him the code? Also don't forget to go over the indicies since I have not run the above code to see if it works. $\endgroup$ – James Nov 3 '15 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.