ComeçarComece de graça

Utility maximization

Bill is an aspiring piano student who allocates hours of study in classical \(c\) and modern \(m\) music. His preferences are represented by the same utility function you just plotted:

\(U(c, m)=c^{0.7}m^{0.3}\).

Hours of study sum up to 2 (\(c+m=2\)) daily. Help Bill find the optimal studying plan.

np and minimize have been loaded for you. We have already imported symbols, diff and solve from SymPy, defined c and m as symbols and defined the Utility function U for you.

Este exercício faz parte do curso

Introduction to Optimization in Python

Ver curso

Instruções do exercício

  • Define the utility function by unpacking vars and returning the negated function.
  • Define the constraint function.
  • Set up the constraint with type and fun.
  • Perform optimization and extract the results for c and m.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Define the utility function
def utility_function(vars):
    ____
    return ____

# Define the constraint function
def constraint(vars):
    return ____

initial_guess = [12, 12]  

# Set up the constraint
constraint_definition = {____}

# Perform optimization
result = ____
____ = result.____

print("Optimal study hours for classical music:", round(c, 2))
print("Optimal study hours for modern music:", round(m, 2))
Editar e executar o código