马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
C:/Users/dell/PycharmProjects/pythonProject1/main.py:38: RuntimeWarning: overflow encountered in long_scalars
data[(i - 1) * 4] = x[i] * x[i] * x[i]
C:/Users/dell/PycharmProjects/pythonProject1/main.py:44: RuntimeWarning: overflow encountered in long_scalars
data1[i * 4] = x[i] * x[i] * x[i]
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[i] * x[i] * x[i]
data[(i - 1) * 4 + 1] = x[i] * x[i]
data[(i - 1) * 4 + 2] = x[i]
data[(i - 1) * 4 + 3] = 1
# print(data) 一共有49行,49个方程
data1 = init(sizeOfInterval * 4)
data1[i * 4] = x[i] * x[i] * x[i]
data1[i * 4 + 1] = x[i] * x[i]
data1[i * 4 + 2] = x[i]
data1[i * 4 + 3] = 1
# print(data1) 一共有49行,49个方程
temp = data[2:]
# print(len(temp))
parameter.append(temp)
temp = data1[2:]
parameter.append(temp)
i += 1
# 输入端点处的函数值。为两个方程,加上前面的2n-2个方程,一共2n个方程
data = init(sizeOfInterval*4-2)
data[0] = x[0]
data[1] = 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[2:]
parameter.append(temp)
# 端点一阶导数值相等为n-1个方程,就是将参数全部移到等式一端,另一端为零。加上前面的方程为3n-1个方程
i = 1
while i < len(x) - 1:
data = init(sizeOfInterval * 4)
data[(i - 1) * 4] = 3 * x[i] * x[i]
data[(i - 1) * 4 + 1] = 2 * x[i]
data[(i - 1) * 4 + 2] = 1
data[i * 4] = -3 * x[i] * x[i]
data[i * 4 + 1] = -2 * x[i]
data[i * 4 + 2] = -1
# print(data) 一共49个方程
temp = data[2:]
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[i]
data[(i - 1) * 4 + 1] = 2
data[i * 4] = -6 * x[i]
data[i * 4 + 1] = -2
# print(data) 一共49个方程
temp = data[2:]
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[i]
result[(i - 1) * 2 + 1] = y[i]
i += 1
result[(sizeOfInterval - 1) * 2] = y[0]
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[0] * data_x * data_x * data_x+ paremeters[1] * data_x * data_x + paremeters[2] * data_x + paremeters[3])
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([0 , 0 , result[0] , result[1]] , new_data_x1)
else:
new_data_y1 = calculate([result[2 + (i - 1) * 4] , result[3 + (i - 1) * 4] , result[4 + (i - 1) * 4] , result[5 + (i - 1) * 4]] , 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)
|