From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Particle in 2D Box - Central Force?

Hi all, I'm trying to simulate the trajectory of a particle in a 2D box. Give it an initial position, velocity, acceleration, and make it reflect off the boundaries of the box. The part I'm struggling with is adding drag force and central force terms to the code.

 

I want to add a drag force term that is proportional to the square of the velocity. (F = - constant * velocity^2). Theoretically this should merely slow the particle down wherever it goes. However, when I do this, the program shortly fails because the acceleration reaches infinity. I tried making the constant of proportionality equal to a very small number (1E-10) and it didn't help - the program still failed, just after a longer period of time.

 

I run into the same issue when I add a central force term. The acceleration, and subsequently the velocity reaches infinite values.

 

Is there a way for me to overcome this bug? I would greatly appreciate any help. I have attached a copy of my VI below.

 

(Note: I'm not looking to copy anyone's code, but for a conceptual solution that I can apply.)

0 Kudos
Message 1 of 18
(4,392 Views)

First of all, you should try to simpliy the code, is overly complicated. Next time when you add example code, place reasoanble values into each control and make the values default. This way we can run it right away without having to guess what kind of inputs are needed.

 

Instead of having an array with two elements, you can use a simple complex scalar (CDB) to define the position. The three code parts below are the same! Arguably, yours (top) is very complicated while the bottom one is most simple. Try it!

 

 

 

(also remember that index array is resizeable)

0 Kudos
Message 2 of 18
(4,368 Views)

Without even looking at your code, I bet I can guess why you are having this problem.  Force is a vector, whose magnitude is proportional to the square of the velocity, but whose sign is the opposite of the sign of the velocity (so it retards the motion).  If (as you have written it) the force is always negative, it will always move the particle to the left.  When velocity is positive (to the right), this slows you down, and eventually brings the velocity to zero.  But unless you hit zero exactly, you might "step" to a small negative velocity.  OK, now you square this small number, it adds to the force, pushing harder to the left, the velocity increases, the force increases, the velocity increases (do you see a pattern here), and things Blow Up.

 

Bob Schor

0 Kudos
Message 3 of 18
(4,348 Views)

For some ideas, have a look at this VI that I posted here last year.

0 Kudos
Message 4 of 18
(4,339 Views)

I think your code only work for single particle. How do you add two other particles in this box?

0 Kudos
Message 5 of 18
(4,194 Views)
This is a long thread. Which "code" are you referring to?
0 Kudos
Message 6 of 18
(4,190 Views)

I'm referring the code you posted in this thread, "have a look at 'this vi' ". The code is " this vi".

0 Kudos
Message 7 of 18
(4,183 Views)

Unless there is a big modification, I don't think your code can work for the 3D box either because you will need to change the entire data type.

0 Kudos
Message 8 of 18
(4,181 Views)

Of course not.  But this thread is called "2D box",  not "3D box".  You'd have to change all the math in order to calculate 3 dimensional movements and forcesn, not just 2D.

 

And if you want more than 1 particle, then you might want to creata a 1-D array of clusters.  Or add another dimension to the other arrays.  Then iterate on each of the particles in turn inside a For Loop that is inside the while loop.  That is actually simpler modifciation to the VI as compared to a 2D to 3D modification.

0 Kudos
Message 9 of 18
(4,177 Views)

@karryli wrote:

I think your code only work for single particle. How do you add two other particles in this box?


Don't be so negative! 😄

 

I usually write scalable code and in this case the code can be transformed to support multiple particles with very minimal changes. All we need to do is change the initial array in multiples of two, e.g. using a FOR loop on the left as follows. The bulk of the code needs no changes at all! (see attached). Now it works for any number of particles. 😄

 

 

Of course currently the particles are considered infinitely small in diameter and thus are independent and cannot collide with each other. We would need to define a finite particle radius and add code to detect collisions betwen particles, which is a little bit more work. Try it!

 

Similarly, it could be easily expanded to 3D by keeping three coordinates and speeds per particle (x,y,z). Simply multiply the # of particles by 3 and adapt the display accordingly. An xy graph is insufficient, but a 3D graphs would work. Again, that would require very little code modification. You could even use two side-by-side xy graphs, adjust for the stereo projection and look at it using e.g. google cardboard or similar. 🙂

 

Of course you could equally easily go to 4D and 5D, just the display will be harder. 😮

Download All
0 Kudos
Message 10 of 18
(4,158 Views)