当前位置:网站首页>Lagrange interpolation formula matlab implementation
Lagrange interpolation formula matlab implementation
2022-08-09 19:02:00 【Bubble diet】
一、The principle of formula derivation
NSubinterpolation basis functions:

Satisfy the interpolation polynomial

An interpolating polynomial of the form this formula is calledLagrang插值多项式.
由
的定义可知
If a count is introduced
,再求导
因此

二 、符号说明
输入:
xi:The abscissa of the known data point
k:函数lk(x)的下标k
xx:The abscissa of the point to be interpolated
输出:lk_x,即函数lk(x)在xxThe ordinate of the coordinate point
代码如下:
function li_x = LagrangeFactor( xi, i, xx )
w = 1;
n = length( xi );
syms x;
for j = 1 : n
w = w * ( x - xi(j) );
end
dw = diff( w );
dwf = matlabFunction( dw );
dwi = dwf( xi(i) );
lx = ( w / ( x - xi(i) ) ) / dwi;
f = matlabFunction( lx );
lk_x = f( xx );
end三、一次插值
1. Preparation of the argument function:
(xi,yi):are the known data point coordinates
代码:xi = [ 0, 1 ];
yi = sin( xi );
n = length( xi );
y = 0;
x = [ xi(1) - 1 : 0.1 : xi(2) + 1 ];2.根据lagrangeInterpolating polynomial calculationsxThe function value at the coordinate point(纵坐标)
代码:
for k = 1 : n
lkx = LagrangeFactor( xi, k, x );
y = y + yi(k) * lkx;
end3.绘图
代码:
figure;
plot( xi, yi, 'b.', 'markersize', 30 )
hold on
plot( x, sin(x), 'k--', 'LineWidth', 1.5 )
plot( x, y, 'r-', 'LineWidth', 2 )
legend( '插值点', '原曲线', 'Interpolate polynomial curves' );
axis( [ -1, 2, -1, 3 ] )结果如图:

四、抛物插值
1.Preparation of the argument function:
xi = [ 0, pi/2, pi ];
yi = [ 0, 1, 0 ];
n = length( xi );
y = 0;
x = [ xi(1) - 1 : 0.1 : xi(n) + 1 ];
2.根据lagrangeInterpolating polynomial calculationsxThe function value at the coordinate point
for k = 1 : n
lkx = LagrangeFactor( xi, k, x );
y = y + yi(i) * lkx;
end
3.绘图
plot( xi, yi, 'b.', 'markersize', 30 )
hold on
plot( x, sin(x), 'k--', 'LineWidth', 1.5 )
plot( x, y, 'r-', 'LineWidth', 2 )
legend( '插值点', '原曲线', 'Interpolate polynomial curves' );
axis( [ xi(1) - 1, xi(n) + 1, -1, 2 ] )

汇总代码:
clear all
clc
%% 一次插值
%(xi,yi):
xi = [ 0, 1 ];
yi = sin( xi );
n = length( xi );
y = 0;
x = [ xi(1) - 1 : 0.1 : xi(2) + 1 ];
for k = 1 : n
lkx = LagrangeFactor( xi, k, x );
y = y + yi(k) * lkx;
end
%y
figure;
plot( xi, yi, 'b.', 'markersize', 30 )
hold on
plot( x, sin(x), 'k--', 'LineWidth', 1.5 )
plot( x, y, 'r-', 'LineWidth', 2 )
legend( '插值点', '原曲线', 'Interpolate polynomial curves' );
axis( [ -1, 2, -1, 3 ] )
%% 抛物插值
clear all
clc
xi = [ 0, pi/2, pi ];
yi = [ 0, 1, 0 ];
n = length( xi );
y = 0;
x = [ xi(1) - 1 : 0.1 : xi(n) + 1 ];
for k = 1 : n
lkx = LagrangeFactor( xi, k, x );
y = y + yi(k) * lkx;
end
%y
figure;
plot( xi, yi, 'b.', 'markersize', 30 )
hold on
plot( x, sin(x), 'k--', 'LineWidth', 1.5 )
plot( x, y, 'r-', 'LineWidth', 2 )
legend( '插值点', '原曲线', 'Interpolate polynomial curves' );
axis( [ xi(1) - 1, xi(n) + 1, -1, 2 ] )
function lk_x = LagrangeFactor( xi, k, xx )
w = 1;
n = length( xi );
syms x;
for j = 1 : n
w = w * ( x - xi(j) );
end
dw = diff( w );
dwf = matlabFunction( dw );
dwi = dwf( xi(k) );
lx = ( w / ( x - xi(k) ) ) / dwi;
f = matlabFunction( lx );
lk_x = f( xx );
end边栏推荐
- A49 - ESP8266建立AP传输XPT2046AD数据WIFI模块
- 嵌入式软件开发的特点和流程
- 关于聊天机器人,跨境电商人必须知道这些…
- 期货开户流程和手续费如何调整
- CPU占用过高问题的排查
- Numpy数组索引/切片 多维度索引
- SQL trill interview: send you a universal template, to?(key, each user to log on to the maximum number of consecutive monthly)
- PGSQL backup tool, which is better?
- CocosCreator accesses WeChat mini-games
- Reasons for slow startup of IDEA (1)
猜你喜欢
随机推荐
快捷键修改typora字体----自制脚本
Using Prometheus skillfully to extend the kubernetes scheduler
MySQL 5.5系列安装步骤教程(图解版)
margin:auto实现盒子水平垂直居中
ffmpeg通过rtsp获取h264码流
Selenium的安装
1.1、VIFB: A Visible and Infrared Image Fusion Benchmark(一个可见光与红外图像融合Benchmark)文章阅读
The article details of the qiucode.cn website realize the code block can be copied by clicking the button
视频聊天源码——一对一直播如何提高直播质量?
.NET 6学习笔记(4)——解决VS2022中Nullable警告
Functions and Features of Smart Home Control System
使用SourceTree添加SSH公钥并克隆码云项目(笔记整理篇)
如何让button中的内容分两行显示
LeetCode 131.分割回文串
A40 - 基于51单片机的GSM模块优化设计
CocosCreator accesses WeChat mini-games
MySQL 5.5 series installation steps tutorial (graphical version)
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
2022-08-09日报:做学术OR去公司 ? 想通这点,治好 AI 打工人的精神内耗
智能家居控制系统的功能和特点

![QuickSort(快速排序)&&MergeSort(归并排序)的效率比较[搭配LeetCode例题]](/img/9c/77fe8b00bb1fb0e968ac1e440922e8.png)







![[ Kitex 源码解读 ] 请求重试](/img/d9/c1871c15cc9124e919d22c9adcc75b.png)