この関数から計算されたデータをファイルに書き込もうとしています。しかし、その機能は回数と呼ばれています。別のファイルに9つの数字があり、この関数はこれらの9つの数字のそれぞれの根を計算します。この関数からの9つのルーツは、同じファイルに書き込まれる必要があります。しかし、私がここでそれをした方法は、ファイルに計算されたルートを書きますが、次のものはファイル内のこれを置き換えます。この関数が呼び出される前に、これらの9つの数値のそれぞれに対して実行される他の数学関数があります。したがって、関数は別々に繰り返し呼び出されます。すべて同じファイルに書き込むことは可能ですか?ありがとうございました。同じファイル内の関数から計算した複数のデータを書き込む
def Newton(poly, start):
""" Newton's method for finding the roots of a polynomial."""
x = start
poly_diff = poly_differentiate(poly)
n = 1
counter = 0
r_i = 0
cFile = open("curve.dat", "w")
while True:
if (n >= 0) and (n < 1):
break
x_n = x - (float(poly_substitute(poly, x))/poly_substitute(poly_diff, x))
if x_n == x:
break
x = x_n # this is the u value corresponding to the given time
n -= 1
counter += 1
x = str(x)
cFile.write('\n' + x + '\n')
if r_i:
print "t(u) = ", (x, counter)
else:
print "t(u) = ", x
cFile.close
は私は次のようにコードを変更しました提案に従った後:
def Newton(poly, start):
""" Newton's method for finding the roots of a polynomial."""
x = start
poly_diff = poly_differentiate(poly)
n = 1
counter = 0
while True:
if (n >= 0) and (n < 1):
break
x_n = x - (float(poly_substitute(poly, x))/poly_substitute(poly_diff, x))
if x_n == x:
break
x = x_n # this is the u value corresponding to the given time
n -= 1
counter += 1
yield x
Bezier(x)
def Bezier(u_value) :
""" Calculating sampling points using rational bezier curve equation"""
u = u_value
p_u = math.pow(1 - u, 3) * 0.7 + 3 * u * math.pow(1 - u, 2) * 0.23 \
+ 3 * (1 - u) * math.pow(u, 2) * 0.1 + math.pow(u, 3) * 0.52
p_u = p_u * w
d = math.pow(1 - u, 3) * w + 3 * u * w * math.pow(1 - u, 2) + 3 * (1 - u) *\
w * math.pow(u, 2) + math.pow(u, 3) * w
p_u = p_u/d
yield p_u
plist = list (p_u)
print plist
を私はベジェ()関数内で同じことを続いたが、それは印刷されないようなplistは作成されません何でも助けてください。ありがとうございました。ない書き込みモードで、
オープン追記モードでファイルを:私は完全にあなたが求めているものを理解していないよものの
私は前にリストでやりました。リスト内のファイルと同様に、次のファイルも現在の値を置き換えます。関数が再び呼び出されたときに値をどのように追加するのですか?おかげで – zingy
ありがとう。試してみる。 – zingy
または 'Newton(...)のルート:for output_file.writeline(root)'です。 – katrielalex