function [yinterp] = lagrange(xinterp, x, y) % LAGRANGE performs Lagrange interpolation % % INPUTS: % xinterp: vector of x-values to interpolate at % x: vector of x-values of known data points % y: vector of y-values of known data points % % OUTPUTS: % yinterp: vector of interpolated y-values corresponding to xinterp

n = length(x);

% Compute the weights w = ones(n, 1); for j = 1:n for i = 1:n if i ~= j w(j) = w(j) * (x(j) - x(i)); end end w(j) = y(j) / w(j); end

% Evaluate the Lagrange interpolating polynomial at xinterp m = length(xinterp); yinterp = zeros(m, 1); for k = 1:m for j = 1:n p = 1; for i = 1:n if i ~= j p = p * (xinterp(k) - x(i)); end end yinterp(k) = yinterp(k) + w(j) * p; end end

end


原文地址: https://www.cveoy.top/t/topic/m1DT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录