本篇为数值分析课程代码实现-牛顿差商法 的实现
仅供参考
配置环境
- Python3.6
- matplotlib
- numpy
-
# -*- coding: utf-8 -*- """ Created on Tue Dec 4 21:04:35 2018 @author: hhuaf """ import matplotlib.pyplot as plt import numpy as np #可以显示中文 plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams['axes.unicode_minus'] = False # 设置风格 plt.style.use('ggplot') class Newton(): def __init__(self): self.W = None self.X = None def fit(self, X, y):#0-n-1 if type(X) != np.ndarray: X=np.array(X) if type(y) != np.ndarray: y=np.array(y) W = np.zeros([len(X),len(X)+1]) W_list=[] W[:,0] = X W[:,1] = y #针对每列 W_list.append(W[0,1]) for j in range(2,W.shape[1]): #对每行 for i in range(j-1,W.shape[0]): W[i,j] = (W[i,j-1] - W[i-1,j-1]) / (W[