Get startedGet started for free

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.

This exercise is part of the course

Introduction to Optimization in Python

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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))
Edit and Run Code