Energy balance in linear and nonlinear waves