# Find v(x) of a mass suspended from a spring

Staff Emeritus
Homework Helper
Gold Member
Thank you. I did follow these questions which had led me to that result.

x+h? as h being the displacement from x=0?
For one thing, x itself is the displacement from the location, x=0.
(If you are taking GPE to be zero at x=0, then h should not enter into this anymore.)

I asked for the GPE (gravitational potential energy) of the mass when it's at location x .

spsch
spsch
Oh! of course, so just mgx?

Homework Helper
Gold Member
2022 Award
Oh! of course, so just mgx?
Right, so what is the total mechanical energy at height x?

spsch
spsch
Right, so what is the total mechanical energy at height x?
(1/2)*m*v^2 + mgx+(1/2)*k*x^2?

Homework Helper
Gold Member
2022 Award
(1/2)*m*v^2 + mgx+(1/2)*k*x^2?
Good. And this should be constant, and we know all the values at t=0. So what general equation does that give you?

spsch
(1/2*m*v0^2) = (1/2)*m*v^2 + mgx+(1/2)*k*x^2

##v = \sqrt{ v_^2 - \frac {kx^2} {m}, - 2gx}## (/ I'm sorry tried to but won't convert)

v = sqrt( v0^2 - (k*x^2) /m), - 2*g*x)

could this be it?

Last edited:
Homework Helper
Gold Member
2022 Award
(1/2*m*v0^2) = (1/2)*m*v^2 + mgx+(1/2)*k*x^2

## v = \sqrt{ v_0^2 - \frac {kx^2} {m} - 2gx} ##

could this be it?
That's it. (Latex doesn't like [ SUB] etc. use _)

Can you turn that into t= an integral wrt x?

spsch
That's it. (Latex doesn't like [ SUB] etc. use _)
Hmm, I subbed the sub with _ but it shows a square now, but i'll figure this out too :-)
Can you turn that into t= an integral wrt x?

Hmm, with t? I think so, I'm anxious to see what solution I get. I'll try and get back.

PS, I am grateful that you made me work through the problem, even though it's a small difference, I now really understand what I did wrong. I don't think I would have this insight if you'd just told me to switch x with h. Thank you very much!

Homework Helper
I'm sorry tried to but won't convert
also: put a \ before the sqrt: $$v = \sqrt{ v_0^2 - \frac {kx^2} {m} - 2gx}$$

(you could 'check' this expression: when is ##v=0## and when is it at a maximum ?)

And this is not the whole 'it', but a step towards the differential equation that the exercise asks ...

My impression is that it wants an equation in the form ##{dv\over dx} = ## { an expression in ##x## }
##\ ##

spsch
Homework Helper
Gold Member
2022 Award
also: put a \ before the sqrt: $$v = \sqrt{ v_0^2 - \frac {kx^2} {m} - 2gx}$$

(you could 'check' this expression: when is ##v=0## and when is it at a maximum ?)

And this is not the whole 'it', but a step towards the differential equation that the exercise asks ...

My impression is that it wants an equation in the form ##{dv\over dx} = ## { an expression in ##x## }
##\ ##
Ah yes, @spsch, I was forgetting the original purpose of the thread. You were just checking an answer by using energy. So to get a differential equation involving v from your post #41 you now need to differentiate. But it is not clear to me whether you need to differentiate wrt t or wrt x. It depends what ODE you got that you are trying to verify.

spsch
spsch
also: put a \ before the sqrt: $$v = \sqrt{ v_0^2 - \frac {kx^2} {m} - 2gx}$$

(you could 'check' this expression: when is ##v=0## and when is it at a maximum ?)

And this is not the whole 'it', but a step towards the differential equation that the exercise asks ...

My impression is that it wants an equation in the form ##{dv\over dx} = ## { an expression in ##x## }
##\ ##

Probably a dumb question but... wouldn't dv/dx just be the derivative of the right side?

spsch
Ah yes, @spsch, I was forgetting the original purpose of the thread. You were just checking an answer by using energy. So to get a differential equation involving v from your post #41 you now need to differentiate. But it is not clear to me whether you need to differentiate wrt t or wrt x. It depends what ODE you got that you are trying to verify.
Hi Haruspex. I'm not sure what you mean with what ODE?

I had a solution which is actually what I got now with your help.

I got that by setting kx-mg = ma and using v*dv/dx for a as it was a hint on the problem.

The problem was looking for v(x)

Last edited:
spsch
So I got this as the derivative. Is this a(x)?

#### Attachments

• IMG_20190606_153747.jpg
71.9 KB · Views: 249
Homework Helper
\sqrt wasn't enough yet
##\TeX## now stumbles over _^ without anything in between

v_0^2 will fix it. You also don't really want the comma. And don't worry: we do it because we like to do it or else we wouldn't do it

Given up on ##\LaTeX## ? and back to images...

Methinks the image in #48 is a good answer to the problem statement in #19 (also an image ).
Whether it's still correct can be checked 'easily':
• ##{dv\over dx} = 0\ \ ## at ##\ \ x_0 = - {mg\over k} ##
• substitute ##\omega =\sqrt{k\over m}, \ \ \xi = x_0-x, \ \ v = ## ... oh, well, maybe not that easy ...
and all that for 3 points ...

Makes me curious to see parts b) and c)
##\ ##

spsch
Homework Helper
So I got this as the derivative. Is this a(x)?
I should think ##a(x) = -\omega^2 (x-x_0) \ \ ## with ##\ \ \omega^2 = {k\over m}##

In other words: ##{da\over dx} = -\omega^2 ##

spsch
spsch
Methinks the image in #48 is a good answer to the problem statement in #19 (also an image ).
Whether it's still correct can be checked 'easily':
• ##{dv\over dx} = 0\ \ ## at ##\ \ x_0 = - {mg\over k} ##
• substitute ##\omega =\sqrt{k\over m}, \ \ \xi = x_0-x, \ \ v = ## ... oh, well, maybe not that easy ...
and all that for 3 points ...

Makes me curious to see parts b) and c)
##\ ##

I think 48 is the derivative of the solution?

we solved a and b almost I think. a asks to set up the differential equation. b asks to solve it.

I haven't looked at what c and d is yet, but it seems not to difficult, c) asks you to graph the function v(x) in an x-v-diagramm.

And d finally asks you for the deviation (*Auslenkung, I don't quite know what that word in german means actually) and the speed of the mass when the spring is pulled 20 cm up or down.
I think it just means plug in 0.2 for x and state the value. so v(0.2).

a, b and c each give you 3 points, d 1 point.

I'll figure out Latex too! :-) It doesn't let me edit the previous post anymore, I think I did it too many times. But next chance I get.

spsch
I should think ##a(x) = -\omega^2 (x-x_0) \ \ ## with ##\ \ \omega^2 = {k\over m}##

In other words: ##{da\over dx} = -\omega^2 ##
Thank you for all the help!

Staff Emeritus
Homework Helper
Gold Member
From Post #41:
...

##v = \sqrt{ v_^2 - \frac {kx^2} {m}, - 2gx}## (/ I'm sorry tried to but won't convert)

v = sqrt( v0^2 - (k*x^2) /m), - 2*g*x)
Just some ##\LaTeX## stuff.

The ##\LaTeX## code you entered was: ##v = \sqrt{ v_^2 - \frac {kx^2} {m}, - 2gx}## .

Correcting the omission of the 0 (thanks @BvU) to give v_0^2 and letting ##\LaTeX## do its thing gives the following.

##v = \sqrt{ v_0^2 - \frac {kx^2} {m}, - 2gx}##

Drop the comma, wrap v_0 in braces and use \dfrac rather than \frac gives the result

## v = \sqrt{ {v_0}^2 - \dfrac {kx^2} {m} - 2gx } ##

from the input line:

## v = \sqrt{ {v_0}^2 - \dfrac {kx^2} {m} - 2gx } ##

spsch
Homework Helper
Gold Member

It looks like the big picture may still elude you. Remember that the zero of potential energy is arbitrary. The gravitational energy of mass ##m## at height ##y## above ground is ##U_g=mg(y-y_0)##; it is zero when ##y=y_0##. The elastic energy of a spring stretched (or compressed) by amount ##x## from its relaxed position is ##U_s=\frac{1}{2}k(x^2-x_0^2)##; it too is zero when ##x=x_0##. Note that ##x## in this context denotes "displacement of the spring from equilibrium", it does not denote position. The total potential energy is ##U_{tot}=U_g+U_s=mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)##.

Case 1. A horizontal spring-mass system
For the vertical motion
(a) you can choose the position of the origin, ##Y_{or}## at the vertical height of the mass
(b) you can choose the zero of potential energy at the origin (##y_0=0##)
Then ##U_g =mg(0-0)=0## throughout the motion.
For the horizontal motion
(a) you can choose the horizontal location of the origin, ##X_{or}## at the mass when the spring is unstretched. Remembering that ##x## stands for the displacement of the spring from the origin, clearly it also stands for the displacement of the mass from the origin. At this point you still have to choose the zero of spring energy.
(b) A common and useful choice is ##x_0=0##, but it could be anything you want.

With all of the above in mind, if the mass has speed ##v_0## when the spring is unstretched, the mechanical energy at some arbitrary ##x## is $$ME=K+U_g+U_s=\frac{1}{2}mv^2+0+\frac{1}{2}kx^2.$$Because mechanical energy is conserved and we have chosen the zero spring potential energy to be where the speed is maximum at ##x=0##, this becomes the familiar $$ME=\frac{1}{2}mv^2+\frac{1}{2}kx^2=\frac{1}{2}mv_0^2~~~~~(1)$$

Case 2. A vertical spring-mass system
Start with the most general expression for the mechanical energy and then you need to choose three quantities, the position of the origin ##Y_{or}##, the zero of gravitational potential energy ##y_0## and the zero of spring potential energy ##x_0##.$$ME=\frac{1}{2}mv^2+mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)~~~~~(2)$$

Suppose you choose the origin ##Y_{or}## to be at the position of the mass when the net force acting on it is zero, i.e. ##kx_{eq}-mg=0##. Then ##x_{eq}=\frac{mg}{k}## below the unstretched position of the spring. The displacement of the mass from this origin is the same as the additional stretch (or compression) of the spring call that quantity, ##\xi##, that is ##y=\xi##.
We choose the zero of gravitational potential energy to be at ##Y_{or}##. Then ##y_0=0##.
We also choose the zero of spring potential energy to be not where the spring is unstretched but where the additional stretch ##\xi = 0##. That occurs at ##x_{eq}## as we have previously found, that is ##x_0=x_{eq}=\frac{mg}{k}##.

Finally, the stretch of the spring from the equilibrium position ##x## is related to the additional stretch ##\xi## by$$x=\xi-\frac{mg}{k}.$$With all this equation (2) becomes$$ME=\frac{1}{2}mv^2+mg\xi-\frac{1}{2}k\left[\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2\right]$$This simplifies to$$ME=\frac{1}{2}mv^2+\frac{1}{2}k\xi^2~~~~~(3)$$
If we compare equations (1) and (2) for the mechanical energy of a spring-mass system, we see that they are identical in that, by judicious choices of the origin and the zeroes of the two potential energies, (a) the spring potential energy depends quadratically on the displacement relative to the (force) equilibrium position and (b) the potential energy contribution can be written out of the picture.

Last edited:
spsch
Homework Helper
Second @kuruman ! With all due respect, to me the nice feature of this exercise (see OP image in #19) is that they ask for a first-order differential equation in ##v(x) ##, as opposed to the usual ##\ddot x = -kx## second order treatment.

Note that the choice for x=0 has already been made in the problem statement. At that point the initial ##v_0## is also given. And the 1st order DE for ##v(x)## necessarily contains the gravitational potential energy.

And we should emphasize that the physics outcome is of course independent of the choices for zero coordinate and zero potential energy.
##\ ##

SammyS
Homework Helper
Gold Member
Note that the choice for x=0 has already been made in the problem statement. At that point the initial v0v_0 is also given. And the 1st order DE for v(x)v(x) necessarily contains the gravitational potential energy.
Yes, I tried to show how, using ME conservation, one can get the same result as integrating the DE, but I sidetracked myself. I should have picked ##Y_{or}=x_0=y_0=0## instead.

spsch
It looks like the big picture may still elude you. Remember that the zero of potential energy is arbitrary. The gravitational energy of mass ##m## at height ##y## above ground is ##U_g=mg(y-y_0)##; it is zero when ##y=y_0##. The elastic energy of a spring stretched (or compressed) by amount ##x## from its relaxed position is ##U_s=\frac{1}{2}k(x^2-x_0^2)##; it too is zero when ##x=x_0##. Note that ##x## in this context denotes "displacement of the spring from equilibrium", it does not denote position. The total potential energy is ##U_{tot}=U_g+U_s=mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)##.

Case 1. A horizontal spring-mass system
For the vertical motion
(a) you can choose the position of the origin, ##Y_{or}## at the vertical height of the mass
(b) you can choose the zero of potential energy at the origin (##y_0=0##)
Then ##U_g =mg(0-0)=0## throughout the motion.
For the horizontal motion
(a) you can choose the horizontal location of the origin, ##X_{or}## at the mass when the spring is unstretched. Remembering that ##x## stands for the displacement of the spring from the origin, clearly it also stands for the displacement of the mass from the origin. At this point you still have to choose the zero of spring energy.
(b) A common and useful choice is ##x_0=0##, but it could be anything you want.

With all of the above in mind, if the mass has speed ##v_0## when the spring is unstretched, the mechanical energy at some arbitrary ##x## is $$ME=K+U_g+U_s=\frac{1}{2}mv^2+0+\frac{1}{2}kx^2.$$Because mechanical energy is conserved and we have chosen the zero spring potential energy to be where the speed is maximum at ##x=0##, this becomes the familiar $$ME=\frac{1}{2}mv^2+\frac{1}{2}kx^2=\frac{1}{2}mv_0^2~~~~~(1)$$

Case 2. A vertical spring-mass system
Start with the most general expression for the mechanical energy and then you need to choose three quantities, the position of the origin ##Y_{or}##, the zero of gravitational potential energy ##y_0## and the zero of spring potential energy ##x_0##.$$ME=\frac{1}{2}mv^2+mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)~~~~~(2)$$

Suppose you choose the origin ##Y_{or}## to be at the position of the mass when the net force acting on it is zero, i.e. ##kx_{eq}-mg=0##. Then ##x_{eq}=\frac{mg}{k}## below the unstretched position of the spring. The displacement of the mass from this origin is the same as the additional stretch (or compression) of the spring call that quantity, ##\xi##, that is ##y=\xi##.
We choose the zero of gravitational potential energy to be at ##Y_{or}##. Then ##y_0=0##.
We also choose the zero of spring potential energy to be not where the spring is unstretched but where the additional stretch ##\xi = 0##. That occurs at ##x_{eq}## as we have previously found, that is ##x_0=x_{eq}=\frac{mg}{k}##.

Finally, the stretch of the spring from the equilibrium position ##x## is related to the additional stretch ##\xi## by$$x=\xi-\frac{mg}{k}.$$With all this equation (2) becomes$$ME=\frac{1}{2}mv^2+mg\xi-\frac{1}{2}k\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2$$This simplifies to$$ME=\frac{1}{2}mv^2+\frac{1}{2}k\xi^2~~~~~(3)$$
If we compare equations (1) and (2) for the mechanical energy of a spring-mass system, we see that they are identical in that, by judicious choices of the origin and the zeroes of the two potential energies, (a) the spring potential energy depends quadratically on the displacement relative to the (force) equilibrium position and (b) the potential energy contribution can be written out of the picture.
Hi, thank you kuruman. This was very helpful. What gives me trouble is how a(x) has been related to ## \sqrt{ \frac km} ## by just knowing x.
I see how it's the acceleration. I had some trouble following it. It's more clear now, but still a little fuzzy haha. I'm sure it shall clear up.

Last edited:
spsch
It looks like the big picture may still elude you. Remember that the zero of potential energy is arbitrary. The gravitational energy of mass ##m## at height ##y## above ground is ##U_g=mg(y-y_0)##; it is zero when ##y=y_0##. The elastic energy of a spring stretched (or compressed) by amount ##x## from its relaxed position is ##U_s=\frac{1}{2}k(x^2-x_0^2)##; it too is zero when ##x=x_0##. Note that ##x## in this context denotes "displacement of the spring from equilibrium", it does not denote position. The total potential energy is ##U_{tot}=U_g+U_s=mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)##.

Case 1. A horizontal spring-mass system
For the vertical motion
(a) you can choose the position of the origin, ##Y_{or}## at the vertical height of the mass
(b) you can choose the zero of potential energy at the origin (##y_0=0##)
Then ##U_g =mg(0-0)=0## throughout the motion.
For the horizontal motion
(a) you can choose the horizontal location of the origin, ##X_{or}## at the mass when the spring is unstretched. Remembering that ##x## stands for the displacement of the spring from the origin, clearly it also stands for the displacement of the mass from the origin. At this point you still have to choose the zero of spring energy.
(b) A common and useful choice is ##x_0=0##, but it could be anything you want.

With all of the above in mind, if the mass has speed ##v_0## when the spring is unstretched, the mechanical energy at some arbitrary ##x## is $$ME=K+U_g+U_s=\frac{1}{2}mv^2+0+\frac{1}{2}kx^2.$$Because mechanical energy is conserved and we have chosen the zero spring potential energy to be where the speed is maximum at ##x=0##, this becomes the familiar $$ME=\frac{1}{2}mv^2+\frac{1}{2}kx^2=\frac{1}{2}mv_0^2~~~~~(1)$$

Case 2. A vertical spring-mass system
Start with the most general expression for the mechanical energy and then you need to choose three quantities, the position of the origin ##Y_{or}##, the zero of gravitational potential energy ##y_0## and the zero of spring potential energy ##x_0##.$$ME=\frac{1}{2}mv^2+mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)~~~~~(2)$$

Suppose you choose the origin ##Y_{or}## to be at the position of the mass when the net force acting on it is zero, i.e. ##kx_{eq}-mg=0##. Then ##x_{eq}=\frac{mg}{k}## below the unstretched position of the spring. The displacement of the mass from this origin is the same as the additional stretch (or compression) of the spring call that quantity, ##\xi##, that is ##y=\xi##.
We choose the zero of gravitational potential energy to be at ##Y_{or}##. Then ##y_0=0##.
We also choose the zero of spring potential energy to be not where the spring is unstretched but where the additional stretch ##\xi = 0##. That occurs at ##x_{eq}## as we have previously found, that is ##x_0=x_{eq}=\frac{mg}{k}##.

Finally, the stretch of the spring from the equilibrium position ##x## is related to the additional stretch ##\xi## by$$x=\xi-\frac{mg}{k}.$$With all this equation (2) becomes$$ME=\frac{1}{2}mv^2+mg\xi-\frac{1}{2}k\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2$$This simplifies to$$ME=\frac{1}{2}mv^2+\frac{1}{2}k\xi^2~~~~~(3)$$
If we compare equations (1) and (2) for the mechanical energy of a spring-mass system, we see that they are identical in that, by judicious choices of the origin and the zeroes of the two potential energies, (a) the spring potential energy depends quadratically on the displacement relative to the (force) equilibrium position and (b) the potential energy contribution can be written out of the picture.

Does epsilon (if it is epsilon, the additional stretch) just represent dx here?

Homework Helper
Gold Member
2022 Award
With all this equation (2) becomes$$ME=\frac{1}{2}mv^2+mg\xi-\frac{1}{2}k\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2$$
Did you mean $$ME=\frac{1}{2}mv^2+mg\xi+\frac{1}{2}k\left(\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2\right)$$

kuruman
Homework Helper
Gold Member
2022 Award
Does epsilon (if it is epsilon, the additional stretch) just represent dx here?
##\xi##, pronounced "ksy", is the Greek equivalent of x. Not sure how you are defining dx here, but that usually refers to an infinitesimal change. @kuruman is using ##\xi## to stand for a variable like x but differing from it by a constant amount.

spsch
spsch
##\xi##, pronounced "ksy", is the Greek equivalent of x. Not sure how you are defining dx here, but that usually refers to an infinitesimal change. @kuruman is using ##\xi## to stand for a variable like x but differing from it by a constant amount.
Ah thanks, yes, I was able to infer that but wanted to make sure. dx for me is a small change usually?

Homework Helper
Gold Member
Did you mean $$ME=\frac{1}{2}mv^2+mg\xi+\frac{1}{2}k\left(\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2\right)$$
Absolutely what I meant. Thanks for the catch, I edited the post.

spsch
spsch
@kuruman I studied your very informative post quite a bit yesterday and today. It was quite helpful. I think I had the same intuition before, but it's more clear now. I think I dreamt about your equations with ksy. My problem may have been with thinking about the coordinate system and setting it up correctly, which I have a much better grasp on how to approach now. thanks @haruspex, @BvU @SammyS too!

I think my first solution was almost correct and the correct solution is v(x) = ## \sqrt { - \frac {kx^2} {m} - 2gx + 9} ## ?

SammyS and BvU
Homework Helper
Gold Member
@kuruman

I think my first solution was almost correct and the correct solution is v(x) = ## \sqrt { - \frac {kx^2} {m} - 2gx + 9} ## ?
Replace ##9## with ##v_0^2## and the solution will be fine.

spsch
spsch
Replace ##9## with ##v_0^2## and the solution will be fine.

Thank you Kuruman,
I apologize if I'm being slow, isn't 9 = ##v_0^2## ? And my C when I integrate the differential equation?
Should I not insert the constant values in a solution? (Meaning if it would be considered incorrect?)

Homework Helper
Gold Member
Thank you Kuruman,
I apologize if I'm being slow, isn't 9 = ##v_0^2## ? And my C when I integrate the differential equation?
Should I not insert the constant values in a solution? (Meaning if it would be considered incorrect?)

You are welcome.

When you write ##9##, you mean ##9## what? Apples, bananas, oranges? You could have written ##9~\rm{m^2/s^2}## but then what about the other quantities? It is bad form to mix symbols and numbers. You can, if you wish, replace all the quantities that are given to you with numbers, but then you will have to include their units next to them in the final equation.

The integration constant is taken care of because in reality you are doing a definite integral. Here is the gist of the solution
$$ma = -kx-g$$ $$mv\frac{dv}{dx} = -kx-g$$ $$m \int_{v_0}^{v(x)} v ~dv = -k\int_{0}^x x~dx-g \int_{0}^x dx$$ Note that the limits of integration reflect the physical situation: at the lower limit ##x=0## the speed matches the initial speed ##v_0##; at the upper limit of some arbitrary value ##x## the speed is ##v(x)##. Since this is a definite integral, no integration constant is necessary.

Last edited:
spsch
spsch
You are welcome.

When you write ##9##, you mean ##9## what? Apples, bananas, oranges? You could have written ##9~\rm{m/s^2}## but then what about the other quantities? It is bad form to mix symbols and numbers. You can, if you wish, replace all the quantities that are given to you with numbers, but then you will have to include their units next to them in the final equation.

The integration constant is taken care of because in reality you are doing a definite integral. Here is the gist of the solution
$$ma = -kx-g$$ $$mv\frac{dv}{dx} = -kx-g$$ $$m \int_{v_0}^{v(x)} v ~dv = -k\int_{0}^x x~dx-g \int_{0}^x dx$$ Note that the limits of integration reflect the physical situation: at the lower limit ##x=0## the speed matches the initial speed ##v_0##; at the upper limit of some arbitrary value ##x## the speed is ##v(x)##. Since this is a definite integral, no integration constant is necessary.
@kuruman thank you! I wasn't aware of that. That makes sense though.

spsch
It looks like the big picture may still elude you. Remember that the zero of potential energy is arbitrary. The gravitational energy of mass ##m## at height ##y## above ground is ##U_g=mg(y-y_0)##; it is zero when ##y=y_0##. The elastic energy of a spring stretched (or compressed) by amount ##x## from its relaxed position is ##U_s=\frac{1}{2}k(x^2-x_0^2)##; it too is zero when ##x=x_0##. Note that ##x## in this context denotes "displacement of the spring from equilibrium", it does not denote position. The total potential energy is ##U_{tot}=U_g+U_s=mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)##.

Case 1. A horizontal spring-mass system
For the vertical motion
(a) you can choose the position of the origin, ##Y_{or}## at the vertical height of the mass
(b) you can choose the zero of potential energy at the origin (##y_0=0##)
Then ##U_g =mg(0-0)=0## throughout the motion.
For the horizontal motion
(a) you can choose the horizontal location of the origin, ##X_{or}## at the mass when the spring is unstretched. Remembering that ##x## stands for the displacement of the spring from the origin, clearly it also stands for the displacement of the mass from the origin. At this point you still have to choose the zero of spring energy.
(b) A common and useful choice is ##x_0=0##, but it could be anything you want.

With all of the above in mind, if the mass has speed ##v_0## when the spring is unstretched, the mechanical energy at some arbitrary ##x## is $$ME=K+U_g+U_s=\frac{1}{2}mv^2+0+\frac{1}{2}kx^2.$$Because mechanical energy is conserved and we have chosen the zero spring potential energy to be where the speed is maximum at ##x=0##, this becomes the familiar $$ME=\frac{1}{2}mv^2+\frac{1}{2}kx^2=\frac{1}{2}mv_0^2~~~~~(1)$$

Case 2. A vertical spring-mass system
Start with the most general expression for the mechanical energy and then you need to choose three quantities, the position of the origin ##Y_{or}##, the zero of gravitational potential energy ##y_0## and the zero of spring potential energy ##x_0##.$$ME=\frac{1}{2}mv^2+mg(y-y_0)+\frac{1}{2}k(x^2-x_0^2)~~~~~(2)$$

Suppose you choose the origin ##Y_{or}## to be at the position of the mass when the net force acting on it is zero, i.e. ##kx_{eq}-mg=0##. Then ##x_{eq}=\frac{mg}{k}## below the unstretched position of the spring. The displacement of the mass from this origin is the same as the additional stretch (or compression) of the spring call that quantity, ##\xi##, that is ##y=\xi##.
We choose the zero of gravitational potential energy to be at ##Y_{or}##. Then ##y_0=0##.
We also choose the zero of spring potential energy to be not where the spring is unstretched but where the additional stretch ##\xi = 0##. That occurs at ##x_{eq}## as we have previously found, that is ##x_0=x_{eq}=\frac{mg}{k}##.

Finally, the stretch of the spring from the equilibrium position ##x## is related to the additional stretch ##\xi## by$$x=\xi-\frac{mg}{k}.$$With all this equation (2) becomes$$ME=\frac{1}{2}mv^2+mg\xi-\frac{1}{2}k\left[\left(\xi- \frac{mg}{k}\right)^2-\left(\frac{mg}{k}\right)^2\right]$$This simplifies to$$ME=\frac{1}{2}mv^2+\frac{1}{2}k\xi^2~~~~~(3)$$
If we compare equations (1) and (2) for the mechanical energy of a spring-mass system, we see that they are identical in that, by judicious choices of the origin and the zeroes of the two potential energies, (a) the spring potential energy depends quadratically on the displacement relative to the (force) equilibrium position and (b) the potential energy contribution can be written out of the picture.
Hi @kuruman I wanted to let you know that it sank in yesterday and I feel I understand the benefit and why to chose the coordinate axes like you suggested.
I've been doing lot's of problems and I tried to follow your suggestions but it hasn't quite made click, even though I could always get the correct answers.
Until yesterday I got to a problem that basically had the reverse as this problem.
The mass was on top of the spring instead of hanging from the spring.
It sounds silly, but from this perspective, everything made total sense.

After, I came here right away to thank you, but my browser or this site didn't work well together.
So belated, thank you kuruman and all who helped, it took a while but I think I've got an intuitive feeling (which is what I'm looking for) for these types of problems now!

SammyS and kuruman