Equilibrium
Solution
import sympy
p = sympy.Symbol("p")
demandEquation = 10-p
supplyEquation = p
prices = []
demandQ = []
supplyQ = []
for price in range(0,11):
demandQ += [demandEquation.subs(p,price)]
supplyQ += [supplyEquation.subs(p,price)]
prices += [price]
print(prices)
print(demandQ)
print(supplyQ)
Now let’s solve for the equilibrium price
EquilibriumP = sympy.solve(demandEquation-supplyEquation)
print(EquilibriumP)
EquilibriumP = EquilibriumP[0]
print(EquilibriumP)
We get an array with 5 for the price, but since we want first (and only) element we do EquilibriumP = EquilibriumP[0]. Once we do this we get the number not an array. The way we get specific elements of an array is by indexing with [x] where x is the element we want, except 0 is the first, 1 is the second and so on.
To solve for the quantity we could just plug into one of the equations.
EquilibriumQ = demandEquation.subs(p,EquilibriumP)
print(EquilibriumQ)
Finally, let’s graph the equilibrium.
import matplotlib.pyplot as plt
plt.plot(demandQ,prices)
plt.plot(supplyQ,prices)
plt.plot(EquilibriumQ,EquilibriumP, 'ro')
plt.xlabel("Quantity")
plt.ylabel("Price")
plt.show()