Let us now consider a horizontally mounted spring with a mass attached to it. If we equate Hooke’s law with Newton’s second law, we obtain the following differential equation:
$$
m\ddot{x}(t) = -kx(t)
$$
Here, the acceleration $a$ has been replaced by the second derivative of position with respect to time. This is a second-order differential equation, and it can be solved in various ways.
The simplest method is to try a candidate solution. There exists a function whose second derivative is the same as the original function, up to a sign: the sine function. This immediately tells us that the motion of a spring system is a harmonic oscillation.
Assuming the solution has the form:
$$
x(t) = A \sin(\omega t)
$$
and substituting it into the differential equation, we find after differentiation and simplification that the angular frequency of a mass-spring system is:
$$
\omega = \sqrt{\frac{k}{m}}
$$
From this, the period of the oscillation can be calculated as:
$$
T = 2\pi \sqrt{\frac{m}{k}}
$$
According to the earlier discussion, the function
$$
y(t) = A \cos(\omega t)
$$
is also a valid solution to the differential equation. Therefore, both sine and cosine functions describe harmonic oscillations correctly. The choice depends on the initial conditions of the system.