MATLAB realizes the function of linear interpolation interp1

1. About interpolation

Interpolation is based on a known data sequence (which can also be understood as a series of discrete points in coordinates) to find the law; then according to the found law, it estimates the value of the points that have not yet been recorded.

2. About linear interpolation

Linear interpolation is an interpolation method for one-dimensional data. It estimates the value based on the two data points adjacent to the point to be interpolated in the one-dimensional data sequence. Of course, it is not the average of the data sizes of these two points (of course there are also cases of average), but the proportion of them is allocated according to the distance to these two points. For some edge points, extrapolation is also needed: that is, by finding the nearest two points, by establishing a one-dimensional linear equation between the two points, the corresponding y value can be obtained by bringing in x. Due to its simplicity, the following code does not use too many built-in functions that come with matlab.

Function description: x0, y0 are original unordered data, but the order of x0 and y0 is one-to-one correspondence, and yq is the linear interpolation that needs to correspond to x.

function yq = linear_me(x0,y0,x)
 len_x =length(x);
 a = [x0,y0];
yq = zeros(len_x,1);
for i=1:len_x
    % 初始化距离x0中距离x最近的个点
    min_pos = inf;
    min_next_pos =inf;
    min_neg = -inf;
    min_next_neg = -inf;
    for j=1:length(a)
        if x0(j)-x(i) >=0   % 此时x0在x的右边
            if min_pos > x0(j)-x(i)
                min_pos = x0(j)-x(i);% 找出距离x0中距离x(i)中右方最近的数
                pos_y = y0(j);
            end
        else                  % 此时x0在x的左边
            if min_neg < x0(j)-x(i) % 找出距离x0中距离x(i)中左方最近的数
                min_neg = x0(j)-x(i);
                neg_y = y0(j);
            end
        end
    end
    %% 内插
    if min_pos~=inf && min_neg~=-inf
        k = (pos_y-neg_y)/(min_pos-min_neg);
        yq(i) = pos_y-k*min_pos;
        
    else
        %% 外插
        % --------------右外插 ------------- %
        if min_pos == inf
            for j=1:length(a)
                if min_next_neg < x0(j)-x(i) && x0(j)-x(i) ~= min_neg % 找出距离x0中距离x(i)中左方第二近的数
                    min_next_neg = x0(j)-x(i);
                    neg_next_y = y0(j);
                end
            end
            k1 = (neg_y-neg_next_y)/(min_neg-min_next_neg);
            yq(i) = neg_y-k1*min_neg;
            % ----------右外插 ----------------- %
            % -----------左外插----------------- %
        else
            for j=1:length(a)
                if min_next_pos > x0(j)-x(i) && x0(j)-x(i) ~= min_pos % 找出距离x0中距离x(i)中左方第二近的数
                    min_next_pos = x0(j)-x(i);
                    pos_next_y = y0(j);
                end
            end
            k2 = (pos_y-pos_next_y)/(min_pos-min_next_pos);
            yq(i) = pos_y+k2*min_pos;
            % -----------左外插 --------------- %
        end
    end
end
end

To achieve the effect of the test:
write test.m script to test:

% test.m
a=load('data_input.txt');
x0=a(:,1);
y0=a(:,2);
x=(0:0.01:1)';
y=interp1(x0,y0,x,'linear','extrap');
yq=linear_me(x0,y0,x);
figure
subplot(1,3,1)
plot(x0,y0,'bp')
title("原始数据图")
subplot(1,3,2)
plot(x,y,'*')
title("使用interp1-linear插值")
subplot(1,3,3)
plot(x,yq,'ro')
title("自编函数插值")

The result is shown in the figure below, where x in data.input.txt is unordered data between 0-1. If necessary, please leave a message in the comment area.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45915507/article/details/110423627