【问题标题】:Find min and max angle of an ellipse in Python在Python中查找椭圆的最小和最大角度
【发布时间】:2021-04-12 07:00:25
【问题描述】:

我有一个方程,描述一个椭圆。我想使用 Python 来找到这些椭圆的最小和最大角度 theta 和轴 Lc 的长度,以便进一步计算。 参数 Lc1theta1 等是各个角度的轴长度。 我尝试sympy.solve 没有成功(结果没有任何意义),现在正在尝试sympy.solveset

这是我当前的脚本。接下来是需要theta的进一步计算。

import math
import sympy as sym
from sympy import solve, Eq
from sympy import *
import matplotlib.pyplot as plt

#define parameters 
#Lc modelled with matlab before
Lc1 = 0.67 / 1000
Lc2 = 0.36/ 1000
Lc3 = 0.7 / 1000

#orientation of the cut section
theta1 = 89  
theta2 = -35 
theta3 = 25 


#calculation from Beaudoin et al. (2016) and Ebner et al. (2010)

dL = ((Lc2 - Lc1)/(Lc3 - Lc1))

c = math.atan(- (((dL * (math.sin(2 * theta3) - math.sin(2 * theta1))) - (math.sin(2 * theta2) - math.sin(2* theta1)))/ ((dL * (math.cos(2 * theta3)  - math.cos(2 * theta1))) - (math.cos(2 * theta2) - math.cos(2 * theta1)))))

b = ((Lc2 - Lc1)/((math.sin(2 * theta2 + c)) - (math.sin(2 * theta1 + c))))

a = Lc1 - (b * math.sin(2 * theta1 + c))

print('Done calculating a, b and c')


##try to find theta at min and max Lc
#theta = sym.symbols('theta')
theta = var('theta')

#define eqation that give us our crossover length
Lc = Eq(a + b * sym.sin(2 * theta +c))

dLc = Eq(2 * b * sym.cos(2 * theta +c) + sym.sin(2 * theta + c))


print('Busy finding minimum and maximum.')
sol = solveset(dLc, theta)
sol

#now we have the derivation we can go find min and max of Lc


使用solveset 到目前为止我没有收到任何结果,因为 python 不会停止运行。 我不确定是否可以使用当前脚本获得可靠的结果。怎么了?有没有更有效的方法?如果有人可以帮助我,我会很高兴! 提前谢谢!

【问题讨论】:

    标签: python max sympy ellipse


    【解决方案1】:

    由于这个方程没有符号参数并且有浮点系数,我猜你只是想要一个数值解,所以你可以使用nsolve

    In [4]: nsolve(dLc, theta, 1.2)
    Out[4]: 1.22165851958244
    

    最初的猜测 (1.2) 来自查看函数图 (plot(dLc.lhs))。

    请注意,您的方程式如下所示:

    In [26]: dLc
    Out[26]: sin(2⋅θ + 0.697151824925716) + 0.0011237899722686⋅cos(2⋅θ + 0.697151824925716) = 0
    

    我们可以用任意符号而不是特定数字来解决这个问题:

    In [27]: a, b = symbols('a, b', real=True)
    
    In [28]: eq = sin(2*theta + a) + b*cos(2*theta + a)
    
    In [29]: solve(eq, theta)
    Out[29]: 
    ⎡          ⎛   ________    ⎞            ⎛   ________    ⎞⎤
    ⎢          ⎜  ╱  2         ⎟            ⎜  ╱  2         ⎟⎥
    ⎢  a       ⎜╲╱  b  + 1  - 1⎟    a       ⎜╲╱  b  + 1  + 1⎟⎥
    ⎢- ─ - atan⎜───────────────⎟, - ─ + atan⎜───────────────⎟⎥
    ⎣  2       ⎝       b       ⎠    2       ⎝       b       ⎠⎦
    

    这给出了两种解决方案,一种是消极的,一种是积极的。其他解决方案来自添加多个pi

    【讨论】:

    • 您好 Oscar,非常感谢您的回答!您答案的第二部分产生了我的预期。唯一的事情:在将 a 和 b 更改为符号之前,我将它们复制为 aa=abb=b。这样我以后可以将它们作为数字重用。 - 我在上面的代码中更改了这一点。
    • 哦,...我承认,我遇到的同样的问题 solveoccured:最小值不是最小值 - 它的 >Lc2,不应该是这种情况。也许它只是一个局部的,而不是一个全局的最小值?
    猜你喜欢
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2021-01-13
    • 2018-09-10
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多