jeffrey1963 commited on
Commit
02cc45b
·
verified ·
1 Parent(s): ca59565

Upload 2 files

Browse files
linear_equilibrium_calculations.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # linear_equilibrium_calculations.py
2
+
3
+ import re
4
+ import numpy as np
5
+
6
+ def extract_numbers(phrase):
7
+ """Extract numerical values from a text input."""
8
+ return [float(num) for num in re.findall(r'-?\d+\.?\d*', phrase)]
9
+
10
+ def calculate_linear_equilibrium(demand_intercept, slope_demand, supply_intercept, supply_slope):
11
+ """Compute Competitive Equilibrium: Quantity and Price for LINEAR functions."""
12
+ if (supply_slope - slope_demand) == 0:
13
+ raise ValueError("Error: Division by zero in equilibrium calculation!")
14
+
15
+ Q_eq = (demand_intercept - supply_intercept) / (supply_slope - slope_demand)
16
+ P_eq = demand_intercept + slope_demand * Q_eq
17
+ return round(Q_eq, 2), round(P_eq, 2)
plot_functions.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # plot_functions.py
2
+
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import os
6
+ from linear_equilibrium_calculations import calculate_linear_equilibrium # Updated import
7
+
8
+ def plot_equilibrium(demand_intercept, slope_demand, supply_intercept, supply_slope):
9
+ """Generates a supply & demand equilibrium plot (Linear Model)."""
10
+
11
+ Q_range = np.linspace(0, 20, 100)
12
+ demand_curve = demand_intercept + (slope_demand * Q_range)
13
+ supply_curve = supply_intercept + (supply_slope * Q_range)
14
+
15
+ plt.figure(figsize=(6, 4))
16
+ plt.plot(Q_range, demand_curve, label="Demand Curve", color="blue")
17
+ plt.plot(Q_range, supply_curve, label="Supply Curve", color="green")
18
+
19
+ # Calculate equilibrium using the new function
20
+ Q_eq, P_eq = calculate_linear_equilibrium(demand_intercept, slope_demand, supply_intercept, supply_slope)
21
+
22
+ # Calculate CS & PS
23
+ CS_top = demand_intercept
24
+ PS_bottom = supply_intercept
25
+
26
+ CS = 0.5 * (CS_top - P_eq) * Q_eq
27
+ PS = 0.5 * (P_eq - PS_bottom) * Q_eq
28
+
29
+ # Highlight CS (shaded area above equilibrium price)
30
+ plt.fill_between([0, Q_eq], [CS_top, P_eq], P_eq, color="blue", alpha=0.3, label="Consumer Surplus")
31
+
32
+ # Highlight PS (shaded area below equilibrium price)
33
+ plt.fill_between([0, Q_eq], P_eq, [PS_bottom, P_eq], color="green", alpha=0.3, label="Producer Surplus")
34
+
35
+ # Mark equilibrium point
36
+ plt.scatter(Q_eq, P_eq, color="red", label=f"Equilibrium ({Q_eq}, {P_eq})")
37
+
38
+ plt.xlabel("Quantity")
39
+ plt.ylabel("Price")
40
+ plt.legend()
41
+ plt.title("Supply & Demand Equilibrium (Linear Model)")
42
+ plt.grid()
43
+
44
+ plot_filename = "equilibrium_plot.png"
45
+ plt.savefig(plot_filename)
46
+ plt.close()
47
+
48
+ if not os.path.exists(plot_filename):
49
+ raise FileNotFoundError("Error: Plot file not found after saving!")
50
+
51
+ return plot_filename, CS, PS