function [ s ] = csfit(x,y,dx0,dxn ) n=length(x)-1; h=diff(x); d=diff(y)/h; a=h(2:n-1); b=2*(h(1:n-
function [ s ] = csfit(x,y,dx0,dxn ) n=length(x)-1; h=diff(x); d=diff(y)/h; a=h(2:n-1); b=2*(h(1:n-
各位大侠帮忙看下这程序好吗、我运行说有问题 x=[104.33,117.83,122.67,143.40,144.10,146.45,158.00,162.10,163.80,167.90];
y=[26.54183333,46.93670833,-18.27804167,0,-64.40058333,-242.3367083,29.7374375,0,19.0515,-213.8815833];
dx0=0;dxn=0;
s=csfit(x,y,dx0,dxn)
Attempted to access u(1); index out of bounds because numel(u)=0.
Error in ==> csfit at 10
u(1)=u(1)-3*(d(1)-dx0);
function [ s ] = csfit(x,y,dx0,dxn )
n=length(x)-1;
h=diff(x);
d=diff(y)/h;
a=h(2:n-1);
b=2*(h(1:n-1)+h(2:n));
c=h(2:n);
u=6*diff(d);
b(1)=b(1)-h(1)/2;
u(1)=u(1)-3*(d(1)-dx0);
b(n-1)=b(n-1)-h(n)/2;
u(n-1)=u(n-1)-3*(dxn-d(n));
for k=2:n-1
temp=a(k-1)/b(k-1);
b(k)=b(k)-temp*c(k-1);
u(k)=u(k)-temp*u(k-1);
end
m(n)=u(n-1)/b(n-1);
for k=n-2:-1:1
m(k+1)=(u(k)-c(k)*m(k+2))/b(k);
end
m(1)=3*(d(1)-dx0)/h(1)-m(2)/2;
m(n+1)=3*(dxn-d(n))/h(n)-m(n)/2;
for k=0:n-1
S(k+1,1)=(m(k+2)-m(k+1))/(6*h(k+1));
S(k+1,2)=m(k+1)/2;
S(k+1,3)=d(k+1)-h(k+1)*(2*m(k+1)+m(k+2))/6;
S(k+1,4)=y(k+1);
end
end
大小写的问题
有两种改法
第一:S=csfit(x,y,dx0,dxn) ->s=csfit(x,y,dx0,dxn)
将大写的S改成小写的s
第二:在程序的最后加上这句 S=s;
这样程序就没有问题了