Friday, March 24, 2023

Lagrange interpolation method of curve fitting Using Matlab

 Lagrange interpolation

Lagrange interpolation is a method of curve fitting that involves finding a polynomial function that passes through a set of given data points. The function is constructed in a way that it satisfies the condition that it passes through all the given data points.

The method of Lagrange interpolation involves first defining a set of n data points (x_1, y_1), (x_2, y_2), ..., (x_n, y_n), where x_i and y_i are the ith coordinate of the data point. Then, a polynomial function of degree n-1 is constructed as follows:

P(x) = y_1L_1(x) + y_2L_2(x) + ... + y_n*L_n(x)

where L_i(x) is the ith Lagrange basis polynomial defined as:

L_i(x) = (x-x_1)(x-x_2)...(x-x_{i-1})(x-x_{i+1})...(x-x_n) / [(x_i-x_1)(x_i-x_2)...(x_i-x_{i-1})(x_i-x_{i+1})...(x_i-x_n)]

The Lagrange basis polynomials have the property that they take the value 1 at their corresponding data point and 0 at all other data points. This ensures that the polynomial function P(x) passes through all the data points.

Once the polynomial function P(x) is constructed using the Lagrange interpolation method, it can be used to estimate the value of the function at any point within the range of the given data points. However, it is important to note that Lagrange interpolation can result in oscillations or instabilities if the data points are not well-distributed or if the degree of the polynomial is too high. Therefore, caution must be exercised when using this method. A more detailed mathematical interpretation is shown as follows:


Now let us consider an example. For simplicity, we will use Matlab for simplification.








Solution. Here is the Matlab code for solving this problem.

===========================================================

% Lagrange method of interpolation
syms x; % Deine a varaible x as symbolic representation
disp(['General formula of 3rd order polynomial using Lagrange method: ' ...
'y=L0(x0)*f(x0)+L1(x1)*f(x1)+L2(x2)*f(x2)+L3(x3)*f(x3)'])
A=[0 1 2 3 4];
B=[0 1 8 27 64];
% define the Lagrange coefficients
L0=(((x-A(2))/(A(1)-A(2)))*((x-A(3))/(A(1)-A(3)))*((x-A(4))/(A(1)-A(4))));
L1=(((x-A(1))/(A(2)-A(1)))*((x-A(3))/(A(2)-A(3)))*((x-A(4))/(A(2)-A(4))));
L2=(((x-A(1))/(A(3)-A(1)))*((x-A(2))/(A(3)-A(2)))*((x-A(4))/(A(3)-A(4))));
L3=(((x-A(1))/(A(4)-A(1)))*((x-A(2))/(A(4)-A(2)))*((x-A(3))/(A(4)-A(3))));
% Define the function
f=L0*B(1)+L1*B(2)+L2*B(3)+L3*B(4)
y=simplify(f)
% For example solve for x=1.5
x_cal=1.5
fx = eval(subs(y,x,x_cal));
fprintf('\nThe value of f(x) is: %f\n', fx);

Here is the result of the computation.

=====================================================


Lagrange_interpolation_3rd_order
The general formula of 3rd order polynomial using the Lagrange method:
y=L0(x0)*f(x0)+L1(x1)*f(x1)+L2(x2)*f(x2)+L3(x3)*f(x3)

f =

9*x*(x/2 - 1/2)*(x - 2) - 4*x*(x - 1)*(x - 3) + x*(x/2 - 3/2)*(x - 2)
y =
x^3

x_cal =
1.5000

The value of f(x) is: 3.375000


From the result, it is possible to fit with respect to function f, or which can be simplified as
function y. f and y have the same value. Like x=1.5, we can find y(x) for any value of x.

No comments:

Post a Comment