Requirements:
Write Lagrangian interpolation function, the input is (x, f, order1)
where x represents node data, n*2 matrix
f represents whether or not to segment
order1: if segmented, order1 represents the order of the function, which can only be
If 1 or 2 is not segmented, the function order is n-1 and
must be written in strict accordance with the requirements, and the interpolation function graph is drawn.
Theory:
For many practical problems now, we do not know the specific form of f(x), the corresponding function value may be directly read out by measuring instruments or other equipment, f(x) is just a mathematical concept Function in the meaning. (For example: image processing, weather forecast, machine tool processing, etc.) The method of answering such questions is interpolation.
Taylor interpolation requires the derivative values of f(x) at the point x 0 to be provided. This requirement is very demanding. The expression of the function f(x) must be quite simple. If only the function value f(xi) = yi (i=0,1,2… ,n) on a series of nodes is given, then the interpolation problem can be expressed as follows: Find a polynomial of degree n (x) to satisfy the condition ( x)=, i = 0 ,1 ,…,n This is the so-called Lagrange interpolation.
Let the function y=f(x) be continuous on the interpolation interval [a,b], and take values, ,…, on n+1 different interpolation nodes a≤,..., ≤b. The purpose is to find a simple function P (x) in an interpolation function class Φ with excellent properties and easy calculation, which satisfies the interpolation condition P ()=yi (i=0,1,...,n), and at other points x≠up, as an approximation of f (x). The method of calculating the interpolation function P (x) is called interpolation. In this experiment, Lagrangian interpolation is used.
When n+1 points <<…< function values, ,…, are given, if you want to calculate the approximate value of the function value f (x) at x≠, you can first select two nodes -1 and let x∈[ -1, ], and then perform linear interpolation on the cells [-1, ], that is,
this kind of piecewise low-order interpolation is called piecewise linear interpolation, also called broken line interpolation.
Similarly, we can select the three nodes closest to x, -1, and +1, and then perform quadratic interpolation.
This kind of piecewise low-order interpolation is called piecewise quadratic interpolation, or piecewise parabolic interpolation.
matlab code:
function u = lagrange(x,f,order1)
syms fx; %fx表示需要估算的f(x)的x的值
[m,n] = size(x);
P = 0;
t=x(1,1):0.1:x(m,1);
if f==0 %不分段
for i = 1 : m
L1 = 1;
for j = 1 : m
if j ~=i
L1 = L1*(fx-x(j,1))/(x(i,1)-x(j,1));
end
end
sum = sum + L1*x(i,2);
end
u = subs(sum,fx,t);
plot(t,u);hold on;
plot(x(:,1),x(:,2),'*')
elseif f==1 %分段
if order1==1 %一阶线性插值
for i = 1:m-1 %分成n-1段
L1(i) = x(i,2)*(fx-x(i+1,1))/(x(i,1) - x(i+1,1)) + x(i+1,2)*(fx-x(i,1))/(x(i+1,1) - x(i,1));
end
for i = 1 : m-1
u = subs(L1(i),fx,t);
plot(t,u);hold on;
end
hold on;
plot(x(:,1),x(:,2),'*')
elseif order1==2 %二阶线性插值
for i = 1:m – 2
P = 0;
for k = i:i+2
L1 = 1;
for j =i:i+2
if j~=k
L1 = L1*(fx-x(j,1))/(x(k,1)-x(j,1));
end
end
sum = sum + L1*x(k,2);
end
L2(i) = P
end
L2(1) = x(1,1);
for i = 2: m-2
L2(i) =0.5*(x(i,1)+x(i+1,1));
end
i = i+1;
L2(i) = x(m,1);
for i = 1:m-2
t=L2(i):0.1:L2(i+1);
u = subs(L2(i),fx,t);
plot(t,u);hold on;
end
hold on;
plot(x(:,1),x(:,2),'*')
end
end
Program running result:
input: x=[3,6; 4,8; 5,60; 8,10; 11,100; 13,20]; ①No
segmentation: f=0; order1=1; ②Segment
1 First-order linear interpolation: f=1; order1=1;
③ Piecewise second-order linear interpolation: f=1; order1=2;