Bắt đầu ngayBắt đầu miễn phí

Finding the derivative

For some objective functions, the optimum can be found using calculus by finding the derivative of the function. sympy offers a solution to avoid manually calculating these derivatives. Suppose you work in a firm that produces toy bicycles. You have the following objective function to calculate your costs, \(C\), which is dependent on the variable, \(q\), the quantity of bicycles produced:

\(C = 2000 - q^2 + 120q\)

To find the optimum value of \(q\), you'll find the derivative of the cost with respect to the quantity, \(\frac{dC}{dq}\), using sympy.

symbols, diff, and solve have been loaded for you in this and the next exercise.

Bài tập này là một phần của khóa học

Introduction to Optimization in Python

Xem khóa học

Hướng dẫn bài tập

  • Create a sympy symbol, q, that represents the quantity of bicycles produced.
  • Find the derivative of the objective function c with respect to q, dc_dq, using sympy.
  • Solve the derivative to find the optimum price.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# Convert q into a symbol
q = ____
c = 2000 - q**2 + 120 * q

# Find the derivative of the objective function
dc_dq = ____
print(f"The derivative is {dc_dq}.")

# Solve the derivative
q_opt = ____
print(f"Optimum quantity: {q_opt}")
Chỉnh sửa và Chạy Mã