简体   繁体   English

为什么 SymPy 没有为我的方程找到任何解?

[英]Why does SymPy not find any solutions for my equation?

I want to calculate roots of my graphs I plotted (through matplotlib).我想计算我绘制的图的根(通过 matplotlib)。

I tried with SciPy but it needs a rough estimate of the coordinates of the roots which is too impractical.我尝试使用 SciPy 但它需要粗略估计根的坐标,这太不切实际了。 Now I'm trying it with SymPy but it can't find a solution and prints empty brackets.现在我正在尝试使用 SymPy,但它找不到解决方案并打印空括号。 Am I doing something wrong?难道我做错了什么?

Here's my code:这是我的代码:

import matplotlib as plt
import numpy as np
from sympy import *

h = 1.8
alpha = 0
g = 9.81
K = 0.0004
v_0 = 360


x = np.linspace(0, 10000, 500)
f_x = h + x * np.tan(alpha) - (g / (2 * (K ** 2) * (v_0 ** 2))) * ((np.e ** (K * x) - 1) ** 2)

*bunch of labels and titles*

x = Symbols("x")
roots = solve(f_x, x)
print(roots)

The variables of the formula are all defined except x of course.公式的变量当然除了 x 之外都是定义好的。

Is there any way to make it work like Geogebra, where you can calculate roots, maxima, etc. of a graph or is Python not fitted for that kind of stuff?有什么方法可以让它像 Geogebra 一样工作,您可以在其中计算图的根、最大值等,还是 Python 不适合这种东西?

If you investigate your equation symbolically you will find that you can easily solve for the two roots of this equation for the values you have chosen.如果你象征性地研究你的方程,你会发现你可以很容易地为你选择的值求解这个方程的两个根。 The value of alpha being 0 makes the equation look like this: 1.8 - 236.55*(exp(0.0004*x) - 1)**2 . alpha 的值为 0 使得等式如下所示: 1.8 - 236.55*(exp(0.0004*x) - 1)**2

>>> from sympy import exp, tan
>>> from sympy.abc import x, a
>>> h = 1.8
>>> alpha = a
>>> g = 9.81
>>> K = 0.0004
>>> v_0 = 360
>>> f_x = h+x*tan(alpha)-(g/(2*(K**2)*(v_0**2)))*((exp(K*x)-1)**2)
>>> solve(f_x.subs(a, alpha), x)

This is a very important starting point when working with nonlinear equations, because if you can find the solutions at some point you can often find the solution at a nearby point.在处理非线性方程时,这是一个非常重要的起点,因为如果您可以在某个点找到解,您通常可以在附近的点找到解。 In your case, a nearby point would be alpha slightly positive or negative.在您的情况下,附近的点将是略为正或负的 alpha。 And such an equation for arbitrary alpha can only be solved as an implicit function (ie numerically).而这样的任意 alpha 方程只能作为隐式 function 求解(即数值上)。 But since you have a previously known value you can use that as your initial guess for the solver.但由于您有一个先前已知的值,您可以将用作求解器的初始猜测。

But how easy it is for the solver to use the guess to find the solution depends on the shape of the function near the solution.但是求解器使用猜测找到解的难易程度取决于解附近的 function 的形状。 Your equation has an undesirable maximum near the solution.您的方程在解附近有一个不需要的最大值。 It looks like your equation was academically constructed to have this property.看起来您的方程式在学术上是为了具有此属性而构建的。 If you rewrite your equation as two equations (one for each branch)如果您将方程式重写为两个方程式(每个分支一个)

>>> branches = [exp(K*x) - s for s in solve(f_x, exp(K*x))]

And then solve each of those branches at a non-zero value of alpha it will be much better behaved: eg nsolve(branch[0].subs(a, new alpha value), value from when alpha was 0) .然后在 alpha 的非零值处解决每个分支,它的表现会更好:例如nsolve(branch[0].subs(a, new alpha value), value from when alpha was 0)

That's the main idea.这是主要思想。 Recap:回顾:

  1. don't use numpy when looking for symbolic answers寻找符号答案时不要使用 numpy
  2. when working with nonlinear equations see if you can make some x-related term be zero (alpha=0 in your case) so you can solve the rest of the equation for x;在处理非线性方程时,看看你是否可以让一些与 x 相关的项为零(在你的情况下为 alpha=0),这样你就可以求解 x 方程的 rest; use that value when allowing the zero term to have a nonzero value (a non-zero alpha)当允许零项具有非零值(非零 alpha)时使用该值
  3. given f(x) + g(x) = 0 that is ill-behaved near root, try work with x = f^-1(g(x))给定 f(x) + g(x) = 0 在根附近表现不佳,尝试使用x = f^-1(g(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM