-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcleastsq.py
115 lines (93 loc) · 2.53 KB
/
cleastsq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# coding=utf-8
'''
作者:Jairus Chan
程序:多项式曲线拟合算法
'''
import matplotlib.pyplot as plt
import math
import numpy
import random
#阶数为9阶
order=9
#进行曲线拟合
def getMatA(xa):
matA=[]
for i in range(0,order+1):
matA1=[]
for j in range(0,order+1):
tx=0.0
for k in range(0,len(xa)):
dx=1.0
for l in range(0,j+i):
dx=dx*xa[k]
tx+=dx
matA1.append(tx)
matA.append(matA1)
matA=numpy.array(matA)
return matA
def getMatB(xa,ya):
matB=[]
for i in range(0,order+1):
ty=0.0
for k in range(0,len(xa)):
dy=1.0
for l in range(0,i):
dy=dy*xa[k]
ty+=ya[k]*dy
matB.append(ty)
matB=numpy.array(matB)
return matB
def getMatAA(xa, ya):
matAA=numpy.linalg.solve(getMatA(xa),getMatB(xa, ya))
return matAA
#设置阶数(默认为9
def setOrder(newOrder):
order = newOrder
#获取拟合后的新序列
def getFitYValues(xValues, yValues, xNewValues):
matAA_get = getMatAA(xValues, yValues)
yya=[]
for i in range(0,len(xNewValues)):
yy=0.0
for j in range(0,order+1):
dy=1.0
for k in range(0,j):
dy*=xNewValues[i]
dy*=matAA_get[j]
yy+=dy
yya.append(yy)
return yya
#画出拟合后的曲线
#print(matAA)
if __name__ == "__main__":
fig = plt.figure()
ax = fig.add_subplot(111)
#生成曲线上的各个点
x = numpy.arange(-1,1,0.02)
y = [((a*a-1)*(a*a-1)*(a*a-1)+0.5)*numpy.sin(a*2) for a in x]
#ax.plot(x,y,color='r',linestyle='-',marker='')
#,label="(a*a-1)*(a*a-1)*(a*a-1)+0.5"
#生成的曲线上的各个点偏移一下,并放入到xa,ya中去
i=0
xa=[]
ya=[]
for xx in x:
yy=y[i]
d=float(random.randint(60,140))/100
#ax.plot([xx*d],[yy*d],color='m',linestyle='',marker='.')
i+=1
xa.append(xx*d)
ya.append(yy*d)
'''for i in range(0,5):
xx=float(random.randint(-100,100))/100
yy=float(random.randint(-60,60))/100
xa.append(xx)
ya.append(yy)'''
ax.plot(xa,ya,color='m',linestyle='',marker='.')
matAA_n = getMatAA(xa,ya)
print len(matAA_n)
xxa= numpy.arange(-1,1.2,0.01)
yya= getFitYValues(xa, ya, xxa)
ax.plot(xxa,yya,color='g',linestyle='-',marker='')
ax.legend()
plt.show()