I don't think I get the mass-spring system differential equation right [closed]

I want to make a program simulating a mass-spring system but my solution to the differential equation is nothing I've seen on the internet and I doubt its correctness.

The program should simulate the motion by the moment a body is attached to a massless spring in its default position. I want to measure from that position.

The equation is $$\ddot y+\omega^2y=g,\omega=\sqrt{\frac{k}{m}}$$

What I get is $$y=\frac{2g}{\omega^2}\sin^2\frac{\omega t}{2}$$

When I first got this I thought it's correct because the body should oscillate back to the position it was released from. But what if it's released from another position? The solution to the differential equation should be the same, because the initial conditions are still $$y(0)=0,\dot y(0)=0$$, but the motion is not the same. Also, I've read that gravity shouldn't affect the system but according to my result it does.

I will appreciate any help because I'm struggling with this for a few hours.

I don't think I get the mass-spring system differential equation right

For a vertical mass-spring system in the presence of gravity, the Newtonian Equation of Motion is:

$$ma=-kx+mg$$

Or:

$$m\ddot{x}+kx=mg$$

Substitute: $$\omega^2=\frac{k}{m}$$, so that:

$$\ddot{x}+\omega^2 x=g\tag{1}$$

This is a non-homogeneous ODE.

To solve it, make a substitution:

$$u=\omega^2 x -g$$

Calculate $$\ddot{u}$$ and substitute all this into $$(1)$$. The new ODE will be homogeneous. Solve, then back-substitute and apply initial conditions for a full solution (not shown).

$$\ddot{u}=\omega^2 \ddot{x}$$

$$\frac{\ddot{u}}{\omega^2}+u=0$$

$$\Rightarrow \ddot{u}+\omega^2 u=0$$

The question remains, why is the system affected by gravity?

You can see this the following way

Let $$x(t):= y(t) - g/\omega^2$$ and derive the differential equation satisfied by $$x(t)$$. Notice that $$x(t)$$ satisfies the equation of a simple harmonic oscillator, with no mention of $$g$$ whatsoever. Introducing the variable transformation lifts the dependence on gravity.

This corresponds physically to viewing oscillations from a different reference level, namely $$-g/\omega^2$$, which you can verify is the equilibrium height of the spring-mass system.

• Please post full answers rather than hints. However, I would suggest covering concepts in your answer rather than just showing math. Aug 18, 2021 at 19:38
• @BioPhysicist edited the post now. I was not aware hints were not wanted, even though the poster asks for "any help". Aug 18, 2021 at 20:16