当前位置:网站首页>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;
end
3.绘图
代码:
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
边栏推荐
猜你喜欢
面试官:Redis 大 key 要如何处理?
ffmpeg通过rtsp获取h264码流
Video chat source code - how to improve the quality of one-to-one live broadcast?
QuickSort(快速排序)&&MergeSort(归并排序)的效率比较[搭配LeetCode例题]
August 9, 2022: Build .NET apps in C# -- use the Visual Studio Code debugger to interactively debug .NET apps (won't, fail)
智慧灯杆网关智慧交通应用
B45 - 基于STM32单片机的家庭防火防盗系统的设计
期货开户交易所的手续费和查询方法
什么是控制板定制开发?
智能工具管理系统
随机推荐
[1413. Stepwise summation to get the minimum value of positive numbers]
Sigrity PowerSI Characteristic Impedance and Coupling Simulation
B019 - 甲醛甲烷煤气温湿度时间测试仪
电子产品硬件开发中存在的问题
B024 – STM32温湿度控制体温检测烟雾报警系统
B50 - 基于51单片机的儿童成长管理系统
[Server data recovery] Data recovery case of file system data loss caused by SAN LUN mapping error
A42 - 基于51单片机的洗衣机设计
Codeforces Round #808 (Div. 2)||Precipitation
Collection of DP Optimization Methods
TMin - whether TMin overflows
Volatile:JVM 我警告你,我的人你别乱动
快捷键修改typora字体----自制脚本
微信开发者工具程序开发好后,不报错,但是黑屏「建议收藏」
Numpy数组索引/切片 多维度索引
打印星型图「建议收藏」
【开源教程4】疯壳·开源编队无人机-OPENMV 脚本烧写
Knowledge Bits - How to Write a Project Summary
The article details of the qiucode.cn website realize the code block can be copied by clicking the button
Jenkins使用pipeline部署服务到远程服务器