n = length(x)
m = length(xh)
yh = zeros(1,m)
c1 = ones(n-1,1)
c2 = ones(1,m)
for i=1:n
xp = x([1:i-1 i+1:n])
yh = yh + y(i)*prod((c1*xh-xp'*c2)./(x(i)-xp'*c2))
end
注:该程序只可一次计算实现一个插值计算。可实现多个插值计算的程序如下:
function yh=lagrange(x,y,xh)
n = length(x)
m = length(xh)
x = x(:)
y = y(:)
xh = xh(:)
yh = zeros(m,1)
c1 = ones(1,n-1)
c2 = ones(m,1)
for i=1:n,
xp = x([1:i-1 i+1:n])
yh = yh + y(i) * prod((xh*c1-c2*xp')./(c2*(x(i)*c1-xp')),2)
end
#include <stdio.h>double Lerp(double x0,double y0,double x1,double y1,double x)
{
double dy = y1 - y0
if(dy == 0){
printf("除0错误!\n")
return 0
}
return x * (x1 - x0) / dy
}
int main()
{
double x0,x1,y1,y0,x,y
printf("Inptu x0 y0 x1 y1 x:")
scanf("%lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x)
y = Lerp(x0,y0,x1,y1,x)
printf("y = %lf\n",y)
return 0
}
a=[3.946 05.193 505.762 1006.311 1506.795 2007.257 2507.704 3008.170 350...8.603 4009.073 4509.511 50010.001 55010.435 60010.932 65011.400 700...
11.938 75012.428 80012.995 85013.560 90014.246 95015.063 1000]
x=a(:,2)
y=a(:,1)
x2=1:60:1200
y2=interp1(x,y,x2, 'pchip')
plot(x,y,'bo',x2,y2,'r*')
legend('千分尺数据','分段插值数据','Location', 'Best')
grid on
xlabel('x 容量值(L)'),ylabel('y 电流值(mA)')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)