猫史档案馆


【Python作品分享】计算曲线质心的位置【作品秀】

用户:今天会笑今天会笑查看:0 回复:0 评论:0 创建时间:2023-07-15T22:43:31


【作品展示】

center_image

 

【作品介绍】

计算曲线质心的位置

 

【作品源代码】

import numpy as np
import matplotlib.pyplot as plt

# 定义曲线函数


def f(x):
    # 曲线函数,例如 y = x^2
    return x**3

# 计算曲线质心


def compute_centroid(x, y):
    numerator = np.trapz(y * x, x)  # 曲线下方的面积
    denominator = np.trapz(y, x)   # 曲线的总面积
    centroid = numerator / denominator
    return centroid


# 生成 x 值的数组
x = np.linspace(0, 10, 100)
# 计算对应的 y 值
y = f(x)

# 绘制曲线
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Curve')
plt.grid(True)
plt.show()

# 计算曲线质心
centroid = compute_centroid(x, y)
print("曲线质心的 x 坐标:", centroid)
print("曲线质心的 y 坐标:", f(centroid))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页