import numpy as np
import matplotlib.pyplot as plt
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文
plt.rcParams['axes.unicode_minus'] = False # 正常显示负号
# 创建图形和子图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# 反函数示例
x = np.linspace(0, 5, 1000)
f = 2**x # 原函数 f(x) = 2^x
f_inv = np.log2(x) # 反函数 f^(-1)(x) = log_2(x)
ax1.plot(x, f, label='f(x) = 2^x')
ax1.plot(f, x, label='f^(-1)(x) = log_2(x)')
ax1.plot(x, x, 'k--', label='y = x')
ax1.set_title('反函数示例')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.legend()
ax1.grid(True)
# 复合函数示例
x = np.linspace(-2, 2, 1000)
f = x**2 # f(x) = x^2
g = np.sin(x) # g(x) = sin(x)
g_of_f = np.sin(x**2) # (g ∘ f)(x) = sin(x^2)
ax2.plot(x, f, label='f(x) = x^2')
ax2.plot(x, g, label='g(x) = sin(x)')
ax2.plot(x, g_of_f, label='g[f(x)] = sin(x^2)')
ax2.set_title('复合函数示例')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.legend()
ax2.grid(True)
plt.tight_layout()
plt.show()