import makeData as md
import pandas as pd
import numpy as np
%pylab
plt.rc('figure', figsize=(16, 9))
L=[]
SL=14.0/6
for i in range(6):
L.append(-7+(i+1)*SL-0.01)
def realLineFunc(param):
def f(x):
i=int(math.floor((x+7)/SL))
return np.poly1d(param[3*i:3*(i+1)])(x)
return f
def draw1(title):
ps=md.loadData('data1.tl')
x=ps[0]
y=ps[1]
res=md.loadData('result.tl')
cres=[]
t1=0
for v in res.values():
cres=np.append(cres,v['param'])
t1+=v['iter']
res1=md.loadData('corres.tl')
cres1=res1['param']
d={'div总迭代次数':pd.Series(t1),
'f1迭代次数':pd.Series(res1['iter']),}
print pd.DataFrame(d)
plt.figure(1)
plt.plot(x,y,'b.',alpha=0.6,label="measure point")
plt.plot(x,map(lambda x:10*math.sin(0.6*x),x),c="cyan",lw=2,ls="-",alpha=0.7,label="real function")
x1=np.insert(x,0,L)
x1.sort()
plt.plot(x1,map(realLineFunc(cres),x1),c="red",lw=2,ls="-",alpha=0.7,label="f1")
plt.plot(x1,map(realLineFunc(cres1),x1),c="blue",lw=2,ls="-",alpha=0.7,label="div")
L.append(-7)
for l in L:
plt.plot([l,l],[-20,20],'b--',lw=2,alpha=0.5)
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.title(title)
plt.show()
draw1('Compare f1 with div')
综上,集中$\sigma$=[0.001,0.002,0.003,0.004,0.005],迭代次数如下:
$\sigma$ | iteration countiter |
---|---|
0.001 | 697 |
0.002 | 1056 |
0.003 | 2501 |
0.004 | 2323 |
0.005 | 2220 |