added jupyter nb

This commit is contained in:
2024-06-06 16:30:11 +02:00
commit 33029d1389
17 changed files with 1458 additions and 0 deletions

635
Evaluation.ipynb Normal file

File diff suppressed because one or more lines are too long

173
MonteCarlo Function.ipynb Normal file
View File

@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b5867788",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import time\n",
"import numpy as np\n",
"\n",
"from scipy.integrate import quad"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba84224d",
"metadata": {},
"outputs": [],
"source": [
"class Point:\n",
" x: float\n",
" y: float\n",
"\n",
" def __init__(self, point: (float, float)):\n",
" self.x, self.y = point\n",
"\n",
"\n",
"class LinearBounds:\n",
" lower: float\n",
" higher: float\n",
"\n",
" def __init__(self, lower: float, higher: float):\n",
" self.lower = lower\n",
" self.higher = higher\n",
"\n",
" def length(self) -> float:\n",
" return self.higher - self.lower\n",
"\n",
" def get_random_value(self) -> float:\n",
" return random.uniform(self.lower, self.higher)\n",
"\n",
"\n",
"class Bounds:\n",
" x: LinearBounds\n",
" y: LinearBounds\n",
"\n",
" def __init__(self, x: LinearBounds, y: LinearBounds):\n",
" self.x = x\n",
" self.y = y\n",
"\n",
" def area(self) -> float:\n",
" return self.x.length() * self.y.length()\n",
"\n",
" def get_random_point(self) -> Point:\n",
" return Point((self.x.get_random_value(), self.y.get_random_value()))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2120dbad",
"metadata": {},
"outputs": [],
"source": [
"def getRealIntegral(f, bounds: LinearBounds) -> float:\n",
" return quad(f, bounds.lower, bounds.higher)[0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bdc2240a",
"metadata": {},
"outputs": [],
"source": [
"def getIsInside(f, p: Point) -> bool:\n",
" y_0: float = f(p.x)\n",
" if abs(y_0) > abs(p.y) and y_0*p.y >= 0:\n",
" return True\n",
"\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d3f58a1f",
"metadata": {},
"outputs": [],
"source": [
"def function(x: float) -> float:\n",
" return x**2-3\n",
"\n",
"bounds = Bounds(LinearBounds(-1, 4), LinearBounds(-4, 15))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "93672263",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The approximated Integral of the function is: 6.67280\n",
"The real Integral of the function is: 6.66667\n",
"That's an error of 0.00613 or 0.09200% \n",
"And the whole thing took 1.37645 Seconds for 1000000 samples\n"
]
}
],
"source": [
"startTime = time.time()\n",
"pointsInside: int = 0\n",
"samples: int = 10**6\n",
"\n",
"for i in range(samples):\n",
" toTestPoint = bounds.get_random_point()\n",
" if getIsInside(function, toTestPoint):\n",
" if toTestPoint.y >= 0:\n",
" pointsInside += 1\n",
" else:\n",
" pointsInside -= 1\n",
"\n",
"integral = (pointsInside / samples) * bounds.area()\n",
"print(f\"The approximated Integral of the function is: {integral:.5f}\")\n",
"\n",
"real_value: float = getRealIntegral(function, bounds.x)\n",
"print(f\"The real Integral of the function is: {real_value:.5f}\")\n",
"\n",
"error: float = abs(real_value-integral)\n",
"print(f\"That's an error of {error:.5f} or {(error/real_value)*100:.5f}% \")\n",
"\n",
"print(f\"And the whole thing took {time.time()-startTime:.5f} Seconds for {samples} samples\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce2f249e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

1
out/output01.json Normal file

File diff suppressed because one or more lines are too long

1
out/output02.json Normal file

File diff suppressed because one or more lines are too long

1
out/output03.json Normal file

File diff suppressed because one or more lines are too long

1
out/output04.json Normal file

File diff suppressed because one or more lines are too long

1
out/output05.json Normal file

File diff suppressed because one or more lines are too long

1
out/output06.json Normal file

File diff suppressed because one or more lines are too long

1
out/output07.json Normal file

File diff suppressed because one or more lines are too long

1
out/output08.json Normal file

File diff suppressed because one or more lines are too long

1
out/output09.json Normal file

File diff suppressed because one or more lines are too long

1
out/output10.json Normal file

File diff suppressed because one or more lines are too long

1
out/output11.json Normal file
View File

@@ -0,0 +1 @@
{"x":[10000000000,20000000000,50000000000],"y":[[1.1070528943500009,1.107091172850001,1.107024459450001,1.107134587650001,1.107118888850001,1.1070673763500007,1.107102086350001,1.107087578350001,1.1070208383000009,1.1070149558000009],[1.107116142600001,1.107052413675001,1.1070808953750009,1.107057258775001,1.1070388527250008,1.1071355902750009,1.1070987538000008,1.1070910386250008,1.107089875775001,1.107066415325001],[1.107083811470001,1.107081873690001,1.107115097660001,1.1070899834800008,1.107081929590001,1.1070892114100008,1.107080606320001,1.1070829237000008,1.107084684160001,1.1070922865600008]]}

1
out/output12.json Normal file
View File

@@ -0,0 +1 @@
{"x":[100000000000,200000000000,500000000000],"y":[[1.107080884000001],[1.107080584805001],[1.107089721517001]]}