lagrange interpolation

1. interpolation function

%% n order interpolation polynomial
%% X is an interpolation nodes, n being the number of interpolation polynomial, if the function expression is known attribute is zero, but unknown function expression is a known function of the value
function IPn = Interpolation_polynomials_of_degree_n(X,Y,precision,attribute)
global MAX;global m;global n;global i;
X = sort(X);
[m,n] = size(X);MAX = max([m,n]);error = [];
if attribute == 0
    F = ones(1,MAX);
    for i = 1:MAX
        F(i) = subs(Y,X(i));
    end
    sum = 0;
    for i = 1:MAX
        sum = sum+F(i)*Interpolation_basis_fun(X,i-1);
    end
    IPn = vpa(collect(sum),4);
    for i = 1:MAX
        error(i) = abs(F(i)-subs(sum,X(i)));
    end
    
    %% mapping
    h=figure;
    set(h,'color','w');
    t = min(X):(max(X)-min(X))/precision:max(X);
    Yreal = subs(Y,t);
    T = subs(sum,t);
    plot(t,Yreal,'b',t,T,'g',X,F,'r*');
    grid on
    title ( 'Lagrange');
    xlabel('Variable x');
    ylabel ( 'Variable y');
    legend ( 'Yreal: real image', 'T: fitting polynomial image', 'F: actual data');
    
    %% display coordinates
    for i = 1:MAX
        text(X(i),F(i),['(',num2str(X(i)),',',num2str(F(i)),')'],'color',[0.02 0.79 0.99]);
    end
     disp ( 'error value'); error

elseif attribute == 1
    sum = 0;
    for i = 1:MAX
        sum = sum+Y(i)*Interpolation_basis_fun(X,i-1);
    end
    IPn = vpa(collect(sum),4);
    
    h=figure;
    set(h,'color','w');
    t = min(X):(max(X)-min(X))/precision:max(X);
    T = subs(sum,t);
    plot(X,Y,'g*',t,T,'b');
    grid on
    title ( 'Lagrange');
    xlabel('Variable x');
    ylabel ( 'Variable y');
    legend ( 'Y: known data', 'T: fitting polynomial image ");
    for i = 1:MAX
        text(X(i),Y(i),['(',num2str(X(i)),',',num2str(Y(i)),')'],'color',[0.02 0.79 0.99]);
    end
end
end

  2. Basis Functions

Interpolation basis functions %%
function IBF = Interpolation_basis_fun(X,k)
[m,n] = size(X);MAX = max([m,n]);
X = sort(X);
mult_x = 1; mult_v = 1;
for i = 1:MAX
    syms x;
    if i ~= k+1
        mult_v = mult_v * (X (k + 1) X (i));
        mult_x = mult_x*(x-X(i));
    end
end
IBF = mult_x / mult_v;
end

  3. The interpolation remainder of error bounds

%% interpolation remainder of the margin of error (the remainder calculated only known function expression)
function MI = More_than_the_interpolation(X,f,xi,precision)
X = sort(X);
a = min(X);b = max(X);
disp ( 'xi interval should be the following:');
[a,b]
[m,n] = size(X);MAX = max([m,n]);
Df = diff(f,MAX);Df_value = subs(Df,xi);
MI = vpa(collect(Df_value*omiga(X)/factorial(MAX)),4);

%% error limit
Df_max = max(subs(Df,X));
R_x = vpa(collect(Df_max*abs(omiga(X))/factorial(MAX)),4);
DISP ( 'error limits:');
R_x

%% plot area
t = a:(b-a)/precision:b;
T1 = subs(R_x,t);
T2 = subs(MI,t);
h=figure;
set(h,'color','w');
plot(t,T1,'r--',t,T2,'g');
    grid on
    title ( 'error image');
    xlabel('Variable x');
    ylabel ( 'Variable y');
    legend ( 'T1: the upper limit of errors', 'T2: specified error limit');

    I = Factorial function (n)
        if n == 0
            I = 1;
        else
            Factorial I = (n-1) * n;
        end
    end
end

  4. continually multiply polynomials

function ox = omiga(X)
[m,n] = size(X);MAX = max([m,n]);
syms x;
More = 1;
for i = 1:MAX
    * = more than (xx (i));
end
ox = more;
end

  5. Examples

clear all
clc
precision=500;
X=1:1:9;
R1=reshape(rand(9),1,9^2);
R2=reshape(rand(18),1,18^2);
R=zeros(1,9);
for i=1:9
    R(i)=R1(9*i)*R2(18*i)*100;
end

%% known function
DISP ( 'known function expression');
syms x;
f=x*exp(-x^2)+log(abs(exp(x)+precision*sin(x)));
Interpolation_polynomials_of_degree_n(X,f,precision,0)

%% known function values
DISP ( 'known function value');
Interpolation_polynomials_of_degree_n(X,R,precision,1)

  result

Expression of known function
Error value
error =
     0     0     0     0     0     0     0     0     0
years =
1.621e-5*x^8 + 0.002542*x^7 - 0.1033*x^6 + 1.566*x^5 - 12.15*x^4 + 52.22*x^3 - 122.5*x^2 + 141.6*x - 54.27
Known function values
years =
- 0.06151*x^8 + 2.428*x^7 - 40.08*x^6 + 359.3*x^5 - 1899.0*x^4 + 6000.0*x^3 - 10950.0*x^2 + 10420.0*x - 3849.0

  Image below

Guess you like

Origin www.cnblogs.com/guliangt/p/12112802.html