1-Dimensional Elastic Collisions (C Program)

In summary, the conversation discusses the creation of a C program to solve a 1-dimensional physics problem involving two elastic balls constrained between elastic walls. The user will input the radii, initial position, and initial velocity of the balls. The conversation includes discussions on the programming aspect of the problem, including suggestions for algorithms and resources on elastic collisions and Lagrangian mechanics. Two possible solutions are presented, with one including code examples. The conversation also addresses the possibility of particles colliding with each other and the walls, and how to handle the collision in the code. Finally, a potential issue with one of the solutions is mentioned, where the velocity reversal after a collision may cause the particles to move in the wrong direction.
  • #1
MichalXC
41
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
  • #2
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...
 
  • #3
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.
 
  • #4
Nice. Thank you. I know differential equations relatively well. I will check out your suggestion and reply.
 
Last edited:
  • #5
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?
 
  • #6
If I have two particles bouncing back and forth, do I have to solve simultaneous differential equations?
 
  • #7
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.
 
  • #8
Will the paricles collide with each other and the walls with e=1?

Is it only velocity and position as input?
 
Last edited:
  • #9
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
 

Related to 1-Dimensional Elastic Collisions (C Program)

1. What is a 1-dimensional elastic collision?

A 1-dimensional elastic collision is a type of collision between two objects in which both the conservation of momentum and the conservation of kinetic energy are preserved. This means that the total momentum and total kinetic energy of the system before and after the collision are the same.

2. How can I calculate the final velocities of the objects in a 1-dimensional elastic collision?

The final velocities of the objects can be calculated using the conservation of momentum and the conservation of kinetic energy equations. These equations take into account the mass and initial velocities of the objects, as well as the coefficient of restitution, which represents the elasticity of the collision.

3. What is the coefficient of restitution and how does it affect a 1-dimensional elastic collision?

The coefficient of restitution is a value between 0 and 1 that represents the elasticity of a collision. A value of 1 indicates a perfectly elastic collision, where no kinetic energy is lost and the objects bounce off each other. A value of 0 represents a completely inelastic collision, where the objects stick together after the collision and no kinetic energy is conserved.

4. Can a 1-dimensional elastic collision occur in real life?

Yes, 1-dimensional elastic collisions occur in real life. An example of this is the collision between billiard balls on a pool table. In this scenario, the balls are nearly perfectly spherical and the collision is relatively frictionless, allowing for the conservation of momentum and kinetic energy.

5. How can I use a C program to simulate a 1-dimensional elastic collision?

To simulate a 1-dimensional elastic collision using a C program, you can set up the initial conditions of the objects (mass, velocity, and coefficient of restitution), calculate the final velocities using the conservation equations, and then output the results. You can also include conditions for different types of collisions, such as a completely inelastic collision where the objects stick together.

Similar threads

  • Classical Physics
Replies
20
Views
921
  • Mechanics
Replies
6
Views
1K
Replies
14
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
1K
Replies
5
Views
3K
  • Introductory Physics Homework Help
Replies
20
Views
1K
  • Introductory Physics Homework Help
Replies
15
Views
2K
  • Introductory Physics Homework Help
Replies
16
Views
2K
  • Mechanics
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
Back
Top