简体   繁体   English

使用Python解决复杂的多变量函数

[英]Solving a complicated multivariable function with Python

I have a very complicated function of two variables, let's call them x and y . 我有两个变量的非常复杂的函数,我们称它们为xy I want to create a Python program where the user can input two values, a and b , where a is the value of that complicated function of x and y , and b = math.atan(y/x) . 我想创建一个Python程序,用户可以在其中输入两个值ab ,其中axy的那个复杂函数的值,并且b = math.atan(y/x) This program should then output the values of x and y . 然后,该程序应输出xy的值。

I am clueless as to where to start. 我不知道从哪里开始。 I have tried to make the function into that of just one variable, then generate many random values for x and pick the closest one, but I have learnt that this is horribly inefficient and produces a result which is only accurate to about 2 significant figures, which is pretty horrible. 我尝试将函数变成一个变量,然后为x生成许多​​随机值并选择最接近的一个,但是我了解到这效率极低,并且只能精确到约2个有效数字,这是非常可怕的。 Is there a better way to do this? 有一个更好的方法吗? Many thanks! 非常感谢!

(PS I did not reveal the function here due to copyright issues. For the sake of example, you can consider the function (由于版权问题,我没有在此处显示该功能。为便于举例,您可以考虑使用该功能

a = 4*math.atan(math.sqrt(math.tan(x)*math.tan(y)/math.tan(x+y)))

where y = x * math.tan(b) .) 其中y = x * math.tan(b) 。)

Edit: After using the approach of the sympy library, it appears as though the program ignores my second equation (the complicated one). 编辑:使用sympy库的方法后,似乎该程序忽略了我的第二个方程式(复杂的方程式)。 I suspect it is too complicated for sympy to handle. 我怀疑它对于sympy来说太复杂了。 Thus, I am asking for another approach which does not utilise sympy . 因此,我要求另一种不使用sympy

You could use sympy and import the trigonometric functions from sympy. 您可以使用sympy并从sympy导入三角函数。

from sympy.core.symbol import symbols
from sympy.solvers.solveset import nonlinsolve
from sympy import sqrt, tan, atan

y = symbols('y', real=True)
a,b = 4,5 # user-given values


eq2 = a - 4*atan(sqrt(tan(y/tan(b))*tan(y)/tan((y/tan(b))+y)))

S = nonlinsolve( [eq2], [y] )
print(S)

It'll return you a series of conditions ( ConditionSet object ) for possible adequate results. 它会为您返回一系列条件(ConditionSet对象),以获得可能的适当结果。

If that wasn't clear enough, you can read the docs for nonlinsolve . 如果还不够清楚,可以阅读nonlinsolve的文档

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

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