interpoler une fonction avec la methode de lagrange in matlab

Here's an example code on how to interpolate a function using the Lagrange method in MATLAB:

main.m
% Sample function to be interpolated
x = [-2, -1, 0, 1, 2];
y = [-6, -1, 0, 1, 6];

% Define new set of x values where interpolation is needed
x_new = linspace(min(x), max(x), 100);

% Compute interpolation
n = length(x);
y_new = zeros(size(x_new));
for i = 1:length(x_new)
    for j = 1:n
        L = ones(1,n);
        for k = 1:n
            if k ~= j
                L(k) = (x_new(i)-x(k))/(x(j)-x(k));
            end
        end
        y_new(i) = y_new(i) + y(j)*prod(L);
    end
end

% Plot original function and interpolated curve
plot(x, y, 'o', x_new, y_new)
legend('Original Function', 'Interpolated Curve')
620 chars
26 lines

The code starts by defining the original set of x and y values. The linspace function is then used to define a new set of x values where interpolation is needed. The interpolation is performed using a nested loop that calculates the Lagrange polynomial for each point in the x_new set. Finally, the original and interpolated curves are plotted for comparison using the plot function.

gistlibby LogSnag