題目
連續投擲一個公正骰子兩次,社出現的點數一句為a,b,在座標平面上,將直線向右平移a單位,再向上平移b單位後獲得直線L’,則直線L’與圓交點個數的期望值是多少?
動畫演示
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.animation import FuncAnimationcircle_radius = 5slope = -3 / 4a_values = np.arange(1, 8)b_values = np.arange(1, 8)fig, ax = plt.subplots(figsize=(8, 10))ax.set_xlim(-10, 10)ax.set_ylim(-15, 10)ax.set_aspect('equal', adjustable='box')theta = np.linspace(0, 2 * np.pi, 100)x_circle = circle_radius * np.cos(theta)y_circle = circle_radius * np.sin(theta)ax.plot(x_circle, y_circle, color="blue", label="Circle C: $x^2 + y^2 = 25$")ax.grid(True, linestyle='--', alpha=0.7)line, = ax.plot([], [], 'r', lw=1.5, alpha=0.7)dice_a_text = ax.text(-7, 8, '', fontsize=15, ha='center')dice_b_text = ax.text(7, 8, '', fontsize=15, ha='center')
def init(): line.set_data([], []) dice_a_text.set_text('') dice_b_text.set_text('') return line, dice_a_text, dice_b_text
def uppubDate(frame): a, b = frame intercept = (3 * a + 4 * b) / 4 x_vals = np.linspace(-10, 10, 100) y_vals = slope * x_vals + intercept line.set_data(x_vals, y_vals) dice_a_text.set_text(f'dice A: {a}') dice_b_text.set_text(f'dice B: {b}') return line, dice_a_text, dice_b_text
frames = [(a, b) for a in a_values for b in b_values]ani = FuncAnimation(fig, uppubDate, frames=frames, init_func=init, blit=True, interval=300)plt.xlabel("x")plt.ylabel("y")plt.legend()plt.show()執行結果(擷取)

Thanks for reading!