\(x_1, ..., x_k >= 0\) (realistic diet intake can not be negative)
\(x_1e_1 + x_2 e_2 + ... + x_k e_k >= E_{lower}\), total energy above the lower limit
\(x_1e_1 + x_2 e_2 + ... + x_k e_k <= E_{upper}\), total energy below the upper limit
\(x_1p_1 + x_2 p_2 + ... + x_k p_k >= P_{lower}\), total protein below the upper limit
\(x_1p_1 + x_2 p_2 + ... + x_k e_k <= P_{upper}\), total protein below the upper limit
And so on.
Solve the optimization problem
This setting is a quadratic program (QP). It is an optimization problem with quadratic objective, and inequality constraints. We do not have equality constraints in this setting.
With R, there are various software to find a solution:
nloptr in nloptr package (non-linear optimization),
constrOptim in stats package, which relies on optim function,