本篇为数值分析课程代码实现-拉格朗日插值法的实现
仅供参考
配置环境
- 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 Lagrangian():
def __init__(self):
self.X = None
self.y = None
def fit(self, X, y):#0-n
if type(X) != np.ndarray:
X=np.array(X)
if type(y) != np.ndarray:
y=np.array(y)
self.X=X
self.y=y
pass
def predict(self, X_pre):
if type(X_pre) != np.ndarray:
X_pre = np.array(X_pre)
def fun(x):