Created
May 25, 2024 13:50
-
-
Save danielvarga/bdff0d9f2991e69f9241bd7a14ee2c90 to your computer and use it in GitHub Desktop.
some plots for Imre Bárány about Victor Grinberg's balancing game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://users.renyi.hu/~geza/GeoSzem/imre24 | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def F(a, g): | |
| numerator = np.sin(g)**2 * ((a + 1)**2 * np.tan(g)**2 + a**2) | |
| denominator = (a + 1)**2 - (a + 1) * np.sin(g) | |
| return numerator / denominator | |
| N = 1000 | |
| a = np.linspace(0, 10, N + 1) | |
| # g = np.linspace(0.7966, 0.7968, N) | |
| g = np.linspace(0.0, 0.85, N) | |
| A, G = np.meshgrid(a, g) | |
| # I don't really understand why .T is necessary, it's to make f_value = f[a_index, g_index] true. | |
| f = F(A, G).T | |
| print(f.shape) | |
| agg = np.mean(f >= 1, axis=0) | |
| solution = g[agg == 1].min() | |
| print(solution) | |
| a_values = [] | |
| f_values = [] | |
| for_every_g_argmin_of_f_over_a = np.argmin(f, axis=0) | |
| assert len(g) == len(for_every_g_argmin_of_f_over_a) | |
| for g_index, (g_value, a_index) in enumerate(zip(g, for_every_g_argmin_of_f_over_a)): | |
| a_value = a[a_index] | |
| f_value = f[a_index, g_index] | |
| a_values.append(a_value) | |
| f_values.append(f_value) | |
| assert np.allclose(np.array(a_values), a[for_every_g_argmin_of_f_over_a]) | |
| fig, ax1 = plt.subplots() | |
| plt.xlabel("$g$") | |
| ax1.plot(g, a_values, label="optimal $a$ for each $g$", c="r") | |
| ax1.set_ylabel("$a$") | |
| ax2 = ax1.twinx() | |
| ax2.plot(g, f_values, label="minimal $F(a, g)$ for each $g$", c="b") | |
| ax2.set_ylabel("$F(a, g)$") | |
| ax1.legend(loc="upper left") | |
| ax2.legend(loc="center left") | |
| # line showing where F'(g) = min_a F(a, g) goes above 1. | |
| ax2.plot([solution, solution], [min(f_values), max(f_values)], c="black") | |
| plt.show() | |
| N = 1000 | |
| a = np.linspace(0, 10, N + 1) | |
| g = np.linspace(0.6, 0.9, N) | |
| A, G = np.meshgrid(a, g) | |
| f = F(A, G) | |
| plt.rcParams['text.usetex'] = True | |
| plt.figure(figsize=(8, 6)) | |
| plt.pcolormesh(A, G, (f >= 1).astype(int), shading='auto', cmap='viridis') | |
| plt.xlabel("$a$", fontsize=20) | |
| plt.ylabel("$g$", fontsize=20) | |
| plt.title(r"Yellow means $F(a, g) \ge 1$.", fontsize=20) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment