如何使用SymPy找到两个函数的交点? [英] How to use SymPy to find the point of intersection of two functions?
本文介绍了如何使用SymPy找到两个函数的交点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 SymPy 库来查找两个函数之间的交点:
I am trying to use the SymPy library to find the point of intersection(s) between two functions:
f(x) = e ^ (x/2)
和 g(x) = 3 - 3 * x
我试过了:
import sympy as syp
x = syp.symbols('x')
f_x = syp.E ** (x / 2)
g_x = 3 - 3 * x
print(syp.nsolve(f_x, g_x, x))
syp.nsolve(f_x, g_x, x)
吐出一个 TypeError
.用 syp.solve([f_x, g_x], x)
替换该行会产生一个空列表 []
.这是错误的,因为 f(x)
和 g(x)
恰好在一点相交.
syp.nsolve(f_x, g_x, x)
spits out a TypeError
. Replacing that line with syp.solve([f_x, g_x], x)
results in an empty list []
. This is wrong because f(x)
and g(x)
intersect at exactly one point.
如何使用 SymPy 获取任何 f(x) 和 g(x) 之间的交点的 x 和 y 值?
How do I get the x and y values of the point of intersection(s) between any f(x) and g(x) using SymPy?
推荐答案
import sympy as syp
x = syp.symbols('x')
f_x = syp.E ** (x / 2)
g_x = 3 - 3 * x
print(syp.solve(f_x - g_x, x))
以上代码的输出为:
The output of the above code is:
[-2*LambertW(exp(1/2)/6) + 1]
这篇关于如何使用SymPy找到两个函数的交点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文