如何在python中使输出成为输入
如何编写一个代码,其中alpha(a)的起始值为a=2,alpha(2)的解将在下次成为输入。例如:假设alpha(2)为2.39,因此下一个值为alpha(2.39)并继续{50次迭代}。谁能帮我一点忙吗。提前感谢。您可以让程序使用如何在python中使输出成为输入,python,Python,如何编写一个代码,其中alpha(a)的起始值为a=2,alpha(2)的解将在下次成为输入。例如:假设alpha(2)为2.39,因此下一个值为alpha(2.39)并继续{50次迭代}。谁能帮我一点忙吗。提前感谢。您可以让程序使用循环进行迭代,并使用变量存储中间结果: >>> import math #defining first function >>> def f(a): return a-math.sin(a)-math.pi/2
循环进行迭代,并使用变量存储中间结果:
>>> import math
#defining first function
>>> def f(a):
return a-math.sin(a)-math.pi/2
#defining second fuction
>>> def df(a):
return 1-math.cos(a)
#defining third function which uses above functions
>>> def alpha(a):
return a-f(a)/df(a)
print(temp)
将打印中间结果。这不是必需的。它仅演示如何在整个过程中更新temp
变量。您可以将其对象化
temp = 2 # set temp to the initial value
for _ in range(50): # a for loop that will iterate 50 times
temp = alpha(temp) # call alpha with the result in temp
# and store the result back in temp
print(temp) # print the result (optional)
然后创建一个inout
对象,每次调用它的alpha
方法时,它都会给出序列中的下一个值
import math
class inout:
def __init__(self, start):
self.value = start
def f(self, a):
return a-math.sin(a)-math.pi/2
def df(self, a):
return 1-math.cos(a)
def alpha(self):
self.value = self.value-self.f(self.value)/self.df(self.value)
return self.value
您是希望在程序运行时执行此操作,还是希望能够退出应用程序并继续使用上次使用的号码?在程序运行时使用for循环?@idjaw。谢谢,谢谢我将尝试使用for loopSo,在某个时刻,这些值不断重复。是否有任何程序可以从50次迭代中选择重复值???@GarrySaini:通常,如果它们开始重复,您可以选择最后一个值。这意味着您已经找到了程序的固定点。
demo = inout(2)
print(demo.alpha())