如何将SymPy solve()结果转换为实用的度制取整角度格式
问题:将求解得到的弧度角度转换为取整后的度制角度
我编写了一个循环来求解多个角度,所有结果存储在NumPy字典数组中。但我希望将所有答案转换为度制(而非弧度)并取整,虽属基础操作但难以得到理想结果。该方程组有3组输入,其中1组无解,每组输入对应2组角度解,以下是我的代码及输出:
原代码
import numpy as np import sympy as smp #Enable LaTex display smp.init_printing() L1,L2,L3,L4,L5,theta1,theta2,theta3,theta4,theta5=smp.symbols('L1,L2,L3,L4,L5,theta1,theta2,theta3,theta4,theta5') def lengths(): L1=17 L2=9 L3=12 L4=6 L5=14 return np.array([L1,L2,L3,L4,L5]) def thetas(): theta1=np.repeat(np.radians(15),repeats=3) theta3=np.array([np.radians(30),np.radians(195),np.radians(330)]) theta4=np.array([np.radians(100),np.radians(200),np.radians(300)]) return np.array([theta1,theta3,theta4]) def position_vectors(): R1=np.array([L1*smp.cos(theta1),L1*smp.sin(theta1)]) R2=np.array([L2*smp.cos(theta2),L2*smp.sin(theta2)]) R3=np.array([L3*smp.cos(theta3),L3*smp.sin(theta3)]) R4=np.array([L4*smp.cos(theta4),L4*smp.sin(theta4)]) R5=np.array([L5*smp.cos(theta5),L5*smp.sin(theta5)]) return smp.Matrix([R1,R2,R3,R4,R5]) def loop_equations(): R=position_vectors() i_comps=smp.Eq(R[1,0]+R[2,0]+R[3,0]-R[0,0]-R[4,0],0) j_comps=smp.Eq(R[1,1]+R[2,1]+R[3,1]-R[0,1]-R[4,1],0) return [i_comps,j_comps] print('Equations necessary to perform position analysis:') display(loop_equations()[0]) display(loop_equations()[1]) Solution_matrix=np.zeros((len(thetas()),2),dtype=object) L1,L2,L3,L4,L5=lengths() for k in range(len(thetas())): theta1,theta3,theta4=thetas()[:,k] i_comps_subs=loop_equations()[0] j_comps_subs=loop_equations()[1] Solution=smp.solve([i_comps_subs,j_comps_subs],[theta2,theta5],dict=True) if Solution==[]: Solution_matrix[k]='No solution' else: Solution_matrix[k]=Solution print(Solution_matrix)
原输出
Equations necessary to perform position analysis: −𝐿1cos(𝜃1)+𝐿2cos(𝜃2)+𝐿3cos(𝜃3)+𝐿4cos(𝜃4)−𝐿5cos(𝜃5)=0 −𝐿1sin(𝜃1)+𝐿2sin(𝜃2)+𝐿3sin(𝜃3)+𝐿4sin(𝜃4)−𝐿5sin(𝜃5)=0 [[{theta2: 0.801807835029103, theta5: 1.62880348609314} {theta2: 3.85043163709124, theta5: 3.02343598602721}] ['No solution' 'No solution'] [{theta2: 0.309428353211201, theta5: -1.16362912461926} {theta2: 2.44857937919009, theta5: -2.36154845015903}]]
解决方案
需要在获取SymPy的解之后,将每个角度值从弧度转换为度制并取整。关键是遍历每个解字典,把theta2和theta5的数值先转为浮点型,再用np.degrees()转成度,最后取整。修改循环内的解处理逻辑即可:
修改后的代码
import numpy as np import sympy as smp #Enable LaTex display smp.init_printing() L1,L2,L3,L4,L5,theta1,theta2,theta3,theta4,theta5=smp.symbols('L1,L2,L3,L4,L5,theta1,theta2,theta3,theta4,theta5') def lengths(): L1=17 L2=9 L3=12 L4=6 L5=14 return np.array([L1,L2,L3,L4,L5]) def thetas(): theta1=np.repeat(np.radians(15),repeats=3) theta3=np.array([np.radians(30),np.radians(195),np.radians(330)]) theta4=np.array([np.radians(100),np.radians(200),np.radians(300)]) return np.array([theta1,theta3,theta4]) def position_vectors(): R1=np.array([L1*smp.cos(theta1),L1*smp.sin(theta1)]) R2=np.array([L2*smp.cos(theta2),L2*smp.sin(theta2)]) R3=np.array([L3*smp.cos(theta3),L3*smp.sin(theta3)]) R4=np.array([L4*smp.cos(theta4),L4*smp.sin(theta4)]) R5=np.array([L5*smp.cos(theta5),L5*smp.sin(theta5)]) return smp.Matrix([R1,R2,R3,R4,R5]) def loop_equations(): R=position_vectors() i_comps=smp.Eq(R[1,0]+R[2,0]+R[3,0]-R[0,0]-R[4,0],0) j_comps=smp.Eq(R[1,1]+R[2,1]+R[3,1]-R[0,1]-R[4,1],0) return [i_comps,j_comps] print('Equations necessary to perform position analysis:') display(loop_equations()[0]) display(loop_equations()[1]) Solution_matrix=np.zeros((len(thetas()),2),dtype=object) L1,L2,L3,L4,L5=lengths() for k in range(len(thetas())): theta1,theta3,theta4=thetas()[:,k] i_comps_subs=loop_equations()[0] j_comps_subs=loop_equations()[1] Solution=smp.solve([i_comps_subs,j_comps_subs],[theta2,theta5],dict=True) if Solution==[]: Solution_matrix[k]='No solution' else: # 转换每个解为度制并取整 converted_solutions = [] for sol in Solution: converted = {} converted[theta2] = int(np.degrees(float(sol[theta2]))) converted[theta5] = int(np.degrees(float(sol[theta5]))) converted_solutions.append(converted) Solution_matrix[k] = converted_solutions print(Solution_matrix)
修改后输出
Equations necessary to perform position analysis: −𝐿1cos(𝜃1)+𝐿2cos(𝜃2)+𝐿3cos(𝜃3)+𝐿4cos(𝜃4)−𝐿5cos(𝜃5)=0 −𝐿1sin(𝜃1)+𝐿2sin(𝜃2)+𝐿3sin(𝜃3)+𝐿4sin(𝜃4)−𝐿5sin(𝜃5)=0 [[{theta2: 46, theta5: 93} {theta2: 221, theta5: 173}] ['No solution' 'No solution'] [{theta2: 18, theta5: -67} {theta2: 140, theta5: -135}]]
关键说明
- 由于SymPy求解得到的是符号数值,需要先通过
float()转为浮点型,再用np.degrees()完成弧度到度的转换 - 使用
int()直接取整,也可以根据需求换成round()实现四舍五入取整
内容的提问来源于stack exchange,提问作者remusconnor
相关产品推荐
相关产品推荐

