《数值分析教学ppt课件.pptx》由会员分享,可在线阅读,更多相关《数值分析教学ppt课件.pptx(22页珍藏版)》请在三一办公上搜索。
1、数值试验题2,2.1 编制分段线性插值和分段三次Hermite插值程序,对被插函数 插值区间,分成10等分,求分段线性插值函数在各节点间中点处的值,并画出分段线性插值函数和 的图形。,下面6张是第1题,x=-4.5,-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5,4.5;f(x)ans=Columns 1 through 7 0.0486 0.0794 0.1500 0.3500 0.7500 0.7500 0.3500 Columns 8 through 10 0.1500 0.0794 0.0486,x=-5,-4,-3,-2,-1,0,1,2,3,4,5;y=0.
2、0385,0.0588,0.1000,0.2000,0.5000,1.0000,0.5000,0.2000,0.1000,0.0588,0.0385;plot(x,y),画图命令如下:,图形如下:,下面6张是第2题,function Sanciyangtiao(X,Y,a,b)%a=f(1),b=f(n)n=length(X);for i=1:n-1 h(i)=X(i+1)-X(i);end for j=2:n-1 u(j)=h(j-1)/(h(j-1)+h(j);t(j)=1-u(j);d(j)=6*(Y(j+1)-Y(j)/h(j)-(Y(j)-Y(j-1)/h(j-1)/(h(j-1)+
3、h(j);endt(1)=1;u(n)=1;d(1)=6/h(1)*(Y(2)-Y(1)/h(1)-a);d(n)=6/h(n-1)*(b-(Y(n)-Y(n-1)/h(n-1);for i=1:n A(i,i)=2;A(1,2)=1;A(n,n-1)=1;for j=2:n-1 A(j,j-1)=u(j);A(j,j+1)=t(j);if abs(i-j)1 A(i,j)=0;end endend,程序(1)(a),D=d;N=AD;M=N;for i=1:n-1 H(i)=(M(i+1)-M(i)/(6*h(i);I(i)=(M(i)*X(i+1)-M(i+1)*X(i)/(2*h(i);
4、J(i)=(M(i+1)*X(i)2-M(i)*X(i+1)2+2*(Y(i+1)-Y(i)+h(i)2/3*(M(i)-M(i+1)/(2*h(i);K(i)=(M(i)*X(i+1)3-M(i+1)*X(i)3+(6*Y(i)-M(i)*h(i)2)*X(i+1)-(6*Y(i+1)-M(i+1)*h(i)2)*X(i)/(6*h(i);endfor i=1:n-1 Q(i)=M(i)*(X(i+1)-(X(i)+X(i+1)/2)3/(6*h(i)+(M(i+1)*(X(i)+X(i+1)/2-X(i)3)/(6*h(i)+(Y(i)-M(i)*(h(i)2/6)*(X(i+1)-(X(
5、i)+X(i+1)/2)/h(i)+(Y(i+1)-M(i+1)*(h(i)2/6)*(X(i)+X(i+1)/2-X(i)/h(i);enddisp(Q)for i=1:n-1x=X(i):0.001:X(i+1);z=H(i)*x.3+I(i)*x.2+J(i)*x+K(i);plot(x,z,X,Y,*);hold onend,X=0 1 2 3 4 5 6 7 8 9 10;Y=0.0 0.79 1.53 2.19 2.71 3.03 3.27 2.89 3.06 3.19 3.29;sanciyangtiao8(X,Y,0.8,0.2)0.3986 1.1684 1.8715 2.4
6、782 2.8733 3.2137 3.0844 2.9199 3.1498 3.2223,function Sanciyangtiao(X,Y)n=length(X);for i=1:n-1 h(i)=X(i+1)-X(i);end for j=2:n-1 u(j)=h(j-1)/(h(j-1)+h(j);t(j)=1-u(j);d(j)=6*(Y(j+1)-Y(j)/h(j)-(Y(j)-Y(j-1)/h(j-1)/(h(j-1)+h(j);endt(1)=0;u(n)=0;d(1)=0;d(n)=0;for i=1:n A(i,i)=2;A(1,2)=0;A(n,n-1)=0;for j
7、=2:n-1 A(j,j-1)=u(j);A(j,j+1)=t(j);if abs(i-j)1 A(i,j)=0;end endend,D=d;N=AD;M=N;for i=1:n-1 H(i)=(M(i+1)-M(i)/(6*h(i);I(i)=(M(i)*X(i+1)-M(i+1)*X(i)/(2*h(i);J(i)=(M(i+1)*X(i)2-M(i)*X(i+1)2+2*(Y(i+1)-Y(i)+h(i)2/3*(M(i)-M(i+1)/(2*h(i);K(i)=(M(i)*X(i+1)3-M(i+1)*X(i)3+(6*Y(i)-M(i)*h(i)2)*X(i+1)-(6*Y(i+1
8、)-M(i+1)*h(i)2)*X(i)/(6*h(i);endfor i=1:n-1 Q(i)=M(i)*(X(i+1)-(X(i)+X(i+1)/2)3/(6*h(i)+(M(i+1)*(X(i)+X(i+1)/2-X(i)3)/(6*h(i)+(Y(i)-M(i)*(h(i)2/6)*(X(i+1)-(X(i)+X(i+1)/2)/h(i)+(Y(i+1)-M(i+1)*(h(i)2/6)*(X(i)+X(i+1)/2-X(i)/h(i);enddisp(Q)for i=1:n-1x=X(i):0.001:X(i+1);z=H(i)*x.3+I(i)*x.2+J(i)*x+K(i);pl
9、ot(x,z,X,Y,*);hold onend,X=0 1 2 3 4 5 6 7 8 9 10;Y=0.0 0.79 1.53 2.19 2.71 3.03 3.27 2.89 3.06 3.19 3.29;sanciyangtiao2(X,Y)0.3984 1.1685 1.8715 2.4782 2.8733 3.2138 3.0841 2.9209 3.1459 3.2368,下面5张是第三题,输入下面命令得拟合图形:fplot(0.5491+(-3.9683e-005)*x+(-2.9977)*x.2+1.9991*x.3,-1.0,2.0),输入如下命令得离散数据和拟合曲线的图形:x=-1.0,-0.5,0.0,0.5,1.0,1.5,2.0;y=-4.447,-0.452,0.551,0.048,-0.447,0.549,4.552;X=(-1.0:0.001:2.0);Y=0.5491+(-3.9683e-005)*X+(-2.9977)*X.2+1.9991*X.3;plot(X,Y,-,x,y,*),