【发布时间】:2018-06-09 09:17:34
【问题描述】:
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)
`
您好,我正在尝试定义一个函数,该函数将找到两个方程相等的正 x 值点:f1(x) = f2(x)
f1(x) = math.cos(2 * math.pi * x) * math.e ** (-x ** 2)
f2(x) = math.log(x + 2.2)
x = 0 和 x = 0.5 之间的点
【问题讨论】:
-
对
g(x) = f1(x) - f2(x)的求根算法的一些建议:stackoverflow.com/questions/14878110/… -
These 是
scipy标量根查找器。
标签: python python-3.x numpy scipy