1、教材76页例题:用三弯矩法求三次样条插值函数 s(x)。all clear;A=0 01 0.52 2.03 1.5;B=0 0.23 -1;m,n=size(A);for i=1:(m-1)h(i)=A(i+1,1)-A(i,1);f(i)=(A(i+1,2)-A(i,2)/h(i);endfor i=1:(m-2)u(i)=h(i)/(h(i)+h(i+1);l(i+1)=1-u(i);d(i+1)=6*(f(i+1)-f(i)/(h(i)+h(i+1);endu(m-1)=1;l(1)=1;d(1)=6*(f(1)-B(1,2)/h(1);d(m)=6*(B(2,2)-f(m-1)/h(
2、m-1);C=2*eye(m);for i=1:(m-1)C(i,i+1)=l(i);C(i+1,i)=u(i);enddisp(三弯矩方程组的系数矩阵:);Cdisp(常数向量:);d=dC1=C,d;w(1)=C1(1,1);y(1)=d(1)/w(1);for i=2:mr(i-1)=l(i-1)/w(i-1);w(i)=C1(i,i)-u(i-1)*r(i-1);y(i)=(d(i)-u(i-1)*y(i-1)/w(i);endM(m)=y(m);for i=(m-1):1M(i)=y(i)-r(i)*M(i+1);enddisp(三弯矩方程组的解:);Msyms x;for i=1:
3、m-1disp(区间: );A(i,1),A(i+1,1)disp(s(x)表达式:)M(i)*(A(i+1,1)-x)3/(6*h(i)+M(i+1)*(x-A(i,1)3/(6*h(i)+(A(i,2)-M(i)*h(i)2/6)*(A(i+1,1)-x)/h(i)+(A(i+1,2)-M(i+1)*h(i)2/6)*(x-A(i,1)/h(i)end运行结果:三弯矩方程组的系数矩阵:C =2.0000 1.0000 0 00.5000 2.0000 0.5000 00 0.5000 2.0000 0.50000 0 1.0000 2.0000常数向量:d =1.80003.0000-6.0000-3.0000三弯矩方程组的解:ans =-0.36002.5200-3.72000.3600区间:ans =0 1s(x)表达式:ans =-3/50*(1-x)3+21/50*x3+3/50+1/50*x区间:ans =1 2s(x)表达式:ans =21/50*(2-x)3-31/50*(x-1)3-123/50+127/50*x区间:ans =2 3s(x)表达式:ans =-31/50*(3-x)3+3/50*(x-2)3+249/50-59/50*x