SymPy 无法求解三角表达式,但 Matlab 做到了

如何解决SymPy 无法求解三角表达式,但 Matlab 做到了

我不明白为什么下面的表达式没有被简化。该示例显示了错误:

import pandas as pd
import numpy as np
from sympy import symbols
from sympy.solvers import solve
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import sympy as sym
from sympy import Matrix,simplify,trigsimp,fraction,lambdify,sin,cos 

X = 0
Y = -320
Z = 710     
        
RX = 0
RY = 90
RZ = 0 
        
s1,c1 = sym.symbols('sin(th1) cos(th1)')
s2,c2 = sym.symbols('sin(th2) cos(th2)')
s3,c3 = sym.symbols('sin(th3) cos(th3)')
s4,c4 = sym.symbols('sin(th4) cos(th4)')
s5,c5 = sym.symbols('sin(th5) cos(th5)')
s6,c6 = sym.symbols('sin(th6) cos(th6)')

#%%

matRotX = Matrix([[1,0],[0,np.cos(np.deg2rad(RX)),-np.sin(np.deg2rad(RX))],np.sin(np.deg2rad(RX)),np.cos(np.deg2rad(RX))]])
matRotY = Matrix([[np.cos(np.deg2rad(RY)),np.sin(np.deg2rad(RX))],1,[-np.sin(np.deg2rad(RX)),np.cos(np.deg2rad(RX))]])
matRotZ = Matrix([[np.cos(np.deg2rad(RY)),-np.sin(np.deg2rad(RX)),[np.sin(np.deg2rad(RX)),1]])

matRot = matRotZ * matRotY
matRot = matRot * matRotX

d1 = 352
a1 = 70
alfa1 = np.deg2rad(-90)

RotZ1 = Matrix([[c1,-s1,[s1,c1,1]])
Trans_d1 = Matrix([[1,d1],1]])
Tran_a1 = Matrix([[1,a1],1]])
RotX1 = Matrix([[1,np.cos(alfa1),-np.sin(alfa1),np.sin(alfa1),1]])

A01 = RotZ1 * Trans_d1 * Tran_a1 * RotX1
  
d2 = 0
a2 = 360
alfa2 = np.deg2rad(0)
beta2 = np.deg2rad(0)

RotZ2 = Matrix([[c2,-s2,[s2,c2,1]])
Trans_d2 = Matrix([[1,d2],1]])
Tran_a2 = Matrix([[1,a2],1]])
RotX2 = Matrix([[1,np.cos(alfa2),-np.sin(alfa2),np.sin(alfa2),1]])
RotY2 = Matrix([[np.cos(beta2),np.sin(beta2),[-np.sin(beta2),np.cos(beta2),1]])

A12 = RotZ2 * Tran_a2 * RotX2 * RotY2 #Hayati

   
d3 = 0
a3 = 0
alfa3 = np.deg2rad(-90)

RotZ3 = Matrix([[c3,-s3,[s3,c3,1]])
Trans_d3 = Matrix([[1,d3],1]])
Tran_a3 = Matrix([[1,a3],1]])
RotX3 = Matrix([[1,np.cos(alfa3),-np.sin(alfa3),np.sin(alfa3),1]])

A23 = RotZ3 * Trans_d3 * Tran_a3 * RotX3

#A4
d4 = 380;
a4 = 0;
alfa4 = np.deg2rad(90);

RotZ4 = Matrix([[c4,-s4,[s4,c4,1]])
Trans_d4 = Matrix([[1,d4],1]])
Tran_a4 = Matrix([[1,a4],1]])
RotX4 = Matrix([[1,np.cos(alfa4),-np.sin(alfa4),np.sin(alfa4),1]])

A34 = RotZ4 * Trans_d4 * Tran_a4 * RotX4

#A5
d5 = 0
a5 = 0
alfa5 = np.deg2rad(-90)

RotZ5 = Matrix([[c5,-s5,[s5,c5,1]])
Trans_d5 = Matrix([[1,d5],1]])
Tran_a5 = Matrix([[1,a5],1]])
RotX5 = Matrix([[1,np.cos(alfa5),-np.sin(alfa5),np.sin(alfa5),1]])

A45 = RotZ5 * Trans_d5 * Tran_a5 * RotX5

d6 = 65
a6 = 0
alfa6 = np.deg2rad(0)

RotZ6 = Matrix([[c6,-s6,[s6,c6,1]])
Trans_d6 = Matrix([[1,d6],1]])
Tran_a6 = Matrix([[1,a6],1]])
RotX6 = Matrix([[1,np.cos(alfa6),-np.sin(alfa6),np.sin(alfa6),1]])

A56 = RotZ6 * Trans_d6 * Tran_a6 * RotX6


T06 = Matrix(np.zeros((4,4)))

T06[0,3] = sym.symbols('px')
T06[1,3] = sym.symbols('py')
T06[2,3] = sym.symbols('pz')
T06[3,3] = 1
T06[0,0] = sym.symbols('nx')
T06[1,0] = sym.symbols('ny')
T06[2,0] = sym.symbols('nz')
T06[3,0] = 0
T06[0,1] = sym.symbols('ox')
T06[1,1] = sym.symbols('oy')
T06[2,1] = sym.symbols('oz')
T06[3,1] = 0
T06[0,2] = sym.symbols('ax')
T06[1,2] = sym.symbols('ay')
T06[2,2] = sym.symbols('az')
T06[3,2] = 0

Theta1rad = -1
  
A01 = A01.subs({'cos(th1)':np.cos(Theta1rad)})
A01 = A01.subs({'sin(th1)':np.sin(Theta1rad)})

T06 = T06.subs({'px':X})
T06 = T06.subs({'py':Y})
T06 = T06.subs({'pz':Z})
T06 = T06.subs({'ax':matRot[0,2]})
T06 = T06.subs({'ay':matRot[1,2]})
T06 = T06.subs({'az':matRot[2,2]})
T06 = T06.subs({'ox':matRot[0,1]})
T06 = T06.subs({'oy':matRot[1,1]})
T06 = T06.subs({'oz':matRot[2,1]})
T06 = T06.subs({'nx':matRot[0,0]})
T06 = T06.subs({'ny':matRot[1,0]})
T06 = T06.subs({'nz':matRot[2,0]})
  
Theta31rad = 0.57

A23 = A23.subs({'cos(th3)':np.cos(Theta31rad)})
A23 = A23.subs({'sin(th3)':np.sin(Theta31rad)})

Eq27 = A12.inv() * A01.inv() * T06 * A56.inv()
Eq27 = simplify(Eq27)

Eq27[0,3] 是之前使用 Symbolic 变量进行代数运算的结果,但simple() 或lambdify() 不起作用。但是,如果我手动复制并粘贴最终表达式,symPy 可以简化表达式,如下所示:

simplify(Eq27[0,3])
Out[123]: (-360.0*cos(th2)**2 + 199.270715138527*cos(th2) - 360.0*sin(th2)**2 - 293.0*sin(th2))/(cos(th2)**2 + sin(th2)**2)

手动复制/粘贴后:

a = (-360.0*cos(th2)**2 + 199.270715138527*cos(th2) - 360.0*sin(th2)**2 - 293.0*sin(th2))/(cos(th2)**2 + sin(th2)**2)

simplify(a)
Out[125]: -293.0*sin(th2) + 199.270715138527*cos(th2) - 360.0

这种行为有什么原因吗?

问候。

解决方法

这是您创建的表达式:

In [39]: a = simplify(Eq27[0,3])

In [40]: a
Out[40]: 
    ⎛                2                                             2                 ⎞
1.0⋅⎝- 360.0⋅cos(th2)  + 199.270715138527⋅cos(th2) - 360.0⋅sin(th2)  - 293.0⋅sin(th2)⎠
──────────────────────────────────────────────────────────────────────────────────────
                                        2           2                                 
                                cos(th2)  + sin(th2)  

我们不需要您的所有其余代码来生成此表达式。重现相同对象的简单方法是使用 srepr 例如:

In [41]: srepr(a)
Out[41]: "Mul(Float('1.0',precision=53),Pow(Add(Pow(Symbol('cos(th2)'),Integer(2)),Pow(Symbol('sin(th2)'),Integer(2))),Integer(-1)),Add(Mul(Integer(-1),Float('360.0',Pow(Symbol('cos(th2)'),Mul(Float('199.27071513852684',Symbol('cos(th2)')),Mul(Integer(-1),Float('293.0',Symbol('sin(th2)'))))"

In [42]: b = eval(srepr(a))

In [43]: b == a
Out[43]: True

仔细查看 srepr 的输出,我发现了问题所在。您的表达式看起来像 cos(th1) 这样的术语,但它们实际上是带有 name "cos(th1)" 的符号。在此处查看差异:

In [44]: c = Symbol('cos(th1)')

In [45]: d = cos(Symbol('th1'))

In [46]: c
Out[46]: cos(th1)

In [47]: d
Out[47]: cos(th₁)

In [48]: srepr(c)
Out[48]: "Symbol('cos(th1)')"

In [49]: srepr(d)
Out[49]: "cos(Symbol('th1'))"

Trig 标识仅在您具有触发函数但名称恰好看起来像触发函数的符号与实际触发函数不同的情况下才适用。当您复制粘贴 repr 时,您将获得实际的触发函数,这就是可以简化结果的原因。

所以问题出在代码的顶部:

s1,c1 = sym.symbols('sin(th1) cos(th1)')
s2,c2 = sym.symbols('sin(th2) cos(th2)')
s3,c3 = sym.symbols('sin(th3) cos(th3)')
s4,c4 = sym.symbols('sin(th4) cos(th4)')
s5,c5 = sym.symbols('sin(th5) cos(th5)')
s6,c6 = sym.symbols('sin(th6) cos(th6)')

应该是:

th1,th2,th3,th4,th5,th6 = symbols('th1:7')
s1,c1 = sin(th1),cos(th1)
s2,c2 = sin(th2),cos(th2)
s3,c3 = sin(th3),cos(th3)
s4,c4 = sin(th4),cos(th4)
s5,c5 = sin(th5),cos(th5)
s6,c6 = sin(th6),cos(th6)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res