2024/5/25

 

import numpy as np
import matplotlib.pyplot as plt

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置中文字体为SimHei
plt.rcParams['axes.unicode_minus'] = False    # 解决坐标轴负号'-'显示为方块的问题

# 定义分段函数
def piecewise_function(x):
    return np.sqrt(2*np.sqrt(x**2) - x**2) - 2.14*np.sqrt(np.sqrt(2) - np.sqrt(np.abs(x)))

# 生成x值在区间[-2, 2]
x = np.linspace(-2, 2, 400)
# 生成y值
y = piecewise_function(x)

# 创建图形
plt.figure(figsize=(8, 6))

# 绘制分段函数曲线
plt.plot(x, y, label='y = sqrt(2*sqrt(x^2) - x^2) - 2.14*sqrt(sqrt(2) - sqrt(|x|))', color='blue')

# 填充由曲线所包围的图形
plt.fill_between(x, y, color='skyblue', alpha=0.4)

# 添加标题和标签
plt.title('分段函数及其填充图形')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()

# 显示图形
plt.grid(True)
plt.show()

 

posted @ 2024-05-24 22:55  The-rich  阅读(18)  评论(0)    收藏  举报