# Simulation- Newton cardle

Hello,
I’m trying to simulate Newton’s Cardle using python, but the differential equation is very complicated to solve!
M

You might find better luck at stackoverflow

1 Like

Edit: I misread the question and misunderstood the differential equation you’re solving, so the rest of my answer here is looking at an approximation of a regular pendulum

Your real question here is about numerically solving initial value problems with a second derivative I think

• Expand the left hand side with some approximation of the second derivative in terms of some time-step `h` such as with the following:
``````d^2 x / dt^2 approximately (x_(n-1) - 2x_n + x_(n+1))/h^2
``````
• Expand the delta x so they’re explicit in `x_n` and `x_(n+1)` etc, e.g. `delta x_(n-1, n) = x_n - x_(n-1)`

• Rearrange so that x_(n+1) is on the left hand side, in form `x_(n+1) = ...`

• Start with `x_1 = x_0` (unless you know otherwise that derivative is at the start nonzero)

• At each time-step `h`, use the function above to calculate the next position

Edit: The above is for a regular pendulum, if you can expand on what the actual differential equation’s terms represent (in particular `n` which I probably erroneously assumed meant positions at given time stamps) maybe I can change this answer

1 Like

Well it’s a good idea to start with, but the problem is a little bit different (but more complicated!!!)

Here’s a screenshot that may explain the situation clearly!
I hope it’s enough to make you understand the problem!

Thank you for helping me…

It’s past midnight for me now, but if I’m understanding correctly the n is referring to the number of the ball, right?

I’ll take a look again tomorrow

1 Like

Yes!
if we have 5 balls for example, for the 2nd ball we gonna have: kdelta x_1,2 - kdelta x_2,3…

Thank you! Good night!

So I just thought about it before going to sleep and I think the example I gave with the pendulum above is actually already nearly there

the only part that involves a derivative in terms of time is the left hand side, so we already have most of the work done

let’s refer to `x_n` by its current integer time index `t` as well via `x_(n,t)`

we expand `d^2 x_n / d t^2` as `(x_(n,t-1) - 2x_(n,t) + x_(n,t+1)) / h^2`

we rearrange `m d^2 x_n / d t^2 = (rest of equation)` into `x_(n, t+1) = h^2 (x_(n, t-1) - 2x_(n,t)) / m + (h^2 / m)(rest of equation)`

The rest of the current equation happens at time stamp `t` and not at `t-1`

Then, we have simultaneous differential equations for each ball

Let’s represent the balls in an array, with each ball being at its index, and lets keep an older copy (for time `t-1`) around

We can therefore calculate the next array based on the two previous arrays, using our formula for `x_(n,t+1)`

does that make sense?

edit: for initial conditions you’d need the 0th and 1st timestamps, which can be set to each ball’s equilibrium except for the first ball, which is dropped from somewhere (remember to set t=1’s time value to the same as t=0 to start it from 0 velocity)

For me I get
x_(n, t-1) = ( h^2/m)*(rest of eq) - x_(n, t-1) - 2x_(n, t) ???

But what about the (rest of equation)!??
We have( x_n ) in it!
I can’t see how we can solve it like this!

In the rest of the equation, we have x_(n-1, t), x_(n, t), and x_(n+1, t)

Note that they’re all at the current time stamp, so we already know all their values

So to get the state of the balls at the next time stamp, we only need to know them at the current time stamp and the previous timestamp

Did you manage to progress further?

Well, today I didn’t have the time…
I passed a math exam
But tomorrow I will try to solve it!

1 Like

I have tried, but I still can’t see how to express all this!
I managed only to do it for a simple pendulum (first ball)

Please if you can help me at least to start

I’m on my lunch break at the moment, so I’ll give a more full reply after work, but I’ll try now

Start by storing the positions of the balls in an array, called balls

Create another array called old_balls ( this is to store the balls’ positions at the previous time step)

Create another array called next_balls, this is what the balls will be in the next time step

Then, keep doing the following as many times as you wish:

Set the values in future balls using the formula you have for x_(n, t+1)

Set old balls equal to balls
Set balls equal to future balls

You know what!? Thank you for helping me but I just checked the deadline to finish the project and I still have only 24 hours! I’m done! It’s over for me! I thought I have more time!

I know it’s not logical! But If you can do a favor for me it will be niceful from you!

24h is plenty of time I think, most of the work has been done already, it’ll just take a bit of coding after that

so let’s talk first about what precisely you don’t understand, and that can speed things along a bit

24h maybe! But I have other things to study! Math, physics especially we are in period of tests

Well I don’t understand how we make the first ball hit the others! I think this is the first thing that must be done!

Basically it’s in the K ( 2R - (x_(n+1) - x_n) ) term

If the distance between the two balls is less than the sum of each radius (i.e. a ball has made contact) then that term is nonzero and contributes to the acceleration of the ball

Note an earlier line of the text you posted: If this term is less than 0, the whole term is 0 (that is, if the balls are too far away to touch this term is 0)

so when we have an equation x_(n, t+1), that term in the equation is the part that will only appear when the balls make contact

Yes it does make sense! But how to turn that into a python language!!! I really don’t know!