【python】【材料力学】计算任意界面的质心坐标及惯性矩

背景

etabs自定义截面

公式

This code uses the following formulas:

Area of the polygon:

A = 1 2 ∑ i = 1 n ( x i y i + 1 − x i + 1 y i ) A = \frac{1}{2} \sum_{i=1}^{n} (x_i y_{i+1} - x_{i+1} y_i) A=21i=1n(xiyi+1xi+1yi)

Centroid coordinates:

C x = 1 6 A ∑ i = 1 n ( x i + x i + 1 ) ( x i y i + 1 − x i + 1 y i ) C_x = \frac{1}{6A} \sum_{i=1}^{n} (x_i + x_{i+1})(x_i y_{i+1} - x_{i+1} y_i) Cx=6A1i=1n(xi+xi+1)(xiyi+1xi+1yi)

C y = 1 6 A ∑ i = 1 n ( y i + y i + 1 ) ( x i y i + 1 − x i + 1 y i ) C_y = \frac{1}{6A} \sum_{i=1}^{n} (y_i + y_{i+1})(x_i y_{i+1} - x_{i+1} y_i) Cy=6A1i=1n(yi+yi+1)(xiyi+1xi+1yi)

Moment of inertia:

I x = 1 12 ∑ i = 0 n − 1 ( y i 2 + y i y i + 1 + y i + 1 2 ) ( x i y i + 1 − x i + 1 y i ) Ix = \frac{1}{12}\sum{i=0}^{n-1} (yi^2 + yiy{i+1} + y{i+1}^2)(xi y{i+1} - x{i+1} yi) Ix=121i=0n1(yi2+yiyi+1+yi+12)(xiyi+1xi+1yi)
I y = 1 12 ∑ i = 0 n − 1 ( x i 2 + x i x i + 1 + x i + 1 2 ) ( x i y i + 1 − x i + 1 y i ) Iy = \frac{1}{12}\sum{i=0}^{n-1} (xi^2 + xi x{i+1} + x{i+1}^2)(xi y{i+1} - x{i+1} yi) Iy=121i=0n1(xi2+xixi+1+xi+12)(xiyi+1xi+1yi)
I x y = 1 24 ∑ i = 0 n − 1 ( 2 x i y i + x i + 1 y i + 2 x i + 1 y i + 1 + x i y i + 1 ) ( x i y i + 1 − x i + 1 y i ) I{xy} = \frac{1}{24}\sum{i=0}^{n-1} (2xi yi + x{i+1} yi + 2x{i+1} y{i+1} + xi y{i+1})(xi y{i+1} - x{i+1} yi) Ixy=241i=0n1(2xiyi+xi+1yi+2xi+1yi+1+xiyi+1)(xiyi+1xi+1yi)
其中, n n n 是多边形的顶点数, ( x i , y i ) (xi,yi) (xi,yi) 表示多边形中第 i i i 个顶点的坐标, ( x i + 1 , y i + 1 ) (x{i+1},y{i+1}) (xi+1,yi+1) 表示多边形中第 i + 1 i+1 i+1 个顶点的坐标, I x Ix Ix 代表多边形相对于 x x x 轴的惯性矩, I y Iy Iy 代表多边形相对于 y y y 轴的惯性矩, I x y I_{xy} Ixy 代表多边形相对于 x x x y y y 轴的二次惯性矩。

代码


import matplotlib.pyplot as plt

def polygon_area(vertices):
    n = len(vertices)
    area = 0
    for i in range(n):
        area += (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
    return 0.5 * abs(area)

def centroid(vertices):
    n = len(vertices)
    cx, cy = 0, 0
    area = polygon_area(vertices)
    for i in range(n):
        factor = (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
        cx += (vertices[i][0] + vertices[(i + 1) % n][0]) * factor
        cy += (vertices[i][1] + vertices[(i + 1) % n][1]) * factor
    return (1 / (6 * area)) * cx, (1 / (6 * area)) * cy

def moment_of_inertia(vertices):
    n = len(vertices)
    Ix, Iy, Ixy = 0, 0, 0
    area = polygon_area(vertices)
    for i in range(n):
        factor = (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
        Ix += (vertices[i][1]**2 + vertices[i][1]*vertices[(i + 1) % n][1] + vertices[(i + 1) % n][1]**2) * factor
        Iy += (vertices[i][0]**2 + vertices[i][0]*vertices[(i + 1) % n][0] + vertices[(i + 1) % n][0]**2) * factor
        Ixy += (vertices[i][0]*vertices[(i + 1) % n][1] + 2*vertices[i][0]*vertices[i][1] + 2*vertices[(i + 1) % n][0]*vertices[(i + 1) % n][1] + vertices[(i + 1) % n][0]*vertices[i][1]) * factor
    return (1 / 12) * abs(Ix), (1 / 12) * abs(Iy), (1 / 24) * abs(Ixy)

def plot_polygon(vertices, centroid):
    polygon = plt.Polygon(vertices, fill=None, edgecolor='r', linewidth=2)
    plt.gca().add_patch(polygon)
    plt.scatter(*centroid, color='blue', marker='o', s=100, label='Centroid')
    plt.axis('equal')
    plt.legend()
    plt.show()

# 可增加,从cad中读取
vertices = [(0, 0), (30, 0), (30, 30), (0, 30), (-20, 20)]

area = polygon_area(vertices)
centroid_coordinates = centroid(vertices)
Ix, Iy, Ixy = moment_of_inertia(vertices)

print("Area:", area)
print("Centroid:", centroid_coordinates)
print("Moment of Inertia: Ix =", Ix, "Iy =", Iy, "Ixy =", Ixy)

plot_polygon(vertices, centroid_coordinates)

总结

常用的markdown编辑器可以渲染latex格式代码
常用公式可以编程

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值