This code create a graphical user interface (GUI) application that calculates the optimal demands based on the Cobb-Douglas utility function. The application is built using Python's built-in Tkinter library.
The user inputs the values of income (M), prices (p1, p2), and the Cobb-Douglas function's coefficients (alpha, beta). Upon clicking the 'Calculate' button, the application uses these inputs to calculate and display the optimal quantities of two goods (x1, x2), and the utility (U) using the formula for the Cobb-Douglas utility function.
The application also displays the mathematical representation of the Cobb-Douglas utility function and the budget constraint, providing users a clear understanding of the calculations happening behind the scenes.
from tkinter import *
# Create the main window
root = Tk()
# Set the title of the window
root.title("Cobb-Douglas Optimal Demand Calculator")
def calculate_optimal_demands():
M = float(entry_M.get())
p1 = float(entry_p1.get())
p2 = float(entry_p2.get())
alpha = float(entry_alpha.get())
beta = float(entry_beta.get())
x1 = (M/p1) * (alpha/(alpha + beta))
x2 = (M/p2) * (beta/(alpha + beta))
u=x1**(alpha)*x2**(beta)
label_x1_result.config(text=str(x1))
label_x2_result.config(text=str(x2))
label_u_result.config(text=str(u))
# Set the title of the window
root.title("Cobb-Douglas Optimal Demand Calculator")
# Create the input fields and labels
Label(root, text="M").grid(row=0, column=0)
entry_M = Entry(root)
entry_M.grid(row=0, column=1)
Label(root, text="p1").grid(row=1, column=0)
entry_p1 = Entry(root)
entry_p1.grid(row=1, column=1)
Label(root, text="p2").grid(row=2, column=0)
entry_p2 = Entry(root)
entry_p2.grid(row=2, column=1)
Label(root, text="alpha").grid(row=3, column=0)
entry_alpha = Entry(root)
entry_alpha.grid(row=3, column=1)
Label(root, text="beta").grid(row=4, column=0)
entry_beta = Entry(root)
entry_beta.grid(row=4, column=1)
# Create the calculate button
Button(root, text="Calculate", command=calculate_optimal_demands).grid(row=5, column=0, columnspan=2)
# Create the output fields and labels
Label(root, text="x1").grid(row=6, column=0)
label_x1_result = Label(root, text="")
label_x1_result.grid(row=6, column=1)
Label(root, text="x2").grid(row=7, column=0)
label_x2_result = Label(root, text="")
label_x2_result.grid(row=7, column=1)
Label(root, text="u").grid(row=8, column=0)
label_u_result = Label(root, text="")
label_u_result.grid(row=8, column=1)
# Show the Cobb-Douglas Utility Function and Budget Constraint
Label(root, text="Cobb-Douglas Utility Function: U = x1^alpha * x2^beta").grid(row=9, column=0, columnspan=2)
Label(root, text="Budget Constraint: M = p1*x1 + p2*x2").grid(row=10, column=0, columnspan=2)
# Start the event loop
root.mainloop()
from IPython.display import Image
Image(filename='C:/Users/juanc/Downloads/cobb-douglas-calculator.png')