拉格朗日插值matlab代码
时间: 2023-11-25 22:45:53 浏览: 115
拉格朗日插值的matlab代码
以下是拉格朗日插值的Matlab代码实现:
```
function [y_interp] = lagrange_interp(x,,x_interp)
% input:
% x: vector of x values
% y: vector of y values corresponding to x
% x_interp: vector of x values to be interpolated at
% output:
% y_interp: vector of interpolated y values at x_interp
n = length(x); % number of data points
y_interp = zeros(size(x_interp)); % initialize y_interp
for i=1:n
% calculate the Lagrange basis polynomial for this data point
L = ones(size(x_interp));
for j=1:n
if j~=i
L = L.*(x_interp-x(j))/(x(i)-x(j));
end
end
% add the contribution of this data point to y_interp
y_interp = y_interp + y(i)*L;
end
end
```
阅读全文