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
69c4a980
Commit
69c4a980
authored
1 year ago
by
cprutean
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
376e9177
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CodeExamples/SphereVolume.ipynb
+96
-0
96 additions, 0 deletions
CodeExamples/SphereVolume.ipynb
with
96 additions
and
0 deletions
CodeExamples/SphereVolume.ipynb
0 → 100644
+
96
−
0
View file @
69c4a980
{
"cells": [
{
"cell_type": "markdown",
"id": "227f88f0",
"metadata": {},
"source": [
"# Sphere Volume"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "695f4dd5",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
" Program to estimate the volume of a sphere by counting the\n",
" number of random points that fall inside compared with the\n",
" number of points in a box round the sphere.\n",
"\"\"\"\n",
"import math\n",
"import random\n",
"\n",
"\n",
"def random_point(max_range):\n",
" \"\"\"\n",
" Return a random 3d point in range +/max_range\n",
" \"\"\"\n",
" x = random.uniform(-max_range,max_range)\n",
" y = random.uniform(-max_range,max_range)\n",
" z = random.uniform(-max_range,max_range)\n",
"\n",
" return x,y,z\n",
"\n",
"def point_in_sphere(radius,x,y,z):\n",
" \"\"\"\n",
" Check if a point is within a sphere specified.\n",
" \"\"\"\n",
" if x*x + y*y + z*z <= radius*radius:\n",
" return True\n",
" else :\n",
" return False\n",
"\n",
"def main():\n",
" \"\"\"\n",
" Main program\n",
" \"\"\"\n",
" \n",
" points = int(input(\"Number of points : \")) # Ask for initial conditions\n",
" radius = float(input(\"Radius of sphere : \"))\n",
" \n",
" # Counters for number inside and look variable\n",
" inside, p = 0 , 0\n",
" while p <= points: # Loop over number of trial points\n",
" x,y,z = random_point(radius) # Random point in cube round sphere\n",
" if point_in_sphere(radius,x,y,z) : # Is it inside, if so count it\n",
" inside += 1\n",
" p += 1 \n",
"\n",
" # Estimated volume is given by ratio of number of points inside\n",
" # times volume of cube round sphere\n",
" #\n",
" estimate = float(inside)/float(points)*math.pow(2*radius,3)\n",
" vol = 4.0*math.pi*math.pow(radius,3)/3.0\n",
"\n",
" print(\"Estimate volume is : \" + str(estimate) + \" real volume is : \" + str(vol))\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:227f88f0 tags:
# Sphere Volume
%% Cell type:code id:695f4dd5 tags:
```
python
"""
Program to estimate the volume of a sphere by counting the
number of random points that fall inside compared with the
number of points in a box round the sphere.
"""
import
math
import
random
def
random_point
(
max_range
):
"""
Return a random 3d point in range +/max_range
"""
x
=
random
.
uniform
(
-
max_range
,
max_range
)
y
=
random
.
uniform
(
-
max_range
,
max_range
)
z
=
random
.
uniform
(
-
max_range
,
max_range
)
return
x
,
y
,
z
def
point_in_sphere
(
radius
,
x
,
y
,
z
):
"""
Check if a point is within a sphere specified.
"""
if
x
*
x
+
y
*
y
+
z
*
z
<=
radius
*
radius
:
return
True
else
:
return
False
def
main
():
"""
Main program
"""
points
=
int
(
input
(
"
Number of points :
"
))
# Ask for initial conditions
radius
=
float
(
input
(
"
Radius of sphere :
"
))
# Counters for number inside and look variable
inside
,
p
=
0
,
0
while
p
<=
points
:
# Loop over number of trial points
x
,
y
,
z
=
random_point
(
radius
)
# Random point in cube round sphere
if
point_in_sphere
(
radius
,
x
,
y
,
z
)
:
# Is it inside, if so count it
inside
+=
1
p
+=
1
# Estimated volume is given by ratio of number of points inside
# times volume of cube round sphere
#
estimate
=
float
(
inside
)
/
float
(
points
)
*
math
.
pow
(
2
*
radius
,
3
)
vol
=
4.0
*
math
.
pi
*
math
.
pow
(
radius
,
3
)
/
3.0
print
(
"
Estimate volume is :
"
+
str
(
estimate
)
+
"
real volume is :
"
+
str
(
vol
))
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