The numerical way to solve a spring is to calculate the forces of the spring every frame, and translate that into a position
The force is F = -kx -bv, where F = ma (k being the return constant of your spring, how stiff it is kinda, and b is the drag coefficient, or how fast it slows down. You can also write it as -bv^2)
So, a = -(k/m) * x -(b/m) * v
We can calculate the acceleration numerically using x and v from the last frame
Velocity is (if you approximate) acceleration * the time, so you can write
v = a * dt + v (we have to add to the previous velocity)
And same for the position
x = v * dt + x
dt is the amount of time between frames, a smaller dt gives a more accurate simulation
These are annoying though, and springs can be simulated analytically pretty well
The analytical formula for a spring with no drag force is x(t) = Acos(wt - C), where w is sqrt(k/m), but at this point you can just hardcode w. It’s the angular frequency. C is an offset that will depend on initial position, at t = 0. A is the amplitude
Now, if there is drag
Analytically, solving for drag forces with velocity squared is very hard (don’t even know if there is a formula for it), so the drag force will be -bv
There are 3 different solutions for this one, depending on the value of b, k, and m. But you can just chose whichever solution you want
First solution is x(t) = e^(-Gt) * Acos(wt + C)
It is very similar to the one without drag, but with an added term to make the oscillations die down over time. G determines how fast it decreases
Second solution is x(t) = Ae^(-Gt) * (C1 + C2t)
This solutions is a critically dampened system, the x(t) basically just smoothly goes to 0, has no oscillations
Third solution is x(t) = A1e^(-G1t) + A2e^e(-G2t)
This one is similar to the second one but it overshoots x = 0, and goes in the negative before going to 0
One thing to note is that we don’t have to follow these formulas. We can use modified versions that fit our needs, for example e^(-Gt) is a bit annoying because it never truly reaches 0, you basically have to stop it when it is basically 0. It could be replaced with a quadratic equation, in the form of (x - 1)^2, at t = 0, it is 1, and at t = 1, it is 0, so we know when it reaches 0. You can also adjust the exponent to adjust its slope
Hope this helps!