数学建模——插值与拟合3

代码:

function y=lagrange(x0,y0,x); 
n=length(x0);m=length(x); 
for i=1:m 
 z=x(i); 
 s=0.0; 
 for k=1:n 
 p=1.0; 
 for j=1:n 
 if j~=k 
 p=p*(z-x0(j))/(x0(k)-x0(j)); 
 end 
 end 
 s=p*y0(k)+s; 
 end 
 y(i)=s; 
end
n=length(x0);m=length(x); 
for k=1:m 
 yy=0.0; 
 for i=1:n 
 h=1.0; 
 a=0.0; 
 for j=1:n 
 if j~=i 
 h=h*((x(k)-x0(j))/(x0(i)-x0(j)))^2; 
 a=1/(x0(i)-x0(j))+a; 
 end 
 end 
 yy=yy+h*((x0(i)-x(k))*(2*a*y0(i)-y1(i))+y0(i)); 
 end 
 y(k)=yy; 
end
clear,clc 
x=100:100:500; 
y=100:100:400; 
z=[636 697 624 478 450 
 698 712 630 478 420 
-189- 
 680 674 598 412 400 
 662 626 552 334 310]; 
pp=csape({x,y},z') 
xi=100:10:500;yi=100:10:400 
cz1=fnval(pp,{xi,yi}) 
cz2=interp2(x,y,z,xi,yi','spline') 
[i,j]=find(cz1==max(max(cz1))) 
x=xi(i),y=yi(j),zmax=cz1(i,j)

少年不被楼层误,余生不羁尽自由。
加油,加油

你可能感兴趣的:(自我学习,自我学习)