Calculating Final Positions & Velocities for M1, M2 & Spring After DeltaT

AI Thread Summary
The discussion focuses on simulating the dynamics of two masses connected by a massless spring, specifically calculating their final positions and velocities after a time increment, deltaT. The user seeks guidance on numerical methods, particularly Euler's method, to avoid issues with overly rapid spring compression that leads to unrealistic results. It is emphasized that the spring force, calculated from compression, acts equally on both masses rather than being divided. Additionally, the user inquires about implementing damping in the spring's force equation, confirming the understanding that the damping term relates to velocity, which could be based on the relative motion of the masses. The conversation highlights the importance of fine-tuning the simulation for accurate real-time audio synthesis.
mikejm
Messages
40
Reaction score
2
Let's say you have two masses on either side of a spring. Mass 1 is connected to the end of a spring. The spring itself has no mass. Mass 2 is free in space. So you have:

Code:
[M1]-[spring]           [M2]
So it's more descriptive, I'll name the variables like you might in programming. Let's define each element as follows.

Mass 1:
  • m1Mass = mass of M1
  • m1PosInit = initial position of M1
  • m1PosFinal = final position of M1
  • m1VelInit = initial velocity of M1
  • m1VelFinal = final velocity of M1

Mass 2:
  • m2Mass = mass of M2
  • m2PosInit = initial position of M2
  • m2PosFinal = final position of M2
  • m2VelInit = initial velocity of M2
  • m2VelFinal = final velocity of M2
Spring:
  • springLengthAtRest = natural length of spring when not compressed at rest
  • springLengthInit = initial length of spring
  • springLengthFinal = final length of spring
  • springCompressionInit = springLengthAtRest - springLengthInit
  • springCompressionFinal = springLengthAtRest - springLengthFinal
  • springForce = force of the nonlinear spring based on spring compression (eg. from a simple equation like F(x) = -k x^{c})

Time:
  • deltaT = amount of time that passes from initial state to final state

At the initial state, mass 2 is already in contact with the end of the spring (or is just about to contact it) with a given momentum. Mass 1 is traveling with a given momentum as well. The spring has a given amount of initial compression (which could be zero or non-zero).

How do you calculate the final positions and velocities after a certain increment of time (deltaT) for all the elements?

I'm trying to simulate a collision in a program I'm writing just for fun but stuck on this. If it's easier to give the spring some mass that's fine too. Whatever works. Thanks a bunch.
 
Last edited:
Physics news on Phys.org
The problem is appreciably harder if the spring has some mass - don’t do that if you can avoid it.

How much calculus do you know? The equations for the position and velocity of each mass are the solutions to differential equations.
 
Nugatory said:
The problem is appreciably harder if the spring has some mass - don’t do that if you can avoid it.

How much calculus do you know? The equations for the position and velocity of each mass are the solutions to differential equations.

Thanks. Yeah I was figuring giving the spring mass would make it much harder. We'll leave it with no mass then.

I can try to know as much as I need to know. :smile: I never knew how to do imaginary math with Euler's formula or Laplace transforms last year and now to a limited extent I can (had to learn similarly to solve some problems). Two years ago I didn't know a line of C++ and now I code freely. I need to solve this problem now or my program design is stuck on it. So it's non-negotiable for me to figure it out.

I have looked at as many websites and textbook examples as I can for collisions but they all only talk about mass-mass, not mass-spring-mass. Also most don't focus on the goal of finding the resulting positions/momentums after a given increment of time from an initial given state.

Without any further help or direction I'm totally stuck on this. Any help or guidance would be very much appreciated.
 
Do you need an analytical solution or are numerical solutions ok?
 
Dale said:
Do you need an analytical solution or are numerical solutions ok?
Numerical is totally fine and probably desirable!

FYI, if you're curious, this is for simulating the collision of a piano hammer or guitar plectrum (both of which are typically modeled as a mass (ie. hand/hammer) connected to a spring (ie. plectrum/felt) impacting another mass (ie. string segment)). It's a real time audio synthesiser I'm designing that needs to be calculated sample-by-sample so it needs to be efficient and doesn't need to be exactly precise.

Thanks.
 
mikejm said:
It's a real time audio synthesiser I'm designing that needs to be calculated sample-by-sample so it needs to be efficient and doesn't need to be exactly precise.
Then you should probably calculate it numerically using something like Euler’s method.

Basically, you know the initial positions, velocities, and forces, and with any set of positions you can calculate the forces. So you break your time into small steps, maybe 1/10 of a sample or less. Then ##t_n=n * dt## and then you calculate the next steps based on the current values:
##x(t_{n+1})=x(t_n)+v(t_n)dt ##
##v(t_{n+1})=v(t_n)+F(t_n,x_n,...)dt/m##
 
Dale said:
Then you should probably calculate it numerically using something like Euler’s method.

Basically, you know the initial positions, velocities, and forces, and with any set of positions you can calculate the forces. So you break your time into small steps, maybe 1/10 of a sample or less. Then tn=n∗dt and then you calculate the next steps based on the current values:
x(tn+1)=x(tn)+v(tn)dt
v(tn+1)=v(tn)+F(tn,xn,...)dt/m

Thanks Dale. That's sort of what I tried doing already but I didn't think about the possibility of doing it with even finer detail than per sample in the way you suggest. The problem is the "hammer/hand" was moving so quickly relative to the sample rate it was collapsing the spring almost fully on the first contact which then threw things off because in reality the spring force would have prevented such a great collapse. I was ending up with major spring collapses with massive spring forces that never would have been generated in real life.

So I will at each step:
  • Increment the two masses' positions based on their velocities.
  • Calculate the force of the spring based on their new positions.
  • Calculate their new velocities based on their old velocity and the spring force.
I can subdivide the sample into enough increments to get this done with finer detail to avoid the problem I'm having. Thanks that's really really helpful and I never would have thought of "oversampling" in this way to fix the problem. I also wasn't sure if my method I was already using was correct - I just thought it made sense and was making it up as I went. Glad to hear it's actually the right way to deal with it. :smile:

Can I ask you just two small follow up questions?

Spring force when calculated by compression as in F(x) = kx^c (where x is total amount of spring compression) is applied equally to both masses right? Ie. It's not divided into 2 is it and half applied to one mass, half to the other?

Also, how would I implement damping on my spring if I wanted to? I understand springs can oscillate if not damped. It shouldn't oscillate in my case because the spring/hammer/plectrum will release quickly then be reset, but I'm just curious. I think the equation for a nonlinear damped spring is:

F(x) = k*x^c - d*v

Where k is the spring constant, c is a constant for the nonlinear property, and d is a damping coefficient. Is this typically correct?

If so, how would I integrate that into the solution? What exactly does the "v" term represent velocity of?

Would the "v" term be based on the velocity of the mass at that end? Or the velocity of one mass minus the other to get the "net" velocity? Would it be the same for both masses or different?

Thanks again. You've been super helpful.
 
Last edited:
Back
Top