commit dc4583a6ce331356ba9f7d103b23f7a91083f399 Author: Parric007 Date: Wed Apr 17 11:48:55 2024 +0200 monteCarlo java diff --git a/.idea/MonteCarlo_Java.iml b/.idea/MonteCarlo_Java.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/MonteCarlo_Java.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..63d15a8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..9feb01a --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + 1713175398866 + + + + + + \ No newline at end of file diff --git a/MonteCarlo_Java/.gitignore b/MonteCarlo_Java/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/MonteCarlo_Java/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/MonteCarlo_Java/.idea/.gitignore b/MonteCarlo_Java/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/MonteCarlo_Java/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/MonteCarlo_Java/.idea/misc.xml b/MonteCarlo_Java/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/MonteCarlo_Java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MonteCarlo_Java/.idea/modules.xml b/MonteCarlo_Java/.idea/modules.xml new file mode 100644 index 0000000..5b681dd --- /dev/null +++ b/MonteCarlo_Java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/MonteCarlo_Java/MonteCarlo_Java.iml b/MonteCarlo_Java/MonteCarlo_Java.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/MonteCarlo_Java/MonteCarlo_Java.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/MonteCarlo_Java/src/Main.java b/MonteCarlo_Java/src/Main.java new file mode 100644 index 0000000..e6a6552 --- /dev/null +++ b/MonteCarlo_Java/src/Main.java @@ -0,0 +1,134 @@ +public class Main { + + static Bounds bounds = new Bounds(new LinearBounds(0, 20), new LinearBounds(-100, 150000)); + static double samples = 1_000_000; + static int threadCount = 16; + + static class Point { + private final double x; + private final double y; + + public Point(double _x, double _y) { + this.x = _x; + this.y = _y; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } + } + + static class LinearBounds { + private final float lower; + private final float higher; + + public LinearBounds(float _lower, float _higher) { + this.lower = _lower; + this.higher = _higher; + } + public double length() { + return Math.abs(higher-lower); + } + public double getRandomValue() { + return lower + (randomFloat() * this.length()); + } + public float getLower() { + return lower; + } + + public float getHigher() { + return higher; + } + } + + static class Bounds { + private final LinearBounds x; + private final LinearBounds y; + + public Bounds(LinearBounds _x, LinearBounds _y) { + this.x = _x; + this.y = _y; + } + public double area() { + return x.length() * y.length(); + } + public Point getRandomPoint() { + return new Point(x.getRandomValue(), y.getRandomValue()); + } + + public LinearBounds getX() { + return x; + } + } + + static double function(double x) { + return Math.pow(x,4)-4*Math.pow(x,3)+18*Math.pow(x,2)-12*x-69; + } + static double indefiniteIntegral(double x) { + return (1.0/5) * Math.pow(x, 5) - Math.pow(x, 4) + 6*Math.pow(x, 3) - 6 * Math.pow(x, 2) -69*x; + } + static boolean getIsInside(Point p) { + double y_0 = function(p.getX()); + return Math.abs(y_0) > Math.abs(p.getY()) && y_0 * p.getY() >= 0; + } + static double getRealIntegral(LinearBounds bounds) { + return indefiniteIntegral(bounds.getHigher()) - indefiniteIntegral(bounds.getLower()); + } + static double randomFloat() { + return Math.random(); + } + + + + public static void main(String[] args) throws InterruptedException { + + double pointsInside = 0; + long startTime = System.nanoTime(); + double real_value = getRealIntegral(bounds.getX()); + + + for (int i = 0; i< threadCount; i++) { + + Threading t = new Threading(); + t.start();t.join(); + pointsInside += t.getPoints(); + + } + + double integral = (pointsInside/(samples*threadCount)) * bounds.area(); + double error = Math.abs(real_value-integral); + + System.out.println("The approximated Integral of the function is: " + String.format("%.2f", integral)); + System.out.println("The real Integral of the function is: " + String.format("%.2f",real_value)); + System.out.println("That's an error of " + String.format("%.8f", error) +" or " + String.format("%.8f",(error/real_value)*100) + "%"); + System.out.println("And the whole thing took " + String.format("%.6f", (System.nanoTime()-startTime)/1000000000.0) + " Seconds for " + String.format("%,.0f", samples*threadCount) + " samples"); + + } + static class Threading extends Thread { + private double pointsInsideThread = 0; + + + @Override + public void run() { + + //System.out.println("Thread " + Thread.currentThread()); + Point toTestPoint; + for (int i = 0; i < samples; i++) { + toTestPoint = bounds.getRandomPoint(); + if (getIsInside(toTestPoint)) { + pointsInsideThread++; + } + } + + } + + public double getPoints() { + return pointsInsideThread; + } + } +} +