Chebyshev多项式, Legendre多项式,与Chebyshev多项式零点插值

Chebyshev多项式

使用通项公式

function v = che_1(a,b,n)
syms x;
t = ((b-a)*x + a + b)/2;
v = expand(cos(n*acos(t)));

end

使用递推关系

function v = che_2(a,b,n)

syms x;

t = ((b-a)*x + a + b)/2;

t_0 = 1;
t_1 = t;

if n == 0
    v = 1;
else
    for index = 1:n-1
        temp = 2*t*t_1 - t_0;
        t_0 = t_1;
        t_1 = temp;
    end
    
    v = expand(t_1);
end
end

Legendre多项式

使用通项

function v = leg_1(a,b,n)

if n == 0
    v = 1;
else
    syms x;

    t = ((b-a)*x + a + b)/2;

    v = expand(diff((t*t - 1)^n,n)/(2^n)/factorial(n));
end

end

使用递推关系

function v = leg_2(a,b,n)

syms x;

t = ((b-a)*x + a + b)/2;

t_0 = 1;
t_1 = t;

if n == 0
    v = 1;
else
    for index = 2:n
        temp = expand(((2*index-1)*t*t_1 - (index-1)*t_0)/index);
        t_0 = t_1;
        t_1 = temp;
    end
    v = expand(t_1);
    
end

两个多项式在[-1,1]上的图像

clear;
u = -1:0.01:1;
set(gcf,'outerposition',get(0,'screensize'));
hold on;
for n = 0:10
    
    f = che_1(-1,1,n);    
    v = subs(f,u);
    plot(u,v,'k-');
end
hold off;
    

clear;
u = -1:0.01:1;
set(gcf,'outerposition',get(0,'screensize'));
hold on;
for n = 0:10    
    f = leg_1(-1,1,n);    
    v = subs(f,u);
    plot(u,v,'k-');
end
hold off;
    


Chebyshev零点插值与等距节点10次Lagrange插值对比

clear;
f=@(x) 1./(1+25*x.*x);

u= -1:0.01:1;
v=f(u);

x = -1:0.2:1;
y = f(x);
v1 = LagrangeInterp(x,y,u);

x_0 = 0:10;
x_0 = cos((21-2*x_0)*pi/22);
y_0 = f(x_0);
v2 = LagrangeInterp(x_0,y_0,u);


set(gcf,'outerposition',get(0,'screensize'));
plot(x,y,'ro',x_0,y_0,'bo',u,v1,'r--',u,v2,'b--',u,v,'k-');

Chebyshev零点插值可以避免Runge现象!!

你可能感兴趣的:(Matlab,Matlab,数值逼近,数值分析)