简体   繁体   English

如何解决python中的表达式,例如f1(x) = f2(x)?

[英]How do I solve expressions in python such as f1(x) = f2(x)?

import math
import pylab
import sympy


def f1(x):
   """function representing a cosine variant function and returned"""
   return math.cos(2 * math.pi * x) * math.exp(-x ** 2)


def f2(x):
    """function representing a log variant function and returned"""
    return math.log(x + 2.2)


def positive_places(f, xs):
    """return a list of elements of xs that are positive when operated in by
f"""
    list1 = []
    for i in xs:
        if f(i) > 0:
            list1.append(i)
    return list1


def create_plot_data(f, xmin, xmax, n):
    """returns a tuple (xs, ys) where xs and ys are two sequences,
    each containing n numbers"""
    xs = [xmin + i * ((xmax - xmin) / (n - 1)) for i in range(n)]
    ys = [f(xs[i]) for i in range(n)]
    return (xs, ys)


def myplot():
    """plots a graph of f1() and returns the graph"""
    print(create_plot_data(f1, -2, 2, 1001))
    (a1, b1) = create_plot_data(f1, -2, 2, 1001)
    (a2, b2) = create_plot_data(f2, -2, 2, 1001)
    pylab.plot(a1, b1, label='f1(x)')
    pylab.plot(a2, b2, label='f2(x)')
    pylab.xlabel('x')
    pylab.ylabel('y')
    pylab.legend()
    pylab.grid()
    pylab.savefig('plot.pdf')
    pylab.savefig('plot.png')
    pylab.show()


def find_cross():
    return sympy.solve(math.cos(2 * math.pi * x) * math.exp(-x ** 2) - math.log(x + 2.2), x)

` `

Hi im trying to define a function that will find the positive x value point where 2 equations are equal: f1(x) = f2(x)嗨,我正在尝试定义一个函数,该函数将找到两个方程相等的正 x 值点: f1(x) = f2(x)

f1(x) = math.cos(2 * math.pi * x) * math.e ** (-x ** 2)
f2(x) = math.log(x + 2.2)

for the point between x = 0 and x = 0.5对于x = 0x = 0.5之间的点对于 x = 0 和 x = 0.5 之间的点

When using SymPy you can take advantage of mpmath's root finder ability to work with arbitrary many digits:使用 SymPy 时,您可以利用mpmath 的根查找器功能来处理任意多个数字:

import mpmath
import sympy as sy

sy.init_printing()
mpmath.mp.dps = 30  # accuracy of 30 digits

# declare expressions:
x = sy.Symbol('x', real=True)
f1 = sy.cos(2*sy.pi*x) * sy.exp(-x**2)
f2 = sy.log(x + sy.Rational(22, 10))

g = f2 - f1  # f1 == f2 is equivalent to g == 0:
dg = g.diff(x)  # 1. derivative needed by numeric root-finder

# generate numeric functions from SymPy expressions:
g_m, dg_m = (sy.lambdify(x, f_, modules="mpmath") for f_ in (g, dg))

# find the roots:
x_sln = mpmath.findroot(g_m, [0, 0.5], solver='secant', df=dg)
print("Intersection f1(x) == f2(x) is at x = {}".format(x_sln))
# => Intersection f1(x) == f2(x) is at x = 0.0922612383093564032487110560683

Note that mixing SymPy , math (and NumPy ) functions can have unwanted side effects.请注意,混合SymPymath (和NumPy )函数可能会产生不需要的副作用。

暂无
暂无

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

相关问题 Python 如何使用(function F1 内部的变量) function 内部(F2 在 F1 内部) - Python how to use (variables inside function F1) inside function (F2 which is inside F1) 如何从 keyPressEvent 中获取 F1 或 F2 等字母值? - how can i get the alphabet value like F1 or F2 from keyPressEvent? 如何从函数列表 [f1, f2, f3,...fn] 组成嵌套的 function g=fn(...(f3(f2(f1()))...) - How to compose a nested function g=fn(…(f3(f2(f1()))…) from a list of functions [f1, f2, f3,…fn] 如何为 f(x) 方程编写代码以在 python 中给出其对应的 f(-x)? - How do I write a code for an f(x) equation to give its corresponding f(-x) in python? 如何避免 Python 3.7 中 f(x) = (1-cos(x))/x**2 中的小数的灾难性取消? - How do I avoid catastrophic cancellation for small numbers in f(x) = (1-cos(x))/x**2 in Python 3.7? 如何为 numpy 数组“x”实现 function f(x) = { x^2, x&gt;0 and -x^2, x&lt;0 }? - How do I implement the function f(x) = { x^2, x>0 and -x^2 , x<0 } for a numpy array 'x'? 在Python中使用赋值表达式时,如何完成赋值语句“x = y:= f(x)”? - How can an assignment statement “x = y := f(x)” be done when using assignment expressions in Python? Python中F(x)和F x之间的差异 - difference between F(x) and F x in Python 如何使用 dsolve() 求解同时包含 f(x) 和 x 的 ODE - How to solve ODE containing both f(x) and x using dsolve() x =收益率f()与x = f()有何不同? - How is x = yield f() different to x = f()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM