Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SciProg2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pclark3
SciProg2024
Commits
ddb26a0a
Commit
ddb26a0a
authored
1 year ago
by
cprutean
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
be822f80
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CodeExamples/OneWayConditional.ipynb
+78
-0
78 additions, 0 deletions
CodeExamples/OneWayConditional.ipynb
with
78 additions
and
0 deletions
CodeExamples/OneWayConditional.ipynb
0 → 100644
+
78
−
0
View file @
ddb26a0a
{
"cells": [
{
"cell_type": "markdown",
"id": "781271bc",
"metadata": {},
"source": [
"# $\\Pi$ Example"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5417046e",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Example of estimating Pi by Monte Carlo Simulation.\n",
"\"\"\"\n",
"import math\n",
"import random\n",
"import matplotlib.pyplot as plt\n",
"\n",
"def main():\n",
" maxPoint = int(input(\"Number of points : \"))\n",
" plotInterval = maxPoint/100\n",
" xData = []\n",
" yData = []\n",
"\n",
" point = 0\n",
" inside = 0\n",
" while point < maxPoint:\n",
" point += 1\n",
" x = random.uniform(-1.0,1.0) # Random between -1 -> 1for both x and y\n",
" y = random.uniform(-1.0,1.0)\n",
"\n",
" if x*x + y*y <= 1.0: # Test if inside unit circle\n",
" inside += 1 \n",
"\n",
" if point % plotInterval == 0: # Calcualte estimate for plot\n",
" estimate = 4.0*inside/point\n",
" xData.append(point)\n",
" yData.append(estimate)\n",
"\n",
" print(\"Estimate for PI after \" + str(maxPoint) + \" is \" + str(estimate)) \n",
" plt.plot(xData,yData) # Plot the estimate\n",
" plt.plot([0,maxPoint],[math.pi,math.pi]) # Plot Pi \n",
" plt.ylim(3.0,3.3) # Limit graph range\n",
" plt.title(\"Plot of estimate of Pi again points\")\n",
" plt.show()\n",
"\n",
"\n",
"main()"
]
}
],
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:781271bc tags:
# $\Pi$ Example
%% Cell type:code id:5417046e tags:
```
python
"""
Example of estimating Pi by Monte Carlo Simulation.
"""
import
math
import
random
import
matplotlib.pyplot
as
plt
def
main
():
maxPoint
=
int
(
input
(
"
Number of points :
"
))
plotInterval
=
maxPoint
/
100
xData
=
[]
yData
=
[]
point
=
0
inside
=
0
while
point
<
maxPoint
:
point
+=
1
x
=
random
.
uniform
(
-
1.0
,
1.0
)
# Random between -1 -> 1for both x and y
y
=
random
.
uniform
(
-
1.0
,
1.0
)
if
x
*
x
+
y
*
y
<=
1.0
:
# Test if inside unit circle
inside
+=
1
if
point
%
plotInterval
==
0
:
# Calcualte estimate for plot
estimate
=
4.0
*
inside
/
point
xData
.
append
(
point
)
yData
.
append
(
estimate
)
print
(
"
Estimate for PI after
"
+
str
(
maxPoint
)
+
"
is
"
+
str
(
estimate
))
plt
.
plot
(
xData
,
yData
)
# Plot the estimate
plt
.
plot
([
0
,
maxPoint
],[
math
.
pi
,
math
.
pi
])
# Plot Pi
plt
.
ylim
(
3.0
,
3.3
)
# Limit graph range
plt
.
title
(
"
Plot of estimate of Pi again points
"
)
plt
.
show
()
main
()
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment