It is well known that the time period of a harmonic oscillator when mass $m$ and spring constant $k$ are constant is $T=2\pi\sqrt{m/k}$.
However, I would be interested to know what the time period is if $k$ is not constant. I have searched hours after hours for right answers from Google and came up with nothing. I am looking for an analytical solution.
Best Answer
Here is a solution for a spring force that varies directly with displacement. It thus varies with time implicitly, but has no explicit dependence on time or any other variable.
Givens and Assumptions
Objective
Find the period of oscillation, $T$
Solution
Starting from conservation of energy, the sum of the kinetic and potential energy of the mass must be equal to the total energy, which is constant. $$KE(x)+PE(x)=E$$ $$KE(x)=\frac{1}{2}mv^2(x)$$ $$PE(x)=\intop_0^xdx'\,F(x')$$ So $PE(x)$ is the potential energy stored in the spring, with $x'$ as just an integration variable.
We can think of $PE(x)$ as another way of defining the force-displacement relationship of the spring. We can define the potential energy versus displacement or the force versus displacement, and getting the other one is fairly easy.
Now, at $x=A$, $KE(x=A)=0$, so $PE(A)=E$ is known.
And so we have $$\frac{1}{2}mv^2(x)=PE(A)-PE(x)$$ Solving for $v(x)$, $$v(x)=\sqrt{\frac{2}{m}\left(PE(A)-PE(x)\right)}$$
Because $v=\frac{dx}{dt}$, we can also write $$dt=\frac{dx}{v(x)}$$ Integrating both sides, the time to go from a position $x_0$ to $x_1$ is $$\Delta t = \intop_{x_0}^{x_1}\frac{dx}{v(x)}$$ In particular, we know the time required to go from $x=0$ to $x=A$ is $T/4$, so $$T=4\intop_0^A\frac{dx}{v(x)}$$ $$T=4\intop_0^A\frac{dx}{\sqrt{\frac{2}{m}}\sqrt{PE(A)-PE(x)}}$$ which further simplifies to...
Final Result
$$T=\sqrt{8m}\intop_0^A\frac{dx}{\sqrt{PE(A)-PE(x)}}$$
Check of Result
For the linear case, $F(x)=kx$, so $PE(x)=\frac{1}{2}kx^2$ and $PE(A)=\frac{1}{2}kA^2$, which gives $$T=\sqrt{8m}\intop_0^A\frac{dx}{\sqrt{\frac{k}{2}}\sqrt{A^2-x^2}} =4\sqrt{\frac{m}{k}}\intop_0^A\frac{dx}{\sqrt{A^2-x^2}}$$ This integral can be looked up in a table, to obtain $$T=4\sqrt{\frac{m}{k}}\left(\sin^{-1}(1)-sin^{-1}(0)\right)=4\sqrt{\frac{m}{k}}\frac{\pi}{2}$$ $$T=2\pi\sqrt{\frac{m}{k}}$$ as expected. (QED)
We can dispense with the assumption that $F(x)$ is odd if we define two amplitude values: $A_+ > 0$ for the amplitude in the positive direction and $A_- < 0$ for the amplitude in the negative direction.
The total oscillator energy, $E = PE(A_+) = PE(A_-)$, so we can still call it $PE(A)$ as long as we remember what that means now.
Then, the period is twice the time required to go from $A_-$ to $A_+$, and so $$T=2\intop_{A_-}^{A_+}\frac{dx}{\sqrt{\frac{2}{m}}\sqrt{PE(A)-PE(x)}}$$ $$T=\sqrt{2m}\intop_{A_-}^{A_+}\frac{dx}{\sqrt{PE(A)-PE(x)}}$$