作业题目和要求: 解决思路:
分别使用基函数是fai_i(x) = x^i ,是勒让德正交基函数,是利用正交函数关系式构造出g0~g4实现拟合。
相关知识: 计算结果如下: 计算拟合误差,使用2-范数: 也就是误差平方求和再开方(方差好像。。)
import numpy as np
def Linear_independence_functions(m,n,list_x,list_y):
G = np.zeros(shape=(m, n+1))
for i in range (0,m):
G_row = []
for j in range (0,n+1):
G_row.append(list_x[i]**j)
G[i] = G_row
GT = np.transpose(G)
yT = np.transpose(list_y)
c = np.linalg.solve(np.dot(GT,G),np.dot(GT,yT))
print(c)
return c
def Legendre_Orthogonality_functions(m,n,list_x,list_y):
"""
c(k) = (gk,f) / (gk,gk),所以需要g0~g4
g0 = 1
g1 = x
g2 = 1/2(3x*x-1)
g3 = 1/2(5x*x*x-3x)
g4 = 1/8(35x*x*x*x-30x*x+3)
"""
list_b = [1,0,-1/2,0,3/8]
list_1 = [0,1,0,-3/2,0]
list_2 = [0,0,2/3,0,-30/8]
list_3 = [0,0,0,5/2,0]
list_4 = [0,0,0,0,35/8]
c = []
for i in range (0,n+1):
k1 = list_1[i]
k2 = list_2[i]
k3 = list_3[i]
k4 = list_4[i]
b = list_b[i]
sum_up = 0
sum_down = 0
for j in range (0,len(list_y)):
x = list_x[i]
g = k1 * x + k2 * (x ** 2) + k3 * (x ** 3) + k4 * (x ** 4) + b
sum_up += g*list_y[j]
sum_down += g*g
print(sum_up)
print(sum_down)
c.append(sum_up/sum_down)
print(c)
return c
def Orthogonality_functions(m,n,list_x,list_y):
"""
c(k) = (gk,f) / (gk,gk),所以需要g0~g4
g0 = 1
g1 = x-1/2
g2 = x*x-x+1/6
g3 = x**3-3/2x**2+3/5x+1/20
g4 = x**4-2x**3+7/9x**2-2/7x+1/70
"""
list_b = [1,-1/2,1/6,1/20,1/70]
list_1 = [0,1,-1,3/5,-2/7]
list_2 = [0,0,1,-3/2,9/7]
list_3 = [0,0,0,1,-2]
list_4 = [0,0,0,0,1]
c = []
for i in range (0,n+1):
k1 = list_1[i]
k2 = list_2[i]
k3 = list_3[i]
k4 = list_4[i]
b = list_b[i]
sum_up = 0
sum_down = 0
for j in range (0,len(list_y)):
x = list_x[i]
g = k1 * x + k2 * (x ** 2) + k3 * (x ** 3) + k4 * (x ** 4) + b
sum_up += g*list_y[j]
sum_down += g*g
print(sum_up)
print(sum_down)
c.append(sum_up/sum_down)
print(c)
return c
def Calculate_error(c,list_x,list_y):
error = 0
for i in range(0,len(list_x)):
x = list_x[i]
p = c[0]+c[1]*x+c[2]*(x**2)+c[3]*(x**3)+c[4]*(x**4)
error_each = (p-list_y[i])**2
error += error_each
error = error**0.5
print(error)
if __name__ == '__main__':
list_x = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
list_y = [5.1234,5.3057,5.5687,5.9375,6.4370,7.0978,7.9493,9.0253,10.3627]
m = len(list_x)
n = 4
c = Orthogonality_functions(m, n, list_x, list_y)
Calculate_error(c, list_x, list_y)
anyway ,不知道是代码问题还是基函数的选取问题,除了第一种方法以外,其余两种正交函数当基函数的误差都很大。 我看x的取值是0.1~0.9,勒让德的正交区间是[-1,1],应该是。。没问题的吧??? 但是这两种方法算出的c0是正确的,c1~c4人工检验计算太麻烦了就没算。。 Attention:如果想使用其他的基函数,只需要改变g函数的参数列表list_b,list_1~4就行。
结果运算如下: 首先是使用线性无关基函数,误差很小 第一行是求出来的c0~c4
C:\Anaconda\envs\pythonProject\python.exe C:/Users/871674389/PycharmProjects/pythonProject/Ordinary_least_squares.py
[5.00097222 0.99268907 2.01064782 3.00333463 0.99096737]
0.000574429212322953
Process finished with exit code 0
勒让德:
C:\Anaconda\envs\pythonProject\python.exe C:/Users/871674389/PycharmProjects/pythonProject/Ordinary_least_squares.py
[6.9786, 34.89299999999999, -15.860454545454544, -15.860454545454546, -24.142183783783782]
25.705626617459462
Process finished with exit code 0
构造的正交函数:
C:\Anaconda\envs\pythonProject\python.exe C:/Users/871674389/PycharmProjects/pythonProject/Ordinary_least_squares.py
[6.9786, -23.262, -161.04461538461538, 61.21578947368423, 1302.6719999999923]
905.0385923940642
Process finished with exit code 0
误差突增,就。。离谱
|