From 419cee3d721be13c3fbdc5829587b151146a3938 Mon Sep 17 00:00:00 2001 From: Parric007 Date: Thu, 11 Apr 2024 16:14:46 +0200 Subject: [PATCH] init commit --- .gitignore | 3 ++ CMakeLists.txt | 6 +++ main.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cd7964 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +/.idea/ +/cmake-build-debug/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8f8435f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.28) +project(MonteCarlo_Cpp) + +set(CMAKE_CXX_STANDARD 26) + +add_executable(MonteCarlo_Cpp main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..87bb1b8 --- /dev/null +++ b/main.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + +double randomFloat() +{ + return (double)(rand()) / (double)(RAND_MAX); +} + +class Point { +private: + double x{}; + double y{}; + +public: + Point(double x, double y): x(x), y(y) {} + + [[nodiscard]] double get_x() const { + return this->x; + } + + [[nodiscard]] double get_y() const { + return this->y; + } + + Point() {}; +}; + +class LinearBounds { +private: + float lower; + float higher; + +public: + LinearBounds(float lower, float higher): lower(lower), higher(higher) {} + [[nodiscard]] double length() const { + return std::abs(higher - lower); + } + + [[nodiscard]] double getRandomValue() const { + return this->lower + (randomFloat() * this->length()); + } + [[nodiscard]] float get_lower() const { + return lower; + } + [[nodiscard]] float get_upper() const { + return higher; + } +}; + + +class Bounds { +protected: + LinearBounds x; + LinearBounds y; +public: + Bounds(LinearBounds x, LinearBounds y): x(x), y(y) {} + double area() { + return x.length() * y.length(); + } + Point getRandomPoint() { + return {x.getRandomValue(), y.getRandomValue()}; + } + LinearBounds get_x() { + return x; + } + +}; + + +double function(double x) { + return pow(x,4)-4*pow(x,3)+18*pow(x,2)-12*x-69; +} +double indefiniteIntegral(double x) { + return (1.0/5) * pow(x, 5) - pow(x, 4) + 6*pow(x, 3) - 6 * pow(x, 2) -69*x; +} + +bool getIsInside(Point p) { + double y_0 = function(p.get_x()); + return std::abs(y_0) > std::abs(p.get_y()) && y_0* p.get_y() >= 0; +} + +double getRealIntegral(LinearBounds bounds) { + return indefiniteIntegral(bounds.get_upper())- indefiniteIntegral(bounds.get_lower()); +} + +int main() { + auto startTime = std::chrono::high_resolution_clock::now(); + double pointsInside = 0; + double samples = 10000000; + Bounds bounds = Bounds(LinearBounds(0, 20), LinearBounds(-100, 150000)); + Point toTestPoint; + for (int i = 0; i< samples; i++) { + toTestPoint = bounds.getRandomPoint(); + if (getIsInside(toTestPoint)) { + pointsInside++; + } + } + + std::cout << std::fixed; + std::cout.precision(5); + double integral = (pointsInside / samples) * bounds.area(); + std::cout << "The approximated Integral of the function is: " << integral << "\n"; + + double real_value = getRealIntegral(bounds.get_x()); + std::cout << "The real Integral of the function is: " << real_value << "\n"; + + double error = std::abs(real_value-integral); + std::cout << "That's an error of " << error <<" or " << (error/real_value)*100 << "%\n"; + std::chrono::duration duration = std::chrono::duration_cast>(std::chrono::high_resolution_clock::now()-startTime); + std::cout << "And the whole thing took " << duration.count() << " Seconds for "; + std::cout.precision(0); + std::cout << samples << " samples"; + return 0; +} +