使用python求解非线性方程 [英] using python to solve a nonlinear equation

查看:77
本文介绍了使用python求解非线性方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未使用过 python,但 Mathematica 无法处理我试图求解的方程.我正在尝试求解以下方程的变量a",其中 s、c、mu 和 delta t 是已知参数.

I have never used python but Mathematica can't handle the equation I am trying to solve. I am trying to solve for the variable "a" of the following equations where s, c, mu, and delta t are known parameters.

我尝试在 Mathematica 中执行 NSolve、Solve 等,但它已经运行了一个小时但没有运气.由于我对 Python 不熟悉,有没有办法可以使用 Python 来求解 a 的这个方程?

I tried doing NSolve, Solve, etc in Mathematica but it has been running for an hour with no luck. Since I am not familiar with Python, is there a way I can use Python to solve this equation for a?

推荐答案

您不会找到这些方程的解析解,因为它们是超越的,在其内部和外部都包含 a三角函数.

You're not going to find an analytic solution to these equations because they're transcendental, containing a both inside and outside of a trigonometric function.

我认为您在使用数值解时遇到的问题是 a 的可接受值范围受到 arcsin 的限制.由于 arcsin 仅针对 -1 和 1 之间的参数定义(假设您希望 a 为实数),alpha 的公式>beta 要求 a >s/2a >(s-c)/2.

I think the trouble you're having with numerical solutions is that the range of acceptable values for a is constrained by the arcsin. Since arcsin is only defined for arguments between -1 and 1 (assuming you want a to be real), your formulas for alpha and beta require that a > s/2 and a > (s-c)/2.

在 Python 中,您可以使用 brentq 函数找到第三个方程(以 f(a) = 0 形式重写)的零:

In Python, you can find a zero of your third equation (rewritten in the form f(a) = 0) using the brentq function:

import numpy as np
from scipy.optimize import brentq

s = 10014.6
c = 6339.06
mu = 398600.0
dt = 780.0
def f(a):
    alpha = 2*np.arcsin(np.sqrt(s/(2*a)))
    beta = 2*np.arcsin(np.sqrt((s-c)/(2*a)))
    return alpha - beta - (np.sin(alpha)-np.sin(beta)) - np.sqrt(mu/a**3)*dt

a0 = max(s/2, (s-c)/2)
a = brentq(f, a0, 10*a0)

为了阐明 brentq(f,a,b) 的工作方式,它在区间 [a,b] 上搜索 f 的零.在这里,我们知道a 至少是max(s/2, (s-c)/2).我只是猜测 10 次这是一个合理的上限,并且适用于给定的参数.更一般地说,您需要确保 fab 之间更改符号.您可以在 SciPy 文档中阅读有关该函数如何工作的更多信息.

To clarify the way brentq(f,a,b) works is that it searches for a zero of f on an interval [a,b]. Here, we know that a is at least max(s/2, (s-c)/2). I just guessed that 10 times that was a plausible upper bound, and that worked for the given parameters. More generally, you need to make sure that f changes sign between a and b. You can read more about how the function works in the SciPy docs.

这篇关于使用python求解非线性方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆