Here is some code for generating random 3D curves based on physics. The code does the following:
1) Initialize a particle with mass, charge, position, and velocity in 3D space 2) Have a magnetic flux density B which randomly changes magnitude and direction once per second 3) Calculate the force on the particle and simulate the trajectory
Since the magnetic field does no work, the speed of the particle remains constant. Since the curve is 3D, each run of the simulation results in three curves (projections on the xy, yz, and xz planes). One such set of curves is shown below (click to enlarge). Each run produces a different curve, since the B field changes randomly. The program spits out spatial coordinates which can be plotted in Excel.
importmathimportrandom# ConstantsB=3.0v=1.0m=1.0q=1.0dt=10.0**(-3.0)######################################### Initialize Simulationt=0.0count=0thetav=2.0*math.pi*random.random()phiv=math.pi*random.random()thetaB=2.0*math.pi*random.random()phiB=math.pi*random.random()x=0.0y=0.0z=0.0vx=v*math.cos(thetav)*math.sin(phiv)vy=v*math.sin(thetav)*math.sin(phiv)vz=v*math.cos(phiv)Bx=B*math.cos(thetaB)*math.sin(phiB)By=B*math.sin(thetaB)*math.sin(phiB)Bz=B*math.cos(phiB)Fx=q*(vy*Bz-vz*By)Fy=-q*(vx*Bz-vz*Bx)Fz=q*(vx*By-vy*Bx)ax=Fx/may=Fy/maz=Fz/m######################################### Run simulation and plot pointsprint"t x y z"whilet<=100.0:x=x+vx*dty=y+vy*dtz=z+vz*dtvx=vx+ax*dtvy=vy+ay*dtvz=vz+az*dtFx=q*(vy*Bz-vz*By)Fy=-q*(vx*Bz-vz*Bx)Fz=q*(vx*By-vy*Bx)ax=Fx/may=Fy/maz=Fz/mifcount%1000==0:B=1.0+3.0*random.random()thetaB=2.0*math.pi*random.random()phiB=math.pi*random.random()Bx=B*math.cos(thetaB)*math.sin(phiB)By=B*math.sin(thetaB)*math.sin(phiB)Bz=B*math.cos(phiB)ifcount%10==0:printt,x,y,zt=t+dtcount=count+1
This discussion board is a place to discuss our Daily Challenges and the math and science
related to those challenges. Explanations are more than just a solution — they should
explain the steps and thinking strategies that you used to obtain the solution. Comments
should further the discussion of math and science.
When posting on Brilliant:
Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.
Markdown
Appears as
*italics* or _italics_
italics
**bold** or __bold__
bold
- bulleted - list
bulleted
list
1. numbered 2. list
numbered
list
Note: you must add a full line of space before and after lists for them to show up correctly
# I indented these lines
# 4 spaces, and now they show
# up as a code block.
print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.
print "hello world"
Math
Appears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3
2×3
2^{34}
234
a_{i-1}
ai−1
\frac{2}{3}
32
\sqrt{2}
2
\sum_{i=1}^3
∑i=13
\sin \theta
sinθ
\boxed{123}
123
Comments
@Steven Chase yeah it seems me interesting.
Today are you posting any new problem. Please reply otherwise I have to keep checking.
If you are posting can you post a LRC circuit second order problem?
Hey bro, you should pursue physics or engineering when you get older. Use your talent! I'm probably going to be a theoretical physicist when I leave high school😁
The equations of motion cannot be solved analytically from my point of view. They are nonlinear and coupled. However, you could try deriving the equations of motion. That would be a nice exercise.
@Karan Chatrath
–
@Karan Chatrath I have some question
1) Which version of python you use?
2) Did you have taken the membership of MATLAB, I have seen it's very expensit. Can I get the pirated version from anywhere??
@A Former Brilliant Member
–
Engineering colleges and universities all over the world give students access to MATLAB and that is the way I use that software too. Obtaining a pirated version of the software is highly unethical and I will not endorse that.
Python, on the other hand, is freely available. I do not use it, but from my limited understanding, the version matters less, especially so if you want to perform simple numerical analyses.
@A Former Brilliant Member
–
It is because schools do not usually have a MATLAB licence. You may check with yours, but I think it is unlikely. You rightly said that an individual licence is very expensive so the only way to get it is when you start your Bachelor studies if that is what you plan to do. Engineering colleges and technical institutions do have licences which their students can access.
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
@Steven Chase yeah it seems me interesting.
Today are you posting any new problem. Please reply otherwise I have to keep checking.
If you are posting can you post a LRC circuit second order problem?
Log in to reply
Hey bro, you should pursue physics or engineering when you get older. Use your talent! I'm probably going to be a theoretical physicist when I leave high school😁
Hey, good stuff!
Log in to reply
Thanks. Each new curve has never been seen before, and will never be seen again
Log in to reply
Random indeed!
Nice note. I remember you sharing insights about generating random curves, a few months back.
Log in to reply
@Karan Chatrath Sir please post the analytical solution of this Problem.
Thanks in advance
Log in to reply
The equations of motion cannot be solved analytically from my point of view. They are nonlinear and coupled. However, you could try deriving the equations of motion. That would be a nice exercise.
Log in to reply
@Karan Chatrath I have some question
1) Which version of python you use?
2) Did you have taken the membership of MATLAB, I have seen it's very expensit. Can I get the pirated version from anywhere??
Log in to reply
Python, on the other hand, is freely available. I do not use it, but from my limited understanding, the version matters less, especially so if you want to perform simple numerical analyses.
Log in to reply
@Karan Chatrath so will you give me that access to me??? Please I just want to know from where can I get???
Log in to reply
Log in to reply
@Karan Chatrath Ok sir.
But why?
Log in to reply
Yes, those were 2D curves. I realized that a changing magnetic field was the perfect thing to generate 3D random curves