from math import pi,exp
import numpy as np
R1 = np.loadtxt(fname = 'D:\研究所\XRD資料\LCMO\python data matrix test\lcmofakee.txt')
R2 = np.loadtxt(fname = 'D:\研究所\XRD資料\LCMO\python data matrix test\lcmofake.txt')
d = 200.0# nm
def func( r,r1,r2 ):
return exp(-pir1d/r)+exp(-pir2d/r)-1
def derivFunc( r,r1,r2 ):
return ((pir1dexp(-pir1d/r)+pir2dexp(-pir2d/r))/(r*r))
def newtonRaphson( r,r1,r2 ):
#if derivFunc(x) > 0:
h = func(r,r1,r2) / derivFunc(r,r1,r2)
while abs(h) >= 0.00001:
h = func(r,r1,r2)/derivFunc(r,r1,r2)
# x(i+1) = x(i) - f(x) / f'(x)
r = r - h
print("temperature =",T)
print("The value of the root is : ", "%.10f"%r)
print("resistivity unit is ohm-cm :",r*10**-7)
np.savetxt('D:\研究所\XRD資料\LCMO\python data matrix test\lcmofakeresistivity1.txt',zip(r))
for i in range(0,len(R1)) :
r1 = R1[i,1]
r2 = R2[i,1]
T = R1[i,0]
r0 = r2*d # Initial values assumed
newtonRaphson(r0,r1,r2)
底下是節錄部份的程式碼:
with open("檔名", "w") as f:
for i in range(0,len(R1)) :
r1 = R1[i,1]
r2 = R2[i,1]
T = R1[i,0]
# Driver program to test above
r0 = r2*d # Initial values assumed
newtonRaphson(r0,r1,r2)
f.write("{} {} {} {}\n".format(T,r1, r2,resistivity))
這樣應該就能寫檔了,你要把 write
的部份放到迴圈去才會把每一筆都輸出
temperature = 150.21
The value of the root is : 12904717.0902160592
resistivity unit is ohm-cm : 1.2904717090216058
Traceback (most recent call last):
File "C:\Python35-32\van der puaw main program", line 38, in
f.write("{} {} {} {}\n".format(T,r1, r2,resistivity))
NameError: name 'resistivity' is not defined
resistivity這東西要在
def newtonRaphson( r,r1,r2 ):
#if derivFunc(x) > 0:
h = func(r,r1,r2) / derivFunc(r,r1,r2)
while abs(h) >= 0.00001:
h = func(r,r1,r2)/derivFunc(r,r1,r2)
下面才找的到
好囉感激您