monteCarlo java
This commit is contained in:
29
MonteCarlo_Java/.gitignore
vendored
Normal file
29
MonteCarlo_Java/.gitignore
vendored
Normal file
@@ -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
|
||||
8
MonteCarlo_Java/.idea/.gitignore
generated
vendored
Normal file
8
MonteCarlo_Java/.idea/.gitignore
generated
vendored
Normal file
@@ -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
|
||||
6
MonteCarlo_Java/.idea/misc.xml
generated
Normal file
6
MonteCarlo_Java/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
MonteCarlo_Java/.idea/modules.xml
generated
Normal file
8
MonteCarlo_Java/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/MonteCarlo_Java.iml" filepath="$PROJECT_DIR$/MonteCarlo_Java.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
11
MonteCarlo_Java/MonteCarlo_Java.iml
Normal file
11
MonteCarlo_Java/MonteCarlo_Java.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
134
MonteCarlo_Java/src/Main.java
Normal file
134
MonteCarlo_Java/src/Main.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user