关于三次样条计算溢出问题,球球各位老哥们帮帮忙啦,万分感谢!!!
C:/Users/dell/PycharmProjects/pythonProject1/main.py:38: RuntimeWarning: overflow encountered in long_scalarsdata[(i - 1) * 4] = x * x * x
C:/Users/dell/PycharmProjects/pythonProject1/main.py:44: RuntimeWarning: overflow encountered in long_scalars
data1 = x * x * x
C:/Users/dell/PycharmProjects/pythonProject1/main.py:61: RuntimeWarning: overflow encountered in long_scalars
data[(sizeOfInterval - 1) * 4 + 0] = x[-1] * x[-1] * x[-1]
Process finished with exit code 0
各位大哥,上面这个错误该怎么改啊,救救孩子吧,不知道怎么增加整数个数。
下面是代码
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
from openpyxl import load_workbook
# 导入 excel 表格中的数据
wb = load_workbook(filename='sea2020.xlsx')
sheet_ranges = wb['sea2020']
def find():
for i in range(2, 53):
x.append(sheet_ranges['A%s' % i].value)
y.append(sheet_ranges['B%s' % i].value)
x = []
y = []
find()
x = np.array(x)
y = np.array(y)
# print(x)
def init(size):
j = 0
data = []
while j < size:
data.append(0)
j += 1
return data
def calculateEquationParameters(x):
# parameter为二维数组,用来存放参数,sizeOfInterval是用来存放区间的个数
parameter = []
sizeOfInterval = len(x) - 1
# print(sizeOfInterval) = 50正确
i = 1
# 首先输入方程两边相邻节点处函数值相等的方程为2n-2个方程
while i < len(x)-1:
data = init(sizeOfInterval * 4)
data[(i - 1) * 4] = x * x * x
data[(i - 1) * 4 + 1] = x * x
data[(i - 1) * 4 + 2] = x
data[(i - 1) * 4 + 3] = 1
# print(data) 一共有49行,49个方程
data1 = init(sizeOfInterval * 4)
data1 = x * x * x
data1 = x * x
data1 = x
data1 = 1
# print(data1) 一共有49行,49个方程
temp = data
# print(len(temp))
parameter.append(temp)
temp = data1
parameter.append(temp)
i += 1
# 输入端点处的函数值。为两个方程,加上前面的2n-2个方程,一共2n个方程
data = init(sizeOfInterval*4-2)
data = x
data = 1
parameter.append(data)
data = init(sizeOfInterval * 4)
data[(sizeOfInterval - 1) * 4 + 0] = x[-1] * x[-1] * x[-1]
data[(sizeOfInterval - 1) * 4 + 1] = x[-1] * x[-1]
data[(sizeOfInterval - 1) * 4 + 2] = x[-1]
data[(sizeOfInterval - 1) * 4 + 3] = 1
temp = data
parameter.append(temp)
# 端点一阶导数值相等为n-1个方程,就是将参数全部移到等式一端,另一端为零。加上前面的方程为3n-1个方程
i = 1
while i < len(x) - 1:
data = init(sizeOfInterval * 4)
data[(i - 1) * 4] = 3 * x * x
data[(i - 1) * 4 + 1] = 2 * x
data[(i - 1) * 4 + 2] = 1
data = -3 * x * x
data = -2 * x
data = -1
# print(data) 一共49个方程
temp = data
parameter.append(temp)
i += 1
# 端点二阶导数相等为n-1个方程,全部移到等式一端,另一端为0,一共为4n-2个方程。
i = 1
while i < len(x) - 1:
data = init(sizeOfInterval * 4)
data[(i - 1) * 4] = 6 * x
data[(i - 1) * 4 + 1] = 2
data = -6 * x
data = -2
# print(data) 一共49个方程
temp = data
parameter.append(temp)
i += 1
# print(parameter)
return parameter
# 计算出各方程参数的值
def solutionOfEquation(parametes,y):
sizeOfInterval = len(x) - 1
result = init(sizeOfInterval * 4 - 2)
i = 1
while i < sizeOfInterval:
result[(i - 1) * 2] = y
result[(i - 1) * 2 + 1] = y
i += 1
result[(sizeOfInterval - 1) * 2] = y
result[(sizeOfInterval - 1) * 2 + 1] = y[-1]
# print(len(result)) 列向量,198行
a = np.array(calculateEquationParameters(x),dtype='int64')
# print(len(a))
b = np.array(result,dtype='int64')
# print(b)
return np.linalg.solve(a, b)
def calculate(paremeters,x):
result = []
for data_x in x:
result.append(paremeters * data_x * data_x * data_x+ paremeters * data_x * data_x + paremeters * data_x + paremeters)
return result
def Draw(data_x,data_y,new_data_x,new_data_y):
plt.plot(new_data_x, new_data_y, label=u"拟合曲线", color="black")
plt.scatter(data_x, data_y, label=u"离散数据", color="red")
mpl.rcParams['font.sans-serif'] = ['SimHei']# 使图形中的中文正常编码显示
mpl.rcParams['axes.unicode_minus'] = False# 使坐标轴刻度表签正常显示正负号
plt.title(u"三次样条函数")
plt.legend(loc="best")
plt.show()
result = solutionOfEquation(calculateEquationParameters(x),y)
new_data_x = []
new_data_y = []
for i in range(0, 50):
new_data_x1 = []
new_data_x1 = np.arange(i * 100, (i + 1) * 100, 1)
if i == 0:
new_data_y1 = calculate( , result] , new_data_x1)
else:
new_data_y1 = calculate( , result , result , result] , new_data_x1)
new_data_x.extend(new_data_x1)
new_data_y.extend(new_data_y1)
Draw(x, y, new_data_x, new_data_y) 球球各位大哥帮帮忙{:9_220:} 这是excel数据,
{:9_218:}
页:
[1]