-
Introduction 4
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
-
Production Possibilities Frontier 4
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
-
Trade 3
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
-
Demand 4
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
-
Supply 2
-
Lecture5.1
-
Lecture5.2
-
-
Equilibrium 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
-
Curve Movements 4
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
-
Elasticity and Revenue 5
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Lecture8.4
-
Lecture8.5
-
-
Taxes 7
-
Lecture9.1
-
Lecture9.2
-
Lecture9.3
-
Lecture9.4
-
Lecture9.5
-
Lecture9.6
-
Lecture9.7
-
-
Consumer and Producer Surplus 8
-
Lecture10.1
-
Lecture10.2
-
Lecture10.3
-
Lecture10.4
-
Lecture10.5
-
Lecture10.6
-
Lecture10.7
-
Lecture10.8
-
-
Imports and Exports 4
-
Lecture11.1
-
Lecture11.2
-
Lecture11.3
-
Lecture11.4
-
-
Tariffs 2
-
Lecture12.1
-
Lecture12.2
-
Creating the Equilibrium Function
Solution
def yourFunction(x):
for y in range(2,11):
print(x*y)
yourFunction(5)
The first thing we are going to do is a create a function for finding and graphing the equilibrium of two curves. We will utilize sympy for the equations of the curve. What we will do is walk through creating the function piece by piece.
Step one is creating a symbol for price with sympy.
import sympy
p = sympy.Symbol("p")
Now, we are going to also want to plot so we will need to import that library. Another important note about functions is that when we create variables inside them, they only last while we use the function. We are going to need three arrays to begin with, one for the prices, and two other arrays for quantity demanded and supplied at each level.
import sympy
import matplotlib.pyplot as plt
p = sympy.Symbol("p")
def Equilibrium(demandEquation,supplyEquation,priceStart,priceEnd):
prices = []
demandQ = []
supplyQ = []
Right now all our function does is initialize these empty arrays. Let’s create the arrays like we have in the past, then test them out by printing them.
import sympy
import matplotlib.pyplot as plt
p = sympy.Symbol("p")
def Equilibrium(demandEquation,supplyEquation,priceStart,priceEnd):
prices = []
demandQ = []
supplyQ = []
for price in range(priceStart,priceEnd+1):
prices += [price]
demandQ += [demandEquation.subs(p,price)]
supplyQ += [supplyEquation.subs(p,price)]
print(prices)
print(demandQ)
print(supplyQ)
Equilibrium(10-p,p,0,10)
Now let’s get rid of the print statements, and find the equilibrium price and quantity. Notice that when we get the price we add “[0]” because sympy will return an array, but we want the first (and in this case only) value.
import sympy
import matplotlib.pyplot as plt
p = sympy.Symbol("p")
def Equilibrium(demandEquation,supplyEquation,priceStart,priceEnd):
prices = []
demandQ = []
supplyQ = []
for price in range(priceStart,priceEnd+1):
prices += [price]
demandQ += [demandEquation.subs(p,price)]
supplyQ += [supplyEquation.subs(p,price)]
equilibriumP = sympy.solve(demandEquation-supplyEquation)[0]
equilibriumQ = demandEquation.subs(p,equilibriumP)
print(equilibriumP)
print(equilibriumQ)
Equilibrium(10-p,p,0,10)
Finally, let’s plot our graph and also print out what the equilibrium.
import sympy
import matplotlib.pyplot as plt
p = sympy.Symbol("p")
def Equilibrium(demandEquation,supplyEquation,priceStart,priceEnd):
prices = []
demandQ = []
supplyQ = []
for price in range(priceStart,priceEnd+1):
prices += [price]
demandQ += [demandEquation.subs(p,price)]
supplyQ += [supplyEquation.subs(p,price)]
equilibriumP = sympy.solve(demandEquation-supplyEquation)[0]
equilibriumQ = demandEquation.subs(p,equilibriumP)
plt.plot(demandQ,prices)
plt.plot(supplyQ,prices)
plt.legend(["Demand","Supply"])
plt.plot(equilibriumQ,equilibriumP, 'ro')
plt.xlabel("Supply and Demand Quantity")
plt.ylabel("Price")
plt.show()
print("The equilibrium price is "+str(equilibriumP)+" and equilibrium quantity is "+str(equilibriumQ)+".")
Equilibrium(10-p,p,0,10)
Challenge