主要内容

如何构建样条曲线

此示例显示如何使用曲线拟合工具箱™中的样条函数以各种方式构建样条曲线。

插值

你可以构建一个cubic spline interpolant与以下站点的余弦功能匹配x, using thecsapicommand.

x = 2 * pi * [0 1.1:.2:.9];y = cos(x);cs = csapi(x,y);

然后,您可以通过使用查看插值样条fnplt..

fnplt(cs,2);轴([ -  1 7 -1.2 1.2])保持绘图(x,y,'o'抱紧关闭

Checking the Interpolant

The cosine function is 2*pi-periodic. How well does our cubic spline interpolant do in that regard? One way to check is to compute the difference in the first derivative at the two endpoints.

差异(fnval(fnder(cs),[0 2 * pi]))))
ans = -0.1375

执行周期性,使用csapeinstead ofcsapi.

csp = csape(x,y,'periodic');保持fnplt.(csp,'G'抱紧关闭

Now the check gives

diff( fnval( fnder(csp), [0 2*pi] ) )
ans = -2.2806e-17

即使是第二衍生物现在现在匹配在端点。

diff( fnval( fnder(csp, 2), [0 2*pi] ) )
ans = -2.2204e-16

The分段线性插值通过相同的数据可通过spapi. Here we add it to the previous plot, in red.

pl = spapi(2, x, y); holdfnplt(pl,'r',2)持有关闭

平滑

如果数据嘈杂,通常希望近似值而不是插值。例如,通过这些数据

x = linspace(0,2 * pi,51);noisy_y = cos(x)+ .2 *(rand(尺寸(x)) - 。5);plot(x,noisy_y,'X') axis([-1 7 -1.2 1.2])

interpolation would give the wiggly interpolant shown below in blue.

保持fnplt(csapi(x,noisy_y))持有关闭

相比之下,用适当的宽容平滑

tol = (.05)^2*(2*pi)
托= 0.0157.

gives a smoothed approximation, shown below in red.

保持fnplt(spaps(x,noisy_y,tol),'r', 2 ) hold关闭

The approximation is much worse near the ends of the interval, and is far from periodic. To enforce periodicity, approximate to periodically extended data, then restrict the approximation to the original interval.

noisy_y([1结束])=均值(noisy_y([1结束]));lx =长度(x);lx2 = round(lx / 2);范围= [lx2:lx 2:lx 2:lx2];sps = spaps([x(lx2:lx)-2 * pi x(2:lx)x(2:lx2)+ 2 * pi],noisy_y(范围),2 * tol);

这给出了近似周期性的近似,如黑色所示。

保持fnplt(sps,[0 2 * pi],'K',2)持有关闭

Least-Squares Approximation

Alternatively, you could use least-squares approximation to the noisy data by a spline with few degrees of freedom.

例如,您可以尝试只需四件的立方样条。

spl2 = spap2(4, 4, x, noisy_y); fnplt(spl2,'b'2); axis([-1 7 -1.2 1.2]) holdplot(x,noisy_y,'X'抱紧关闭

结选择

使用时spapi要么spap2, you usually have to specify a particular spline space. This is done by specifying aknot sequenceand an订购, and this may be a bit of a problem. However, when doing spline interpolation tox,ydata using a spline of orderk,您可以使用该功能optkntto supply a good knot sequence, as in the following example.

k = 5;%订单5,即,我们正在使用四个样条x = 2 * pi * sort([0 1 rand(1,10)]);y = cos(x);sp = spapi(optknt(x,k),x,y);fnplt(sp,2,'G');保持绘图(x,y,'o'抱紧关闭axis([-1 7 -1.1 1.1])

When doing least-squares approximation, you can use the current approximation to determine a possibly better knot selection with the aid ofnewknt. For example, the following approximation to the exponential function isn't all that good, as can be seen from its error, plotted in red.

x = linspace(0,10,101);y = exp(x);SP0 = SPAP2(奥纳克(0:2:10,4),4,x,y);绘图(x,y-fnval(sp0,x),'r','行宽'2)

However, you can use that initial approximation to create another one with thesame结的数量,但它们是更好的分布式。它的错误是黑色的。

sp1 = spap2(newknt(sp0),4,x,y);保持plot(x,y-fnval(sp1,x),'K','行宽'2)保持关闭

网格数据

所有the spline interpolation and approximation commands in the Curve Fitting Toolbox can also handle gridded data, in any number of variables.

例如,这里是墨西哥帽子功能的双向样条嵌段。

x = .0001 +( -  4:.2:4);Y = -3:.2:3;[yy,xx] = meshgrid(y,x);r = pi * sqrt(xx。^ 2 + yy。^ 2);z = sin(r)./ r;bcs = csapi({x,y},z);Fnplt(BCS)轴([ -  5 5 -5 5 -5 1])

这里是最小二乘approximation to noisy values of that same function on the same grid.

knotsx = augknt(linspace(x(1), x(end), 21), 4); knotsy = augknt(linspace(y(1), y(end), 15), 4); bsp2 = spap2({knotsx,knotsy},[4 4], {x,y},z+.02*(rand(size(z))-.5)); fnplt(bsp2) axis([-5 5 -5 5 -.5 1])

曲线

Gridded data can be handled easily because Curve Fitting Toolbox can deal withvector-valued花键。这也使得可以轻松使用参数曲线。

这里,例如,通过将立方样条曲线放置通过下图中标记的点来获得的近似值。

t = 0:8; xy = [0 0;1 1; 1.7 0;1 -1;0 0; -1 1; -1.7 0; -1 -1; 0 0].'; infty = csape(t, xy,'periodic');fnplt.(infty, 2) axis([-2 2 -1.1 1.1]) hold绘图(XY(1,:),XY(2,:),'o'抱紧关闭

这是相同的曲线,但是在第三维度中的运动。

滚筒= CSAPE(T,[XY; 0 1/2 1 1/2 0 1/2 1 1/2 0],'periodic');fnplt.( roller , 2, [0 4],'b'抱紧fnplt.( roller, 2, [4 8],'r') plot3(0,0,0,'o'抱紧关闭

曲线的两半以不同的颜色绘制,并且原点被标记为可视化这两个飞行空间曲线。

Surfaces

Bivariate tensor-product splines with values in R^3 give surfaces. For example, here is a good approximation to a torus.

x = 0:4;y = -2:2;r = 4;r = 2;v =零(3,5,5);v(3,:,:) = [0(r-r)/ 2 0(r-r)/ 2 0]。'* [1 1 1 1 1];v(2,:,:) = [r(r + r)/ 2 r(r + r)/ 2 r]。'* [0 1 0 -1 0];v(1,:,:) = [r(r + r)/ 2 r(r + r)/ 2 r]。'* [1 0 -1 0 1];dough0 = csape({x,y},v,'periodic');fnplt(dough0)轴equal, axis关闭

Here is a crown of normals to that surface.

nx = 43;xy = [(1,nx);linspace(2,-2,nx)];点= fnval(Dough0,XY)';ders = fnval(fndir(dough0,眼睛(2)),xy);法线=十字架(ders(4:6,:),ders(1:3,:));法线=(正常./repmat(sqrt(sqrt(normals.*normals))'plyse ],3,1))';pn = [点;点+正常];保持forJ = 1:Nx Plot3(PN([j,j + nx],1),pn([j,j + nx],2),pn([j,j + nx],3))end保持关闭

最后,这里的投影到(x,y)-plane上。

fnplt.(fncmb(dough0, [1 0 0; 0 1 0])) axis([-5.25 5.25 -4.14 4.14]), axis关闭

分散的数据

It is also possible to interpolate to values given at ungridded data sites in the plane. Consider, for example, the task of mapping the unit square smoothly to the unit disk. We construct the data values, marked as circles, and the corresponding data sites, marked as x's. Each data site is connected to its associated value by an arrow.

n = 64;t = linspace(0,2 * pi,n + 1);t(结束)= [];值= [cos(t);罪(t)];绘图(值(1,:),值(2,:),'要么') axisequal, axis关闭站点=值./repmat(max(abs(abs(values))2,2,1);保持plot(sites(1,:),sites(2,:),'xk') quiver(sites(1,:),sites(2,:),...值(1,:) - 站点(1,:),值(2,:) - 站点(2,:))持有关闭

Then usetpapsto construct a bivariate interpolating vector-valued thin-plate spline.

st = tpaps(站点,值,1);

The spline does indeed map the unit square smoothly (approximately) to the unit disk, as its plot viafnplt.indicates. The plot shows the image of a uniformly-spaced square grid under the spline map inst.

保持fnplt.(st) hold关闭