Monte Carlo Python Plotting Question

In summary: Overall, your logic seems sound and your commands seem suitable for achieving your goal. Just make sure to test your code thoroughly and make any necessary adjustments as you go along. In summary, the conversation is about creating a code to simulate the projection of a radiation source onto a collimated detector and checking if the projection is circular. The code involves creating a list of vectors and organizing their components to plot on a 2D graph. The use of numpy and matplotlib packages is also mentioned.
  • #1
clope023
992
131
If this is better served in a section like Nuclear Engineering please let me know.

Homework Statement

Simulate the projection of a radiation source onto the plane of a collimated detector. Check if said projection is circular.

The attempt at a solution

What I want to do is create a list of vector objects that will be 'moved' from their initial position to the final position at the plane of the detector. The source point is at the origin. The collimator entrance is 2mm (in the y-direction) away and the detector plane is 4mm (in the y-direction) away.
I want the final position of the vectors to be randomly distributed in the plane and check if the x and z elements of those vectors satisfy the equation of the circle with the radius of that circle being the radius of the circular collimator and then plot those positions. I want to be using vpython's vector class for some of the vector operations though if simple nested lists are just as good I will use those too. This is the code I have so far.

import numpy as np
import random
import matplotlib.pyplot as plt
from visual import vector
r = 2.5 #radius of collimator in mm
l = 50.8 #length of collimator in mm
n = 1000 #number of test points
lst = []
for i in range(n):
lst.append(vector(0,0,0)) #creates 1000 vectors at the origin
lst1 = []
rr = r*random.random()
for j in lst:
lst1.append(j + vector(rr, l, rr))
def toList(v):
return [v.x, v.y, v.z] #returns the components of the vector and places them in a list
lst2 = []
for k in lst1:
lst2.append(toList(k)) #places those list of vector components into another list
lst3 = []
for u in lst2:
lst3.append(u.pop())
# pop function removes and returns the last element in the list

At this point I have a list filled with a 1000 3 element lists, and I want to organize the x (0th elements) and z (2nd elements) components and put them in a 2d plot and then check if they satisfy the equation of the circle like so:

plt.plot(x,z,'o')
plt.show()
lst4 = []
lst5 = []
for v in lst3:
if v[0]**2 + v[2]**2 <= r**2:
lst4.append(v)
if v[0]**2 + v[2]**2 > r**2:
lst5.append(v)

So right now this where my logic takes me, am I on the right track? Are my commands right to do what I want to do? I appreciate any and all help, thanks very much.
 
Technology news on Phys.org
  • #2
Yes, you're on the right track. You have the basic steps outlined for what you need to do, so now it's just a matter of implementing them in code. For example, you can use the numpy package to quickly calculate the squared distance between each vector and the origin, which will determine whether or not it falls within the circle. You can also use matplotlib to plot your results.
 

1. What is Monte Carlo Python Plotting?

Monte Carlo Python Plotting is a method of using random sampling to simulate and analyze data in order to solve complex problems. It is often used in scientific and mathematical fields to estimate values and make predictions.

2. How does Monte Carlo Python Plotting work?

In Monte Carlo Python Plotting, a large number of random samples are generated and used to create a model or simulation of a system. The results of these samples are then analyzed to estimate the behavior of the system and make predictions.

3. What are the applications of Monte Carlo Python Plotting?

Monte Carlo Python Plotting has a wide range of applications in various fields such as physics, engineering, finance, and economics. It is commonly used to analyze and predict the behavior of physical systems, optimize processes, and make financial decisions.

4. What are the advantages of using Monte Carlo Python Plotting?

One of the main advantages of Monte Carlo Python Plotting is its ability to handle complex problems that cannot be solved analytically. It also allows for the incorporation of uncertainty and variability in the data, making it a powerful tool for decision-making and risk analysis.

5. Are there any limitations to Monte Carlo Python Plotting?

Although Monte Carlo Python Plotting is a powerful tool, it does have some limitations. It can be computationally intensive and time-consuming, and the accuracy of the results depends on the quality of the random sample generated. It may also be challenging to use for problems with a large number of variables.

Similar threads

  • Programming and Computer Science
Replies
1
Views
746
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
645
  • Programming and Computer Science
Replies
1
Views
3K
Replies
6
Views
2K
Replies
0
Views
2K
  • Atomic and Condensed Matter
Replies
3
Views
855
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top