从下面函数表, x 81 100 121 144 169 225 f(x) 9 10 11 12 13 15 分别用 基于拉格朗日插值法,利用线性插值和抛物线插值,求√(115)的近似值; 基于牛顿插值法(阅读课本P81-P84),利用线性插值和抛物线插值,求√(115)的近似值; 要求:(a)利用程序自动选择在插值计算中所需要的节点;(b)把原始数据点以及得到的插值多项式函数曲线都绘制在MATLAB Figure里,便于观察插值效果。
时间: 2023-11-28 19:48:10 浏览: 61
我们可以使用 MATLAB 来实现这个问题。
首先,我们需要定义原始数据点:
```matlab
x = [81 100 121 144 169 225];
f = [9 10 11 12 13 15];
```
接下来,我们可以使用 `interp1` 函数来进行基于拉格朗日插值法的线性插值和抛物线插值,以及基于牛顿插值法的线性插值和抛物线插值。
```matlab
% 线性插值
lin_interp = interp1(x, f, sqrt(115), 'linear');
% 抛物线插值
par_interp = interp1(x, f, sqrt(115), 'pchip');
% 牛顿插值法
newton_interp = interp1(x, f, sqrt(115), 'spline');
% 抛物线牛顿插值法
poly_newton_interp = interp1(x, f, sqrt(115), 'v5cubic');
```
最后,我们可以使用 `plot` 函数将原始数据点以及插值多项式函数曲线绘制在 MATLAB Figure 里。
```matlab
% 绘制原始数据点
scatter(x, f, 'filled');
hold on;
% 绘制插值多项式函数曲线
xx = linspace(min(x), max(x), 1000);
yy = interp1(x, f, xx, 'linear');
plot(xx, yy, 'b--', 'LineWidth', 1.5);
yy = interp1(x, f, xx, 'pchip');
plot(xx, yy, 'g--', 'LineWidth', 1.5);
yy = interp1(x, f, xx, 'spline');
plot(xx, yy, 'r--', 'LineWidth', 1.5);
yy = interp1(x, f, xx, 'v5cubic');
plot(xx, yy, 'm--', 'LineWidth', 1.5);
% 添加图例和标题
legend('原始数据点', '线性插值', '抛物线插值', '牛顿插值法', '抛物线牛顿插值法', 'Location', 'northwest');
title('插值函数曲线');
```
完整代码如下:
```matlab
% 定义原始数据点
x = [81 100 121 144 169 225];
f = [9 10 11 12 13 15];
% 线性插值
lin_interp = interp1(x, f, sqrt(115), 'linear');
% 抛物线插值
par_interp = interp1(x, f, sqrt(115), 'pchip');
% 牛顿插值法
newton_interp = interp1(x, f, sqrt(115), 'spline');
% 抛物线牛顿插值法
poly_newton_interp = interp1(x, f, sqrt(115), 'v5cubic');
% 绘制原始数据点
scatter(x, f, 'filled');
hold on;
% 绘制插值多项式函数曲线
xx = linspace(min(x), max(x), 1000);
yy = interp1(x, f, xx, 'linear');
plot(xx, yy, 'b--', 'LineWidth', 1.5);
yy = interp1(x, f, xx, 'pchip');
plot(xx, yy, 'g--', 'LineWidth', 1.5);
yy = interp1(x, f, xx, 'spline');
plot(xx, yy, 'r--', 'LineWidth', 1.5);
yy = interp1(x, f, xx, 'v5cubic');
plot(xx, yy, 'm--', 'LineWidth', 1.5);
% 添加图例和标题
legend('原始数据点', '线性插值', '抛物线插值', '牛顿插值法', '抛物线牛顿插值法', 'Location', 'northwest');
title('插值函数曲线');
```