For any thermodynamic system that is under going a process, there will be an energy balance. This energy balance is expressed by the general equation below.
(Eq 1) $E_{in}-E_{out}=ΔE_{system}$
What this equation is stating is that the energy “in” minus the energy “out” equals the change of energy in the system. In addition, the energy balance of a system can also be expressed as a rate of change.
(Eq 2) $\dot{E}_{in}-\dot{E}_{out}=dE_{system}/dt$
Furthermore, for constant rates, the total quantities will be related to the quantity per unit time during the time interval $Δt$.
(Eq 3) $Q=\dot{Q}Δt$, $W=\dot{W}Δt$, and $E=(dE/dt)Δt$
In addition, energy balance can also be expressed as a per unit mass.
(Eq 4) $e_{in}-e_{out}=Δe_{system}$
This equation is obtained by dividing equation one by the mass of the system, $m$. Finally, energy balance can be expressed in a differential form.
(Eq 5) $δE_{in}-δE_{out}=dE_{system}$ or $δe_{in}-δe_{out}=de_{system}$
Energy Balance of a Cycle
During a thermodynamics cycle the initial and final state will be identical. Hence,
(Eq 6) $ΔE_{system}=E_2-E_1=0$
In turn, this will simplify to the following.
(Eq 7) $E_{in}-E_{out}=0$ or $E_{in}=E_{out}$
Finally, due to the fact that a closed system does not involve any mass flow across the boundary, the energy balance for heat and work interaction can be expressed in the following terms.
(Eq 8) $W_{net,out}=Q_{net,in}$ or $\dot{W}_{net,out}=\dot{Q}_{net,in}$
This expression states that the net work output of a cycle will equal the net heat input for a closed system.
When you are analyzing these types of problems it is sometimes difficult to tell the direction that heat and work are transferring in and out of system. As a result, it can be helpful to use classical thermodynamic sign convention. This means that it is assumed that heat is transferred into the system and work is done by the system. In turn, the following equation is used to express classical thermodynamic sign convention.
(Eq 9) $Q_{net,in}-W_{net,out}=ΔE_{system}$ or $Q-W=ΔE$
If you obtain a negative value for $Q$ or $W$ than the assumed direction of energy transfer is wrong and needs to reversed.