To derive the continuity equation you will need use the conservation of mass principles as well as the Reynolds transport theorem. First, the conservation of mass principles states that a system is a collection of unchanging contents. In other words the time rate of change of the system mass will equal zero.
(Eq 1)
The systems mass can be expressed in the following equation.
(Eq 2)
As mentioned above, the continuity equation is also derived using the Reynolds transport theorem. The equation below represents the general form of the Reynolds transport theorem.
(Eq 3)
The purpose of the Reynolds transport theorem is to link system ideas with control volume ideas. In this equation there is a variable “B” which represents an extensive property of the system. In addition there is a second variable “b” which is the intensive property of the system. To derive the continuity equation B = mass while b = 1. As a result, when considering this, the Reynolds transform theorem will result in the following equation. This equation is for a non deforming fixed control volume.
(Eq 3)
Incompressible Flow
The left side of equation 3 represents the change of mass in respect to time of the system. While on the right side of the equation the first part,
Next, the integrand,
(Eq 4)
Where $\dot{m} represents the mass flowrate.
(Eq 5)
Finally, when the flow is not steady, than the continuity equation for non-deforming fixed control volume will become the following.
(Eq 6)
What equation 6 states is that in order to conserve mass than the time rate of change of mass for the contents of the control volume plus any mass passing through the control surface must equal zero.
Compressible Flow
The above is used when the fluid is incompressible. However, there can be cases when the fluid is incompressible. When the fluid is incompressible,
(Eq 7)
Once the average velocity is determined the same continuity equations for incompressible flow can be used for compressible flow.