5.16
python分段函数
• 所花时间:1
• 代码行数:367
• 博客容量:1
• 代码如下:
import numpy as np
import matplotlib.pyplot as plt
# 定义 x 的取值范围
x = np.arange(-2, 2, 0.0001)
# 定义 y1 和 y2 函数
y1 = np.sqrt(2 * np.sqrt(x ** 2) - x ** 2)
y2 = (-2.14) * np.sqrt(np.sqrt(2) - np.sqrt(np.abs(x)))
# 创建图形并设置大小
plt.figure(figsize=(10, 6))
# 绘制 y1 和 y2 的曲线
plt.plot(x, y1, label=r'$y_1 = \sqrt{2 \sqrt{x^2} - x^2}$', color='blue')
plt.plot(x, y2, label=r'$y_2 = -2.14 \sqrt{\sqrt{2} - \sqrt{|x|}}$', color='green')
# 填充两个曲线之间的区域
plt.fill_between(x, y1, y2, where=(y1 >= y2), color='lightblue', alpha=0.5)
# 设置图形标签和标题
plt.xlabel('x')
plt.ylabel('y')
plt.title('Piecewise Functions and Filled Area')
plt.legend()
plt.grid(True)
# 显示图形
plt.show()
浙公网安备 33010602011771号