You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.3 KiB
84 lines
3.3 KiB
import random
|
|
import numpy as np
|
|
import data as gl_data
|
|
|
|
|
|
def linear_function(coefficient, x): # 构造一次函数y = a * X + b
|
|
return coefficient[0] * x + coefficient[1]
|
|
|
|
|
|
def quadratic_function(coefficient, x): # 构造二次函数y = a * X^2 + b * X + c
|
|
return coefficient[0] * x ** 2 + coefficient[1] * x + coefficient[2]
|
|
|
|
|
|
def qubic_function(coefficient, x): # 构造三次函数y = a* X^3 + b * X^2 + c * X + d
|
|
return coefficient[0] * x ** 3 + coefficient[1] * x ** 2 + coefficient[2] * x + coefficient[3]
|
|
|
|
|
|
def quartic_function(coefficient, x): # 构造四次函数
|
|
return coefficient[0] * x ** 4 + coefficient[1] * x ** 3 + coefficient[2] * x ** 2 + coefficient[3] * x + \
|
|
coefficient[4]
|
|
|
|
|
|
# 编程4.1 -----------------------------------------
|
|
def random_points(sampleNum, Low, High):
|
|
sx, sy=[], []
|
|
for i in range(sampleNum):
|
|
sx.append(random.uniform(Low, High)) # 生成随机浮点数
|
|
sy.append(random.uniform(Low, High))
|
|
sx, sy = np.array(sx), np.array(sy) # 列表转数组
|
|
gl_data.SampleData = np.array(list(zip(sx, sy))) # 拼接成二维数组并存入全局变量
|
|
return gl_data.SampleData
|
|
# if __name__ == '__main__':
|
|
# sampleData = random_points(20, 0, 1000)
|
|
# print(sampleData )
|
|
# 编程4.1 END-----------------------------------------
|
|
|
|
|
|
# 编程4.2 -----------------------------------------
|
|
def compute_curveData(Low, High, step, coefficient, m): # coefficient代表二次函数系数
|
|
x_values = np.arange(Low, High, step)
|
|
if m == 1:
|
|
y_values = linear_function(coefficient, x_values)
|
|
if m == 2:
|
|
y_values = quadratic_function(coefficient, x_values) # 调用quadratic_function(x)函数得到y值
|
|
if m == 3:
|
|
y_values = qubic_function(coefficient, x_values)
|
|
if m == 4:
|
|
y_values = quartic_function(coefficient, x_values)
|
|
gl_data.CurveData = np.column_stack((x_values, y_values)) # 将x,y堆叠成二维数组
|
|
return gl_data.CurveData
|
|
# if __name__ == '__main__':
|
|
# coefficient = [1, 2, 3]
|
|
# curveData = compute_curveData(gl_data.LOW, gl_data.HIGH, 1, coefficient,2)
|
|
# print(curveData)
|
|
|
|
|
|
# 由于出现了数据稳定性的问题,因此在这里多了一个需要进行标准化处理的步骤
|
|
def compute_curveData2(low, high, step, theta, m, x_mean, x_std):
|
|
x_fit = np.arange(low, high, step)
|
|
# 将x_fit标准化
|
|
x_fit_normalized = (x_fit - x_mean) / x_std
|
|
A_fit = np.vander(x_fit_normalized, m + 1, increasing=True)
|
|
y_fit = A_fit @ theta
|
|
gl_data.CurveData = np.column_stack((x_fit, y_fit))
|
|
return gl_data.CurveData
|
|
|
|
|
|
# 在X5中由于拓展了函数因此修改
|
|
def compute_curveData_X5(func, popt):
|
|
x_values = np.arange(gl_data.LOW, gl_data.HIGH, 1)
|
|
y_values = func(x_values, *popt)
|
|
gl_data.CurveData = np.column_stack((x_values, y_values))
|
|
return gl_data.CurveData
|
|
|
|
# 进行标准化的X5函数
|
|
def compute_curveData_X5_2(func, popt, sampleData):
|
|
sx, sy = sampleData[:, 0], sampleData[:, 1]
|
|
x_values = np.arange(gl_data.LOW, gl_data.HIGH, 1)
|
|
x_values_normalized = (x_values - np.mean(sx)) / np.std(sx)
|
|
y_values = func(x_values_normalized, *popt)
|
|
gl_data.CurveData = np.column_stack((x_values, y_values))
|
|
return gl_data.CurveData
|
|
# 编程4.2 END-----------------------------------------
|