Equation of motion including gear ratio Two-mass rotational system has the following form and is represented in following structural diagram.

where $\tau_e$, $\omega_1$ and $J_m$ - motor torque, angular velocity and moment of inertia
$\tau_s$, $\tau_s$, $\omega_2$ and $J_d$ - shaft torque, load torque, angular velocity and load moment of inertia;
$K_{md}$ - shaft stiffness
Problem: how to include a gear ratio $N=\frac{\omega_1}{\omega_2}$ in equation of motion and in in a block diagram respectively?
$L=V-P=J_m \frac{\omega_1^2}{2}+J_d \frac{\omega_2^2}{2}-\frac{K_{md}(\phi_1-\phi_2)^2}{2}$
$V$ - kinetic, and $P$ - potential energy
Here is the Lagrangian for the entire system. And I don’t understand how to insert the gear ratio here?
 A: Directly plug in constant ratio between gear rotation angles which is   the same ratio between  their time derivative angular velocities. If
$$N=\dfrac{{\phi_1}}{{\phi_2}}=\dfrac{\dot{\phi_1}}{\dot{\phi_2}}=\dfrac{\omega_1}{\omega_2}$$
then the action is:
$$L=V-P=\frac{\omega_2^2}{2} \left(J_m N^2 +J_d \right)-\frac{K_{md}}{2}\;(N-1)^2\;\phi_2^2.$$
A: I dont have enough reputation to comment or downvote the existing (accepted) answer.
Why I think the answer is different
[motor]---)--[GB]---)--===shaft=====--)--[load]
          phi1    phi1'             phi2

In the presence of a flexible shaft there are 3 angular positions. See the ASCII diagram above. The important point to note is that the definition of N is not $\omega_1/\omega_2$. It is $\omega_1 / \omega_1'$, and $\omega_1' \neq \omega_2$ when the shaft is in twisted condition. The shaft can be in twisted condition at various times while the system is operating.
In fact the OP has asked

I have a question right away. If N=1, then the term with the shaft stiffness is lost? This is very strange, how to explain it?

So I am posting my answer to the duplicate question asked the OP at engineering.se.
My answer at engineering.se
Assuming that the gear box is on the left end of the shaft (i.e. no flexible shaft between motor and gearbox).

*

*The angular velocity on the left end of the gear box is $\omega_1$.

*The angular velocity of the shaft side of the gear box is assumed as $\omega_1' = \frac{\omega_1}{N}$.

*The angular velocity on the right end of the shaft is $\omega_2$. So the torque on the shaft is $\pm K_m (\frac{\phi_1}{N} - \phi_2)$. (sign to be checked).

*Because of the way I described the gearbox, $\omega_1' < \omega_1 $. so the torque on the shaft when acting on the motor through the gearbox is $\frac{1}{N}$. This can be seen in the below derivation.

*Since I have assumed that shaft is directly connected to the load, the torque in the shaft is made available 1:1. This can also be seen in below derivation.

(Below derivation to be verified independently by OP)
$$
L = \frac{J_m \omega_1^2}{2}
+ \frac{J_d \omega_2^2}{2}
+ \frac{Km (\frac{1}{N} \phi_1 - \phi_2)^2}{2}
$$
$$
\frac{d}{dt} \frac{\partial L}{\partial \omega_1}
= \frac{d}{dt} J_m \omega_1
= J_m \frac{d \omega_1}{dt}
$$
$$
\frac{\partial L}{\partial \phi_1}
= \frac{K_m}{\color{red}{N}} (\frac{1}{N} \phi_1 - \phi_2)
$$
Similarly for the other body also (exercise left to you).
$$
\frac{\partial L}{\partial \phi_2}
= -K_m (\frac{1}{N} \phi_1 - \phi_2)
$$
I have not considered the input torque. It can be added to this result.
