from sympy import symbols, Eq, solve
import math
L1, L2, L3, L4 = 26.5, 111.6, 67.5, 87.5 # 杆长
angle1 = []
angle2 = []
angle3 = []
# 角位移
for i in range(120): # 每3度求解存储数据
angle1.append(i * 3)
for constant in angle1:
# 解非线性方程,求解角位移
constant_c = L4 - L1 * math.cos(math.radians(constant))
constant_s = -L1 * math.sin(math.radians(constant))
w, x, y, z = symbols('w x y z')
eqs = [
Eq(w, constant_c),
Eq(x, constant_s),
Eq(y, -L2 * math.cos(z)),
Eq(z, -L2 * math.sin(z))
]
displacement_equation = solve(eqs, (w, x, y, z))
displacement_values = displacement_equation[0] # 获取解的值
# 判断符合实际的解
if displacement_values[x] >= 0:
angle2.append(math.acos(displacement_values[y]))
angle3.append(math.acos(displacement_values[z]))
else:
angle2.append(math.pi - math.acos(displacement_values[y]))
angle3.append(math.pi - math.acos(displacement_values[z]))
# 角速度
speed_angle1 = 1 # rad/s
speed_angle2 = []
speed_angle3 = []
for i in range(len(angle1)):
constant2_s = L2 * math.sin(angle2[i])
constant3_s = L3 * math.sin(angle3[i])
constant1_s = L1 * math.sin(math.radians(angle1[i])) * speed_angle1
constant2_c = L2 * math.cos(angle2[i])
constant3_c = L3 * math.cos(angle3[i])
constant1_c = L1 * math.cos(math.radians(angle1[i])) * speed_angle1
x, y = symbols('x y')
eqs = [
Eq(x, constant2_s - constant3_s + constant1_s),
Eq(y, constant2_c - constant3_c + constant1_c)
]
speed_equation = solve(eqs, (x, y))
speed_angle2.append(speed_equation[x])
speed_angle3.append(speed_equation[y])
# 角加速度
acceleration_angle1 = 0
acceleration_angle2 = []
acceleration_angle3 = []
for i in range(len(angle1)):
constant2_s = L2 * math.sin(angle2[i])
constant3_s = L3 * math.sin(angle3[i])
constant1_s = L1 * math.sin(math.radians(angle1[i]))
constant2_c = L2 * math.cos(angle2[i])
constant3_c = L3 * math.cos(angle3[i])
constant1_c = L1 * math.cos(math.radians(angle1[i]))
constant_s = constant2_s * pow(speed_angle2[i], 2) - constant3_s * pow(speed_angle3[i], 2) + constant1_s
constant_c = constant2_c * pow(speed_angle2[i], 2) - constant3_c * pow(speed_angle3[i], 2) + constant1_c
x, y = symbols('x y')
eqs = [
Eq(x, constant_s),
Eq(y, constant_c)
]
acceleration_equation = solve(eqs, (x, y))
acceleration_angle2.append(acceleration_equation[x])
acceleration_angle3.append(acceleration_equation[y])
# 数据输出
print("{:>4},{:>10},{:>6},{:>6},{:>9},{:>7},{:>6}".format(
"角",
"角2",
"角2速度",
"角2加速度",
"角3",
"角3速度",
"角3加速度"
))
for i in range(len(angle1)):
print("{:>5},{:>10.2f},{:>10.2f},{:>10.2f},{:>10.2f},{:>10.2f},{:>10.2f}".format(
angle1[i],
math.degrees(angle2[i]),
math.degrees(speed_angle2[i]),
math.degrees(acceleration_angle2[i]),
math.degrees(angle3[i]),
math.degrees(speed_angle3[i]),
math.degrees(acceleration_angle3[i])
))