from functools import lru_cache
import numpy as np
import matplotlib.pyplot as plt
a_VALUES = sorted(
["-1 * 10^{}".format(i) for i in range(0, 7)] +
["-5 * 10^{}".format(i) for i in range(0, 7)] +
["-2 * 10^{}".format(i) for i in range(0, 7)], key=lambda t: sage_eval(t))
RRR = RealField(Integer(512))
COLOR1 = "tab:blue"
COLOR2 = "tab:red"
@interact
def draw_plot(a_value = slider(a_VALUES, default = "-1 * 10^0", label="a")):
x0_val = CONSTANT_VALUES[x0]
a_value = sage_eval(a_value)
q_espression= x^a_value * exp(a_value * x0_val / x)
q_espression_n = q_espression/q_espression.substitute(x=x0_val)
q_espression_n_compiled = fast_callable(q_espression_n, vars=[x], domain=RRR)
r = q_espression_n / x^2
r_espression_n_compiled = fast_callable(r, vars=[x], domain=RRR)
r_espression_norm_int1, _ = numerical_integral(r_espression_n_compiled, a=0, b=10, algorithm="qag", rule=6)
r_espression_norm_int2, _ = numerical_integral(r_espression_n_compiled, a=10, b=+oo, algorithm="qag", rule=6)
r_espression_norm = r_espression_norm_int1 + r_espression_norm_int2
r_final = lambda t: r_espression_n_compiled(t) / r_espression_norm
p1_plot = plot(
q_espression_n_compiled,
xmin=0.01,
xmax=20,
color="blue"
)
p2_plot = plot(
r_final,
xmin=0.01,
xmax=20,
color="red"
)
p1_x_points = np.array(p1_plot._objects[0].xdata, dtype=np.float64)
p1_y_points = np.array(p1_plot._objects[0].ydata, dtype=np.float64)
p2_x_points = np.array(p2_plot._objects[0].xdata, dtype=np.float64)
p2_y_points = np.array(p2_plot._objects[0].ydata, dtype=np.float64)
plt.xlabel("$x$", fontsize=14)
plt.title("Probabilty distribution at equilibrium")
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['bottom'].set_position('zero')
plt.ylabel("$p$", fontsize=14, color=COLOR1)
plt.plot(p2_x_points, p2_y_points, color=COLOR1, linewidth=2.5)
plt.fill_between(p2_x_points, p2_y_points, color=COLOR1, alpha=0.1)
plt.gca().tick_params(axis='y', colors=COLOR1)
ax2=plt.gca().twinx()
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)
plt.gca().spines['right'].set_color(COLOR2)
plt.gca().spines['left'].set_color(COLOR1)
plt.gca().tick_params(axis='y', colors=COLOR2)
plt.ylabel("$q$", fontsize=14, color=COLOR2)
plt.plot(p1_x_points, p1_y_points, color=COLOR2, linewidth=1.5)
plt.gca().set_xlim(left=0)
v_line_bottom = (x0_val, 0)
v_line_bottom_display = plt.gca().transData.transform(v_line_bottom)
v_line_bottom_axis = plt.gca().transAxes.inverted().transform(v_line_bottom_display)
ymin_line = v_line_bottom_axis[1]
plt.axvline(x = x0_val, ymin=ymin_line, color = 'tab:gray', label = '$x_0$', linestyle='--', linewidth=1)