Lagrange 插值多项式积分求圆周率近似
摘要:
公式1:y1=4/(1+x^2) 公式2:y2=4*sqrt(1-x^2) 分别对公式1、公式2求其拉格朗日插值多项式,再对其求0-1上的定积分来求圆周率π的近似值,并在Matlab 中通过画图来比较两个所求得的值与真实值π的偏差。
Lagrange 插值多项式:
)()(l )(L 0i n i n
i x f x x ∑==
其中 )())(())(()
())(())(()(l 11101110i n i i i i i i i n i i x x x x x x x x x x x x x x x x x x x x x -??--??---??--??--=+-+-
)(i x f 为函数在i x 处的函数值,)(x L n 为Lagrange 插值多项式。
Matlab 实现:
clc;clear;
a=0;b=1;
n=input('Enter a number n:'); %将0-1分割成n 节点,即n-1段 X=zeros(1,n); %用来放置节点x 的值
P=zeros(1,n); %用来放置节点x 对应的函数值y1 Q=zeros(1,n); %用来放置节点x 对应的函数值y2 x=0;
h=(b-a)/(n-1); %h 为步长
for i=1:n
y1=4/(1+x^2);
y2=4*sqrt(1-x^2);
X(i)=x;
P(i)=y1;
Q(i)=y2;
x=x+h;