<jupyter_start><jupyter_text>第六章 插值与逼近 实验目的给定函数 $y = f(x)$ 的 $n + 1$ 个节点值 $y_j = f(x_j), j = 0, 1, ..., n$，试用 Lagrange 方法求其 $n$ 次插值多项式或分段插值多项式。 实验原理Lagrange 多项式插值（高次）与分段低次插值。 实验内容与步骤<jupyter_code>import numpy as np
import matplotlib.pyplot
from copy import deepcopy

from pylab import * 
mpl.rcParams['font.sans-serif'] = ['SimSun'] # 在其他机器上可能需要修改字体名
mpl.rcParams['axes.unicode_minus'] = False 
mpl.rcParams['savefig.dpi'] = 108<jupyter_output><empty_output><jupyter_text>给定数据如下，      $x_j$    0.4    0.55    0.65    0.80    0.95    1.05        $y_j$    0.41075    0.57815    0.69675    0.90    1.00    1.25382  构造 5 次 Lagrange 插值多项式和分段 2 次插值多项式，并计算 $f(0.596)$、$f(0.99)$ 的值。  五次插值<jupyter_code>x = np.array([0.4, 0.55, 0.65, 0.80, 0.95, 1.05])
y = np.array([0.41075, 0.57815, 0.69675, 0.90, 1.00, 1.25382])
def lagrange(x, y):
    length = len(x)
    res = 1
    functions = [None] * length
    for i in range(0, length):
        
        def result(z, i0):
            res = 1
            for j in range(0, length):
#                 print(res)
                if i0 == j:
#                     print('j: ', j)
#                     print('i: ', i0)
                    continue
                res = res * ((z - x[j]) / (x[i0] - x[j]))
            res = res * y[i0]
            return res
        
        functions[i] = result
    
    def result_2(a):
        res = 0
        count = 0
        for i in functions:
            res += i(a, count)
            count = count + 1
        return res
    
    return result_2
result_function = lagrange(x, y)
result_function(0.596), result_function(0.99)
x_perform = np.linspace(0.4, 1.05, 512, endpoint=True)
y_perform = [result_function(i) for i in x_perform]
plot(x_perform, y_perform)
show()<jupyter_output><empty_output><jupyter_text>分段二次插值<jupyter_code>def lagrange_part_2(x, y):
    length = len(x)
    def result(z):
        i = 2
        # 选取合适区间
        while i < length:
            if x[i - 2] <= z and z <= x[i]:
                break
            i = i + 1
        h = x[i] - x[i - 2]
        return y[i - 2] * (z - x[i-1]) * (z - x[i]) / ((x[i-2] - x[i-1]) * (x[i-2] - x[i])) \
                + y[i - 1] * (z - x[i-2]) * (z - x[i]) / ((x[i-1] - x[i-2]) * (x[i-1] - x[i])) \
                + y[i] * (z - x[i-2]) * (z - x[i-1]) / ((x[i] - x[i-1]) * (x[i] - x[i-2]))
    return result
lagrange_part_2(x, y)(0.596), lagrange_part_2(x, y)(0.99)
x_perform = np.linspace(0.4, 1.05, 512, endpoint=True)
y_perform = [lagrange_part_2(x, y)(i) for i in x_perform]
plot(x_perform, y_perform)
show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17177200465296627, 'fraction_numerical': 0.06785575804575417, 'mean_word_length': 1.9723502304147464, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '202352', 'license': 'Apache-2.0', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1190, 'n_words': 242, 'path': 'notebooks/chapter6.ipynb', 'repo_name': 'lalxyy/numeric-analysis-python', 'size': '2579'}