求拉格朗日插值某点的值

老师上课的代码,已经将图像画出,现在需要求x=0.6时y的值
clear all; close all; clc;

xlist = pi * (0: 0.01 : 1);
x1 = 0 ; y1 = 0 ;
x2 = pi/2; y2 = 1;
x3 = pi; y3 = 0;
x4 = pi/4; y4 = sin(x4);
x5 = 3*pi/4; y5 = sin(x5);

ylist2 = 0* xlist;
for s = 1 : length(xlist)
x = xlist(s);
ylist2(s) = y1 * (x-x2)(x-x3)(x-x4)(x-x5)/((x1-x2)(x1-x3)(x1-x4)(x1-x5))...
+ y2 * (x-x1)(x-x3)(x-x4)(x-x5)/((x2-x1)(x2-x3)(x2-x4)(x2-x5))...
+ y3 * (x-x1)(x-x2)(x-x4)(x-x5)/((x3-x1)(x3-x2)(x3-x4)(x3-x5))...
+ y4 * (x-x1)(x-x2)(x-x3)(x-x5)/((x4-x1)(x4-x2)(x4-x3)(x4-x5))...
+ y5 * (x-x1)(x-x2)(x-x3)(x-x4)/((x5-x1)(x5-x2)(x5-x4)(x5-x3));
end
plot( xlist/pi, ylist2,'--')
xlabel('x/pi')

就是0.6 对应位置的ylist2的值,改成x=0.6,算一下 y=0.56404

img

同学不知道这个实例【拉格朗日插值求具体值点】的思路,是否能帮助到你:https://blog.csdn.net/weixin_53026957/article/details/127246020
【实例以公式、图像、代码释义去讲解】

img

拉格朗日插值求具体值点
希望对你有所帮助,望采纳
https://blog.csdn.net/weixin_53026957/article/details/127246020

代码:

x1 = 0 ; y1 = 0 ;
x2 = pi/2; y2 = 1;
x3 = pi; y3 = 0;
x4 = pi/4; y4 = sin(x4);
x5 = 3*pi/4; y5 = sin(x5);
x = 0.6;
y = y1 * (x-x2)*(x-x3)*(x-x4)*(x-x5)/((x1-x2)*(x1-x3)*(x1-x4)*(x1-x5))...
+ y2 * (x-x1)*(x-x3)*(x-x4)*(x-x5)/((x2-x1)*(x2-x3)*(x2-x4)*(x2-x5))...
+ y3 * (x-x1)*(x-x2)*(x-x4)*(x-x5)/((x3-x1)*(x3-x2)*(x3-x4)*(x3-x5))...
+ y4 * (x-x1)*(x-x2)*(x-x3)*(x-x5)/((x4-x1)*(x4-x2)*(x4-x3)*(x4-x5))...
+ y5 * (x-x1)*(x-x2)*(x-x3)*(x-x4)/((x5-x1)*(x5-x2)*(x5-x4)*(x5-x3))

结果:
y=0.564037797512611

img