1-Dimensional Elastic Collisions (C Program)

Click For Summary

Discussion Overview

The discussion revolves around writing a C program to simulate 1-dimensional elastic collisions between two particles constrained between elastic walls. Participants explore the programming aspects of implementing the physics of elastic collisions, including user inputs for initial conditions and the calculation of collision times.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on programming a simulation for elastic collisions, initially considering the inclusion of radii but later simplifying the problem to point particles.
  • Another participant suggests using the Runge-Kutta algorithm for solving the differential equations governing the motion of the particles.
  • There is a discussion about whether the motion of the two particles can be considered chaotic and how to derive the specific differential equations for their interactions.
  • Participants debate the need to solve simultaneous differential equations for the two particles and the implications of their interactions.
  • One participant provides a code snippet for simulating the collisions, highlighting potential issues with error accumulation and the need for position updates upon collisions.
  • Another participant expresses confusion about the output of their implementation and seeks clarification on the collision handling logic.
  • Concerns are raised about the accuracy of the simulation due to the precision of time slices and the need to adjust the precision to minimize error.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the implementation of the simulation and the underlying physics. There is no consensus on the best approach to handle particle collisions or the specific implementation details, indicating multiple competing views and unresolved issues.

Contextual Notes

Participants note limitations in their understanding of the physics involved, particularly regarding the chaotic nature of the system and the mathematical formulation of the collisions. There are also concerns about error propagation in the simulation due to the chosen precision and the handling of particle positions upon collisions.

MichalXC
Messages
39
Reaction score
0

Homework Statement



I am trying to write (for my own interest) a program in C that solves a simple 1-dimensional physics problem. I want the program to determine the times (after t=0) of the first ten collisions for two elastic balls constrained between elastic walls at x=0 and x=a.

The user will input the radii, initial position, and initial velocity of the two balls.

Homework Equations



I know and understand the dynamical equations governing elastic collisions. My real question concerns the programming aspect... How should I attack the problem? Unfortunately, I am a programming noob and have only been programming for a few weeks.

The Attempt at a Solution



So far, I have used the printf and scanf functions to get the radii, initial position, and initial velocity of the two balls... Help.
 
Physics news on Phys.org
Okay, so I don't need the radii. The "objects" will be particles. This simplifies the physics, but I still don't know how to go about programming...
 
Do you know Ordinary Differential Equations? Look up the Runge-Kutta algorithm. That's likely what you need (or a similar algorithm). I wrote a small n-body simulation about 6 years ago, and have forgotten a few things, but that might get you started.

Might also look up the Spring-Mass problem. That might provide a gentle introduction.
 
Nice. Thank you. I know differential equations relatively well. I will check out your suggestion and reply.
 
Last edited:
I understand how a differential equation could describe, say, a pendulum or an oscillatory system. I also see how the RK algorithm can help us numerically solve such equations. As for my problem, however, the motion would be "chaotic," no? As in, non-periodic? Hmm... So I do not see from where to obtain the specific differential equation governing my two-body system. Help?
 
If I have two particles bouncing back and forth, do I have to solve simultaneous differential equations?
 
It would take me a while to get back up to speed on that stuff, and I don't really have the time at the moment. So hopefully someone will pitch in here and help out. Surely there must be someone around here who knows this stuff much better than I.

One thing you might want to look at is the wiki on elastic collisions:

http://en.wikipedia.org/wiki/Elastic_collision

If memory serves, you might want to look at this as well:

http://en.wikipedia.org/wiki/Lagrangian_mechanics

And that's all I've got at the moment. Hope that helps rather than confuses. Sorry I can't be of more assistance.
 
Will the paricles collide with each other and the walls with e=1?

Is it only velocity and position as input?
 
Last edited:
Jaynte,

Yes, the inputs will only be position and velocity, and the coefficient of restitution will be, as you said, e=1.

I want my program to output the times at which the particles collide with each other, but I am not very good with C yet, so I need some hints regarding implementation. Maybe an outline or some pseudocode? I would be most grateful.
 
  • #10
I have a pretty simple solution but maybe not the best.

x1 = particle closest to x=0
x2 = particle closest to x=a

for(i=0;i<T;i+=P){ //T=stop time (i.e. 1), P=precision (i.e. 0.001)
x1=v1*t+x01; //v1=initial velocity for particle 1, x01=initial position
x2=v2*t+x02; //v2=initial velocity for particle 2, x02=initial position

if(x1<=0 || x1>=x2){
printf("x1 collision at time: %f.\n",t); //x1 collides with lower boundary or x2
v1=v1*(-1); //change direction of particle

if(x2>=a || x2<=x2){
printf("x2 collision at time: %f.\n",t); //x2 collides with upper boundary or x1
v2=v2*(-1);
}

//Note: Now it will print twice if they collide with each other, I don't know if you want
//a collision with each other should count as one or two collisions
 
  • #11
Correction: This example above will give an accumulated error every time it collides.
There has to be a update in position also.

int i;
float t=0;
float a=10;
float v1=1;
float v2=2;
float P=0.001; //precision or delta t
float T=0.1;
float dist1=abs(v1*P);//distance traveled each delta t
float dist2=abs(v2*P);
float tmp;
float x1=1;
float x2=8;
float t_time=0;

for(i=0;i<1000;i++){
x1+=v1*t;
if(x1<0){
x1=0+dist1-x1; //Just to place it back inside the boundary when collision
v1=v1*(-1);
t_time+=t;
t=0;
printf("Collision with x=0 at time %f\n",t_time);
}
x2+=v2*t;
if(x2>a){
x2=2*a-dist2-x2; //Same here
v2=v2*(-1);
t_time+=t;
t=0;
printf("Collision with x=a at time %f\n",t_time);
}

if(x1>=x2){
tmp=x1;//Switch places with the particles
x1=x2;
x2=tmp;
v1=v1*(-1);
v2=v2*(-1);
t_time+=t;
t=0;
printf("Collision between particles at time %f\n",t_time);
}

t+=P;
}
 
Last edited:
  • #12
Thanks so much! I will look at your code when I get a chance later today.
 
  • #13
I read through the first code, and, even though there is some error propagation, I tried to implement it. Can you see anything wrong with this? The output I'm getting doesn't make sense.

Code:
	float x01, x02, v1, v2, x1, x2, t, T, P, L, i;
	
	x01 = 1; /* initial position of left-most particle */
	x02 = 2; /* initial position of right-most particle */
	v1 = 3; /* initial velocity of left-most particle */
	v2 = 4; /* initial velocity of right-most particle */
	P = 0.1; /* precision of time-slice */
	T = 100; /* duration of simulation */
	L = 10; /* size of box */
	t = 0; /* initialize time */
	
	for (i = 0; i < T; ++i) {
		
		x1 = v1 * t + x01;
		x2 = v2 * t + x02;
		t = t + P;
	
		if (x1 <= 0 || x1 >= x2) {
			printf("x1 collides at time %.1f\n", t);
			v1 = v1 * (-1);
		}

		if (x2 >= L || x2 <= x1) {
			printf("x2 collides at time %.1f\n", t);
			v2 = v2 * (-1);
		}
	}
}
 
Last edited:
  • #14
The problem, I think, is that once a particle runs into a boundary or another particle, we reverse the velocity, which places the particle "back on track." However, the velocity reversal will not hold once we place the particle back into an "acceptable region," so the particle will not continue to propagate in the correct direction. Does that make any sense?
 
  • #15
I see now that the collision with the particles would not change positions with eachouther, I meant they should change velocity with each other, but I am not sure it will work anyway.

It works for the collision with the walls anyway.

Why I placed it back inside the region is because otherwise it will be outside for one delta t and when reversed it will not end up on the correct place which will give an error in the posision. And the error will accumulate.

I place it at the mirror posision of where it ends up outside the region. (i.ex x1=-0.1 which in reality should be at x1=0.1 after the bounce)
 
  • #16
I think you should increase the precision to i.e. 0.0001, otherwise the distance
the particle traveles will be so great with that velocity.

You can debug by also printing the position at each delta t
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
1K
Replies
20
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K