-
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 a Supply and Demand Class
Solution
import sympy
import matplotlib.pyplot as plt
p = sympy.Symbol("p")
class supplyDemand:
def __init__(self,demandEquation,supplyEquation):
self.demandEquation = demandEquation
self.supplyEquation = supplyEquation
self.priceEnd = sympy.solve(demandEquation)[0]
self.prices = []
self.demandQ = []
self.supplyQ = []
for price in range(0,self.priceEnd+1):
self.prices += [price]
self.demandQ += [demandEquation.subs(p,price)]
self.supplyQ += [supplyEquation.subs(p,price)]
self.startingQ = sympy.solve(demandEquation-supplyEquation)[0]
self.startingP = demandEquation.subs(p,self.startingQ)
self.mode = "Normal"
def plot(self):
plt.plot(self.demandQ,self.prices,'k')
plt.plot(self.supplyQ,self.prices,'k')
plt.plot(self.startingQ,self.startingP, 'bo')
plt.xlabel("Supply and Demand Quantity")
plt.ylabel("Price")
plt.show()
In our init function, I added an attribute mode because we are going to have different modes for the equilibrium. As well, in the plotting function, we have ‘k’ which means plot the line as black, and ‘bo’ which means plot a blue circle.
Now, to understand international trade, we want to reflect back on shortages and surpluses. If we have a lowered price, we get a theoretical shortage where firms want to produce less than the consumer. In international trade, however, the firms that will sell at this level produce a lowered quantity, and the shortage is handled by imports from foreign countries. When the price that the world sells at is higher, we get the opposite, a surplus. The consumers want to buy less than what the firms want to produce, so only some of the consumers still buy the product. The surplus is then sold as exports to other countries.
Now, we are going to create a class function which figures out what happens when we introduce international trade. We will walk through the theory, and then create a list of requirements for our function. The first requirement is:
1)