首页 > 解决方案 > 抛物线的curve_fit()问题

问题描述

我无法将 curve_fit() 应用于 .csv 文件中的数据。.csv 文件是从运动传感器下方多次弹跳的篮球中获取的真实数据。数据点的图是球高与时间的关系,结果图显示了一系列递减的抛物线。制作该情节是小菜一碟,但我显然不知道我在用curve_fit()做什么。我认为问题在于我对系数 a、b 和 c 的选择,尽管我认为 b 和 c 将从 0 开始,并且如家庭作业中所述,a 应该是 g/2,其中 g 是当然-9.81。不幸的是,curve_fit 只显示了一个与数据无关的大的向下抛物线。我哪里错了?我也不知道如何在这个问题中包含一个 csv 文件,所以我将数据粘贴在代码下方。我将其作为代码的一部分输入以保持格式。如果有办法在这些问题中包含数据文件,请赐教。

import numpy as np    
import matplotlib.pylab as plt 
from scipy.optimize import curve_fit  

dname=r'my_computer_directory'  **#replace with your own directory name**                    
fname='free_fall_run18_csv.csv'                                
dfname ='{0:s}/{1:s}'.format(dname,fname)                  

data01=np.genfromtxt(dfname,delimiter=',',skip_header=1)   # data01 is a ndarray
                                                           # skip one header -> skip_header=1
                                                           # csv             -> delimiter=','
count = 0
with open(fname, 'r') as f:
    for line in f:
        count +=1
        n1 = count-1
print ('Number of data points is', count-1) 

t = data01[:,0]       
y = data01[:,1]*(-1)      

# remove any negative times
t_temp=t[np.where(t>0)]
y_temp=y[np.where(t>0)]

def myline1(t,a,b,c):
    y=a*t**2+b*t+c
    return y

g=-9.81
a0=g/2
b0=0
c0=0
popt0,pcov0 = curve_fit(myline1,t_temp,y_temp,p0=[a0,b0,c0])
a0=popt0[0]   
b0=popt0[1]
c0=popt0[2]
y_hat1=a0*t_temp**2+b0*t_temp+c0 

#fig = plt.figure()
plt.plot(t_temp,y_temp, '.', label = 'Height vs Time')
plt.grid() #show grid
plt.title('Height vs Time')
plt.legend()
plt.xlabel('Time [s]') #label axis
plt.ylabel('Height [m]') #label axis
plt.show() #show figure

plt.plot(t_temp,y_temp, '.', label = 'Height vs Time') 
plt.plot(t_temp,y_hat1, '-', label = 'Height vs Time with curve_fit()')
plt.grid() #show grid
plt.title('Height vs Time with curve_fit()')
plt.legend()
plt.xlabel('Time [s]') #label axis
plt.ylabel('Height [m]') #label axis
plt.show() #show figure

#.csv data   
Time (s),Position (m)
0,0.152
0.025,0.152
0.05,0.152
0.075,0.152
0.1,0.151
0.125,0.151
0.15,0.151
0.175,0.151
0.2,0.15
0.225,0.15
0.25,0.151
0.275,0.151
0.3,0.151
0.325,0.15
0.35,0.15
0.375,0.149
0.4,0.148
0.425,0.147
0.45,0.146
0.475,0.145
0.5,0.144
0.525,0.143
0.55,0.143
0.575,0.143
0.6,0.144
0.625,0.144
0.65,0.145
0.675,0.146
0.7,0.147
0.725,0.148
0.75,0.154
0.775,0.166
0.8,0.184
0.825,0.207
0.85,0.238
0.875,0.273
0.9,0.316
0.925,0.364
0.95,0.417
0.975,0.477
1,0.543
1.025,0.615
1.05,0.694
1.075,0.78
1.1,0.869
1.125,0.964
1.15,1.066
1.175,1.19
1.2,1.105
1.225,1.019
1.25,0.94
1.275,0.867
1.3,0.802
1.325,0.741
1.35,0.687
1.375,0.639
1.4,0.596
1.425,0.56
1.45,0.53
1.475,0.505
1.5,0.488
1.525,0.475
1.55,0.469
1.575,0.468
1.6,0.474
1.625,0.485
1.65,0.502
1.675,0.525
1.7,0.554
1.725,0.589
1.75,0.63
1.775,0.677
1.8,0.73
1.825,0.791
1.85,0.853
1.875,0.924
1.9,1.003
1.925,1.087
1.95,1.19
1.975,1.119
2,1.045
2.025,0.98
2.05,0.921
2.075,0.867
2.1,0.822
2.125,0.781
2.15,0.747
2.175,0.718
2.2,0.696
2.225,0.679
2.25,0.668
2.275,0.663
2.3,0.663
2.325,0.669
2.35,0.682
2.375,0.7
2.4,0.725
2.425,0.755
2.45,0.792
2.475,0.834
2.5,0.882
2.525,0.937
2.55,0.997
2.575,1.064
2.6,1.147
2.625,1.153
2.65,1.092
2.675,1.036
2.7,0.987
2.725,0.945
2.75,0.907
2.775,0.876
2.8,0.851
2.825,0.831
2.85,0.82
2.875,0.813
2.9,0.812
2.925,0.816
2.95,0.826
2.975,0.843
3,0.865
3.025,0.894
3.05,0.929
3.075,0.969
3.1,1.016
3.125,1.068
3.15,1.132
3.175,1.166
3.2,1.113
3.225,1.066
3.25,1.026
3.275,0.991
3.3,0.963
3.325,0.94
3.35,0.923
3.375,0.913
3.4,0.908
3.425,0.91
3.45,0.917
3.475,0.93
3.5,0.95
3.525,0.975
3.55,1.006
3.575,1.043
3.6,1.086
3.625,1.141
3.65,1.161
3.675,1.117
3.7,1.078
3.725,1.045
3.75,1.019
3.775,0.998
3.8,0.984
3.825,0.975
3.85,0.972
3.875,0.975
3.9,0.985
3.925,1
3.95,1.022
3.975,1.049
4,1.083
4.025,1.123
4.05,1.174
4.075,1.136
4.1,1.102
4.125,1.074
4.15,1.051
4.175,1.035
4.2,1.025
4.225,1.021
4.25,1.023
4.275,1.03
4.3,1.044
4.325,1.063
4.35,1.089
4.375,1.121
4.4,1.167
4.425,1.148
4.45,1.117
4.475,1.093
4.5,1.075
4.525,1.063
4.55,1.057
4.575,1.056
4.6,1.062
4.625,1.074
4.65,1.091
4.675,1.115
4.7,1.149
4.725,1.161
4.75,1.133
4.775,1.112
4.8,1.096
4.825,1.086
4.85,1.082
4.875,1.084
4.9,1.092
4.925,1.107
4.95,1.127
4.975,1.158
5,1.16
5.025,1.137
5.05,1.121
5.075,1.11
5.1,1.105

标签: pythoncsvscipycurve-fitting

解决方案


推荐阅读