Friday, April 6, 2012

sumpin sumpin....

I got drunk tonight, wrote a Monte Carlo integration program in Java for shits, and now I'm watching The Marx Brothers "A Night at the Opera" which is pretty funny...

The error estimate is way off.  ...  I'm drunk, so fuck it.This is a crappy way to do integrals, but it is cheap and quick (relatively speaking).  During the Manhattan Project (back when computers where people rather than machines... and women, too, even though in today's society women hate math and prefer barbie dolls thanks for shitty advertizements and gender roles based on capitalist shit), this was how you did numerical integration.


import java.util.Arrays;
import java.util.Random;
/**
 *
 * @author discordantmind
 */
public class MonteCarloIntegration {
    public static void main(String[] args) {
        /* Create program to calculate Monte Carlo Integration
         * The integrand is a separate method called 'integrand'
         */
        // create random points for integration, and sort from highest to lowest
        int numberPoints = 20; // how many points along curve should be evaluated
        int endPoint = 2;
        double[] xposition = new double[numberPoints];
        Random xGen = new Random();
        for(int i=1 ; i < (numberPoints - 1); i++) {
            xposition[i] = endPoint * xGen.nextDouble();
         //   System.out.println(xposition[i]);
        } 
        xposition[0] = 0.;
        xposition[numberPoints - 1] = 2.;


        Arrays.sort(xposition); 
        
        // check to see if points are sort-of evenly spread out
        for (double dd : xposition){
            System.out.println(dd);
        }
        
        //Create new random number generators for x and y coordinates 
        Random xtestpoint = new Random();
        Random ytestpoint = new Random();
        
        // starting stuff for Monte Carlo shit
        double testx;
        double testy;
        double interval;
        int below;
        double testintegral;
        double area = 0; // initialize integral
        int numberRandom = 100; // number of random points for MC
        double error = 0; // initialize error counter thingy
        double rho; // density of points below curve
        /* BEGIN MONTE CARLO SHIT */
        for(int i=0; i < (xposition.length - 1); i++) {
            // Taking care of end points
            interval = xposition[i+1] - xposition[i];
            below = 0;
            for(int j=0; j<numberRandom; j++) {
                testx = xposition[i] + interval * xtestpoint.nextDouble();
                testy = 2.0 * ytestpoint.nextDouble();
                testintegral = integrand(testx);
                if(testy <= testintegral) {
                    below++;
                }
            }
            rho = below/ (double) (numberRandom);
            area += interval * 2.0 * rho;
            error += interval * 2.0 * Math.sqrt((rho - Math.pow(rho,2))/numberRandom);
        }
        System.out.println("Error estimate is " + error);
        System.out.println("Integral from 0 to "+ endPoint + " is: " + area);


    }
    public static double integrand(double x) {
        // plug in whatever function you want to integrate here as 'func'
        double func = Math.sqrt(4. - Math.pow(x,2)); 
        return func;
    }
}
I can't really drink hard liquor anymore.  I'm still taking aspirin therapy, which means I can't drink to heavily or I could hemorrhage into my brain.  But, the doctor said I can stop taking Plavix, which is nice...

Enhanced by Zemanta

No comments:

Post a Comment