在numpy中使用python数值求解器求解方程 [英] Solve an equation using a python numerical solver in numpy

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

问题描述

我有一个等式,如下:

R - ((1.0 - np.exp(-tau))/(1.0 - np.exp(-a*tau))) = 0.

我想使用numpy中可用的数值求解器来求解此方程式中的tau.最好的方法是什么?

I want to solve for tau in this equation using a numerical solver available within numpy. What is the best way to go about this?

此公式中Ra的值因该公式的不同实现而异,但在要求解tau时将其固定为特定值.

The values for R and a in this equation vary for different implementations of this formula, but are fixed at particular values when it is to be solved for tau.

推荐答案

在传统的数学符号中,您的方程是

In conventional mathematical notation, your equation is

SciPy fsolve函数搜索给定表达式等于零(表达式的零"或根")的点.您需要为fsolve提供您的所需解决方案"附近的初始猜测.找到这种初始猜测的一种好方法是只绘制表达式并寻找零交叉.

The SciPy fsolve function searches for a point at which a given expression equals zero (a "zero" or "root" of the expression). You'll need to provide fsolve with an initial guess that's "near" your desired solution. A good way to find such an initial guess is to just plot the expression and look for the zero crossing.

#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve

# Define the expression whose roots we want to find

a = 0.5
R = 1.6

func = lambda tau : R - ((1.0 - np.exp(-tau))/(1.0 - np.exp(-a*tau))) 

# Plot it

tau = np.linspace(-0.5, 1.5, 201)

plt.plot(tau, func(tau))
plt.xlabel("tau")
plt.ylabel("expression value")
plt.grid()
plt.show()

# Use the numerical solver to find the roots

tau_initial_guess = 0.5
tau_solution = fsolve(func, tau_initial_guess)

print "The solution is tau = %f" % tau_solution
print "at which the value of the expression is %f" % func(tau_solution)

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

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