如何在 python 中创建一个函数,该函数采用乘积并重新代入方程式以获得解决方案

How to make a function in python that takes a product and re substitutes in equation to get a solution

我正在尝试在 Python 3 中创建一个函数,该函数采用初始值,将其代入方程式,采用该解,并按照 a 的参数指定的次数迭代该过程功能。最终目标是在代入等式时得到一个值 returns 相同的值。

我想使用的方程式是:

def f(x):
    return tanh(5*x)

谢谢

您可以使用 for 循环执行此操作:

from math import tanh

# the function you defined
def f(x):
    return tanh(5*x)

# create a separate function that repeats the process
def iterate(initial, iterations):
    for _ in range(iterations):
        initial = f(initial)
    return initial

print(iterate(10, 10000))

请注意,并非所有功能都会达到 f(x) -> x 但在这种情况下会达到