matlab数值分析程序--高等数学,数值代数的matlab实现-文字版, matlab电子书, 和matlab 有关的电子书:

6.7.7 几种作三次样条有关图像的MATLAB程序

6.7.7  几种作三次样条有关图像的MATLAB程序

求有关分段三次样条图形的MATLAB主程序

(一)限定端点约束条件的作图程序

function S=splinetx(x0,y0,xj,x,y,dy1,dyn)

S = spline(x0,[dy1,y0,dyn],xj);

Sn = spline(x0,[dy1,y0,dyn],x);

 plot(x0,y0,'o',x,Sn,'-',xj,S,'*',x,y,'-.')

legend('节点(xi,yi)', '分段三次样条函数','插值点(x,S)','被插值函数y')

 

(二)不限定端点约束条件的作图程序

function S=splinetx1(x0,y0,xi,x,y)

S= interp1(x0,y0,xi, 'spline'); Sn= interp1(x0,y0,x, 'spline');

 plot(x0,y0,'o',x,Sn,'-',xi,S,'*',x,y,'-.')

legend('节点(xi,yi)', '分段三次样条函数','插值点(x,S)','被插值函数y')

 

(三)自由作图程序

直接在MATLAB工作窗口编程序,例如,

>>subplot(2,2,1),x1=-8:4/3:-4,c1=sin(x1);xx1 = -8:0.1:-4;

pp1 = interp1 (x1,c1,xx1,'spline ');

cc1 =sin(xx1);%pp1 = spline (x1,c1,xx1);

plot(x1,c1,'bo',xx1,pp1,'k-',xx1,cc1,'r-.')

subplot(2,2,2)

x2=-4:4/3:-0;c2=sin(x2); xx2 = -4:0.1:0;

pp2 = interp1 (x2,c2,xx2,'spline ');

cc2=sin(xx2);plot(x2,c2,'bo',xx2,pp2,'k-',xx2,cc2,'r-.')

title('y=sinx及其三次样条插值函数,节点(xi,yi)的图形')

subplot(2,1,2)

x=-8:4/3:8;c=sin(x);xx = -8:0.1:8;

pp = spline(x,c,xx);

cc=sin(xx); plot(x,c,'bo',xx,pp,'k-',xx,cc,'r-.')

legend('节点(xi,yi)','三次样条插值函数','y=sinx 的函数')

或       >> subplot(2,2,1),x1=-8:4/3:-4,c1=cos(x1);xx1 = -8:0.1:-4;

Y1=interp1(x1,c1,xx1, 'pchip');

pp1 = interp1 (x1,c1,xx1,'spline ');

cc1 =cos(xx1);

    plot(x1,c1,'bo',xx1,pp1,'k-',xx1,Y1,'r:',xx1,cc1,'g-.')

    subplot(2,2,2)

x2=-4:4/3:-0;c2= cos(x2); xx2 = -4:0.1:0;

pp2 = interp1 (x2,c2,xx2,'spline ');

Y2=interp1(x2,c2,xx2, 'pchip');cc2=cos(xx2);    plot(x2,c2,'bo',xx2,pp2,'k-',xx2,Y2,'r:',xx2,cc2,'g-.')

title('y=cosx及其三次样条插值函数,分段三次埃尔米特插值, 节点(xi,yi)的图形')

    subplot(2,1,2)

    x=-8:4/3:8;c= cos(x);xx = -8:0.1:8;

    pp = spline(x,c,xx);Y=interp1(x,c,xx, 'pchip');

    cc= cos(xx); plot(x,c,'bo',xx,pp,'k-',xx,Y,'r:',xx,cc,'g-.')

legend('节点(xi,yi)','三次样条插值函数','分段三次埃尔米特插值','y=cosx 的函数')

或       >>n=7,h=2*pi/n; x=(-2*pi+h)/2:h:(2*pi-h)/2;

y= tan(cos((3^(1/2)+sin(2*x))./(3+4*x.^2))); x0=-pi:h:pi;X=-pi:h/12:pi;

y0= tan(cos((3^(1/2)+sin(2*x0))./(3+4*x0.^2)));

Y= tan(cos((3^(1/2)+sin(2*X))./(3+4*X.^2)));

YL=lagr1(x0,y0,X); YS=interp1(x0,y0,X,'spline');

YH=interp1(x0,y0,X, 'pchip'); yL=lagr1(x0,y0,x);

yX=interp1(x0,y0,x); yS=interp1(x0,y0,x,'spline');

yH=interp1(x0,y0,x, 'pchip'); RL=abs((y-yL)./y);RS=abs((y-yS)./y);

RH=abs((y-yH)./y); RX=abs((y-yX)./y);RLj=abs(y-yL);mRLj=mean(RLj);

RSj=abs(y-yS);

mRSj=mean(RSj);RHj=abs(y-yH);RXj=abs(y-yX);mRHj=mean(RHj);

mRXj=mean(RXj);mRL=mean(RL);mRX=mean(RX);

mRS=mean(RS);mRH=mean(RH);

CZ=[x' y' yL' yX' yS' yH'],R=[x' RL' RX' RS' RH'],

mR=[mRL' mRX' mRS' mRH']

Rj=[x' RLj' RXj' RSj' RHj'],mRj=[mRLj' mRXj' mRSj' mRHj'],

plot(x0,y0,'bo',X,Y,'k-',X,YS,'rp', X,YH,'g>'),

legend('节点','被插值函数','三次样条函数','分段埃尔米特插值函数')

 

例6.7.9  设函数定义在区间上,节点(X(i),(X (i)))的横坐标向量X的元素是首项a=-5,末项b=5,公差h=1.5的等差数列,构造分段三次样条函数.把区间分成20等份,构成20个小区间,用限定端点约束条件和不限定端点约束条件的MATLAB程序计算各小区间中点的值,并作出节点,插值点,的图形,并与分段埃尔米特插值函数的图形比较.

解 记节点的横坐标插值点.

(1) 不限定端点约束条件,在MATLAB工作窗口输入程序

>>x0=-5:1.5:5; y0=1./(1+x0.^2); x1=-4.75:0.5:4.75; x=-5:0.001:5;

y=1./(1+x.^2); S= splinetx1(x0,y0,x1,x,y)

title(' y=1/(1+x^2)及其三次样条插值函数,节点和插值点的图形')

运行后屏幕显示各小区间中点的值,作出节点,插值点,的图形(略).

(2)限定端点约束条件,取,在MATLAB工作窗口输入程序

>>x0=-5:1.5:5;y0=1./(1+x0.^2);x1=-4.75:0.5:4.75;

x=-5:0.001:5;

y=1./(1+x.^2); S= splinetx (x0,y0,x1,x,y,0,0)

title(' y=1/(1+x^2)及其分段压紧三次样条函数,节点和插值点的图形')

运行后屏幕显示各小区间中点的值(略),作出节点,插值点,的图形(略).

如果调节端点约束条件或者增加节点的倍数,例如,在MATLAB工作窗口输入程序

>> x0=-5:0.5:5; y0=1./(1+x0.^2); x1=-4.75:0.5:4.75; x=-5:0.001:5;

y=1./(1+x.^2); S= splinetx (x0,y0,x1,x,y,0,0)

title(' y=1/(1+x^2)及其增加节点后的不限定端点约束条件的三次样条函数,节点和插值点的图形')

则运行后输出的图形中的三次样条函数与被插值函数的图像基本重合.

 

例6.7.10  设函数定义在区间上,取,按等距节点构造分段三次样条函数,用MATLAB程序计算各小区间中点的值,分别作出局部和整体区间上的节点,插值点,,三次样条函数和分段三次埃尔米特插值函数的图形,并进行比较.

 

解  编写并保存名为sanci.m的M文件如下

subplot(2,2,1)

h=4*pi/7; x1=-2*pi:h:-2*pi+3*h;c1=0.5.*x1-cos(x1);

 xx1 =-2*pi+4*pi/14:h:-2*pi+pi/11+2*h; X1=-2*pi:0.001:-2*pi+3*h;

Y1=interp1(x1,c1,xx1, 'pchip'); YY1=interp1(x1,c1,X1, 'pchip');

pp1 = interp1 (x1,c1,xx1,'spline ');

P1 = interp1 (x1,c1,X1,'spline ');

cc1 =0.5.*X1-cos(X1);

plot(x1,c1,'bo',xx1,pp1,'k*',X1,P1,'k-',xx1,Y1,'rx',X1,

YY1,'r:',X1,cc1,'g-.')

subplot(2,2,2)

x2=2*pi-3*h:h:2*pi;c2=0.5.*x2-cos(x2);

xx2 =2*pi-4*pi/14-2*h:h:2*pi-4*pi/14;

X2=2*pi-3*h:0.001:2*pi; pp2 = interp1 (x2,c2,xx2,

'spline ');

YY2=interp1(x2,c2, X2, 'pchip'); Y2=interp1(x2,c2,xx2, 'pchip');

P2= interp1 (x2,c2,X2,'spline '); cc2=0.5.*X2-cos(X2);   

plot(x2,c2,'bo',xx2,pp2,'k*',X2,P2,'k-',xx2,Y2,'rx',X2,

YY2,'r:',X2,cc2,'g-.')

title('y=0.5x-cos(x)及其三次样条函数,分段三次埃尔米特插值函数,节点和插值点的图形')

subplot(2,1,2)

x=-2*pi:h:2*pi;c=0.5.*x-cos(x); xx =-2*pi+4*pi/14:h:2*pi-4*pi/14;

pp = spline(x,c,xx),Y=interp1(x,c,xx, 'pchip'), X=-2*pi:0.001:2*pi;

P = interp1 (x,c,X,'spline '); YY=interp1(x,c,X, 'pchip'); cc=0.5.*X-cos(X);

plot(x,c,'bo',xx,pp,'k*',X,P,'k-',xx,Y,'rx',X,YY,'r:',X,cc,'g-.')

legend('节点(xi,yi)','三次样条插值','三次样条插值函数','分段三次埃尔米特插值','分段三次埃尔米特插值函数','y=0.5x-cosx 的函数')

在MATLAB工作窗口输入文件名

>> sanci

运行后屏幕显示各小区间中点处三次样条插值pp和分段三次埃尔米特插值Y,出现被插值函数,节点,三次样条和分段三次埃尔米特的函数及其插值点等图形(略)。

pp =-3.2181  -0.9609  -0.6824   -0.9476    1.1128  2.6295  2.1675

Y=-3.0085   -1.0074   -0.7589   -0.7872   1.1498   2.4072   2.3787

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/3/3060707.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1