所以我真的不明白我在这里做错了什么,我认为它与变量或循环或其他东西有关。我尝试运行这个,除了curve_fit函数之外,一切都运行得很好,只是给出了maxfev错误,无论我给出的是什么maxfev,它都会给出相同的错误。
from numpy import *
from matplotlib.pyplot import *
from scipy.optimize import curve_fit
r = array([0.24/2, 0.30/2, 0.40/2, 0.48/2, 0.73/2])
def v(t,*p):
return p[0]*tanh(9.81*t/p[0]) + p[1]
g = []
terr = array([0.005, 0.005, 0.01, 0.01, 0.015])
xerr = array([0.0005,0.0005,0.0005,0.0005,0.0005])/1000.0
vg = []
vgerr = []
for i in range(5):
gp = []
vgerrp = []
vgp = []
vgerrp = []
for n in range(4):
gp = gp + [loadtxt(r'%iw-sz=%i.txt' % (n+1,i+1), skiprows=2)]
gp[n][:,0] = gp[n][:,0]-gp[n][0,0]
gp[n][:,1] = gp[n][:,1]/1000.0
vgp = vgp + [zeros(len(gp[n][:,0]))]
vgerrp = vgerrp + [zeros(len(gp[n][:,0]))]
g = g + [gp]
vg = vg + [vgp]
vgerr = vgerr + [vgerrp]
for i in range(5):
figure('ball size %i' % (i+1))
title('Water Velocity vs Time graph for ball size %i' % (i+1))
for n in range(4):
subplot('41%i' % (n+1))
xlabel('Time (s)')
ylabel('Velocity (m/s)')
time = zeros(len(vg[i][n]))
vel = zeros(len(vg[i][n]))
velerr = zeros(len(vg[i][n]))
for k in range(len(vg[i][n])-1):
vg[i][n][k] = (g[i][n][k+1,1]-g[i][n][k,1])/(g[i][n][k+1,0]-g[i][n][k,0])
vgerr[i][n][k] = vg[i][n][k]*sqrt((sqrt(2)*xerr[i])**2/(g[i][n][k+1,1]-g[i][n][k,1])**2 + (sqrt(2)*terr[i])**2/(g[i][n][k+1,0]-g[i][n][k,0])**2)
time[k] = float(g[i][n][k,0])
vel[k] = float(vg[i][n][k])
velerr[k] = float(vgerr[i][n][k])
if k == len(vg[i][n])-1:
vg[i][n][k+1] = vg[i][n][k]
vgerr[i][n][k+1] = vgerr[i][n][k]
if n == 0:
col = 'ob'
elif n == 1:
col = 'or'
elif n == 2:
col = 'og'
elif n == 3:
col = 'oy'
if vg[i][n][k] != 0:
plot(g[i][n][k,0],vg[i][n][k], col)
errorbar(g[i][n][k,0],vg[i][n][k], xerr = terr[i], yerr = vgerr[i][n][k], fmt='+r')
fparam, fcov = curve_fit(v, time, vel, [0.01,0.0], velerr)
t = arange(0,max(g[i][n][:,0]),0.01)
plot(t,v(t, fparam[0]))
show()
我尝试使用相同的数据运行相同的代码,就像在不同的程序中使用数值一样,并且它有效:
from numpy import *
from matplotlib.pyplot import *
from scipy.optimize import curve_fit
def v(t,*p):
return p[0]*tanh(9.81*t/p[0]) + p[1]
vel = array([0.06562 ,
0.07654 ,
0.0656 ,
0.06562 ,
0.05468 ,
0.07654 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.07654 ,
0.06562 ,
0.0656 ,
0.06562 ,
0.07654 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.06562 ,
0.07654 ,
0.0656 ,
0.06562 ,
0.07654 ,
0.06562 ,
0.0656 ,
0.0656 ,
0.07656 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.07654 ,
0.06562 ,
0.0656 ,
0.06562 ,
0.07654 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.07654 ,
0.06562 ,
0.0656 ,
0.07656 ,
0.0656 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.06562 ,
0.07654 ,
0.06562 ,
0.0656 ,
0.0656 ,
0.06562 ,
0.07654 ,
0.06562 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.0656 ,
0.07656 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.0656 ,
0.07656 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.0656 ,
0.06562 ,
0.0656 ,
0.06562])
velerr = array([0.00928008017207 ,
0.0108243998448 ,
0.00927725174823 ,
0.00928008017207 ,
0.00773293269077 ,
0.0108243998448 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.0108243998448 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207 ,
0.0108243998448 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207 ,
0.0108243998448 ,
0.00927725174823 ,
0.00928008017207 ,
0.0108243998448 ,
0.00928008017207 ,
0.00927725174823 ,
0.00927725174823 ,
0.0108272282695 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.0108243998448 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207 ,
0.0108243998448 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.0108243998448 ,
0.00928008017207 ,
0.00927725174823 ,
0.0108272282695 ,
0.00927725174823 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207 ,
0.0108243998448 ,
0.00928008017207 ,
0.00927725174823 ,
0.00927725174823 ,
0.00928008017207 ,
0.0108243998448 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00927725174823 ,
0.0108272282695 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00927725174823 ,
0.0108272282695 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00927725174823 ,
0.00928008017207 ,
0.00927725174823 ,
0.00928008017207])
time = array([0.0 ,
0.05 ,
0.1 ,
0.15 ,
0.2 ,
0.25 ,
0.3 ,
0.35 ,
0.4 ,
0.45 ,
0.5 ,
0.55 ,
0.6 ,
0.65 ,
0.7 ,
0.75 ,
0.8 ,
0.85 ,
0.9 ,
0.95 ,
1.0 ,
1.05 ,
1.1 ,
1.15 ,
1.2 ,
1.25 ,
1.3 ,
1.35 ,
1.4 ,
1.45 ,
1.5 ,
1.55 ,
1.6 ,
1.65 ,
1.7 ,
1.75 ,
1.8 ,
1.85 ,
1.9 ,
1.95 ,
2.0 ,
2.05 ,
2.1 ,
2.15 ,
2.2 ,
2.25 ,
2.3 ,
2.35 ,
2.4 ,
2.45 ,
2.5 ,
2.55 ,
2.6 ,
2.65 ,
2.7 ,
2.75 ,
2.8 ,
2.85 ,
2.9 ,
2.95 ,
3.0 ,
3.05 ,
3.1 ,
3.15 ,
3.2 ,
3.25 ,
3.3 ,
3.35 ,
3.4 ,
3.45 ,
3.5 ,
3.55])
fparam, fcov = curve_fit(v, time, vel, [0.01,0.0], velerr)
这就是为什么我不知道还能做什么,除了保存数据并在不同的程序中运行,有人可以帮助我吗?