主要内容

样条拟合钛测试数据

This example shows how to use commands from Curve Fitting Toolbox™ to fit a spline to titanium test data with manual and automatic selection of knots.

样条插值的手动结选项

以下是记录钛的某种性质的一些数据,测量为温度的函数。我们将使用它来说明样条插值的一些问题。

[xx,yy] = titanium;

A plot of the data shows a rather sharp peak.

plot(xx,yy,'bx');框架= [-10 10 -.1 .3] + [min(xx),max(xx),min(yy),max(yy)];轴(框架);

We pick a few data points from these somewhat rough data, since we want to interpolate. Here is a picture of the data, with the selected data points marked.

pick = [1 5 11 21 27 29 31 33 35 40 45 49]; tau = xx(pick); y = yy(pick); hold情节(Tau,Y,'ro');保持关闭

Since a spline of order k with n+k knots has n degrees of freedom, and we have 12 data points, a fit with a fourth order spline requires 12+4 = 16 knots. Moreover, this knot sequence t must be such that the i-th data site lies in the support of the i-th B-spline. We achieve this by using the data sites as knots, but add two simple knots at either end.

dl = tau(2) - tau(1); dr = tau(end) - tau(end-1); t = [tau(1)-dl*[2 1] tau tau(end)+dr*[1 2]];%构建结序列情节(Tau,Y,'ro');保持轴(frame+[-2*dl 2*dr 0 0]) plot(t,repmat(frame(3)+.03,size(t)),'kx'抱紧关闭传说({'Data Values''Knots'},'location','nw')

我们使用这种结序列来构造插值立方样条。

sp = spapi(t,tau,y);

Now, for the plot. Since we do not care about the part of the spline outside the data interval, we restrict the plot to that interval.

情节(Tau,Y,'ro')轴(框架)保持fnplt(sp,[tau(1) tau(end)],'K'抱紧关闭

A closer look at the left part of the spline fit shows some undulations.

xxx = linspace(tau(1),tau(5),41); plot(xxx, fnval(sp, xxx),'K', tau, y,'ro');轴([Tau(1)Tau(5)0.6 1.2]);

The unreasonable bump in the first interval stems from the fact that our spline goes smoothly to zero at its first knot. To see that, here is a picture of the entire spline, along with its knot sequence and the data points.

fnplt(sp,'K');保持情节(Tau,Y,'ro', t,repmat(.1,size(t)),'kx');保持关闭传说({'Spline Interpolant''Data Values''Knots'},'location','nw')

Here is a simple way to enforce a more reasonable boundary behavior. We add two more data points outside the given data interval and choose as our data there the values of the straight line through the first two data points.

tt = [tau(1)-[4 3 2 1]*dl tau tau(end)+[1 2 3 4]*dr]; xx = [tau(1)-[2 1]*dl tau tau(end)+[1 2]*dr]; yy = [y(1)-[2 1]*(y(2)-y(1)) y y(end)+[1 2]*(y(end)-y(end-1))]; sp2 = spapi(tt,xx,yy); plot(tau,y,'ro', xx([1 2 end-1 end]),yy([1 2 end-1 end]),'博');轴(帧+ [ -  2 * DL 2 * DR 0 0]);保持fnplt(sp2,'b',tau([1末]))持有关闭传说({'Original Data''Data Added for End Conditions'。。。'适合添加数据'},'location','nw')

Here is a comparison of the two spline fits, to show the reduction of the undulation in the first and last interval.

保持fnplt(sp,'K',tau([1末]))持有关闭传说({'Original Data''Data Added for End Conditions'。。。'适合添加数据''原创'},'location','nw')

最后,这里仔细看看前四个数据间隔,更清楚地显示左端附近的下降蝎子。

情节(Tau,Y,'ro',xxx,fnval(sp2,xxx),'b',xxx,fnval(sp,xxx),'K');轴([tau(1) tau(5) .6 1.2]); legend({'Original Data''适合添加数据'。。。'原创'},'location','nw')

Automatic Knot Choice for Interpolation

如果所有这些细节会让您关闭,请让曲线拟合工具箱为您选择结。指定Interpolant的所需顺序作为样条插值命令的第一个输入参数spapi,而不是结序列。

autosp = spapi(4, tau, y); knots = fnbrk(autosp,'knots');情节(Tau,Y,'ro'抱紧fnplt(autosp,'g') plot(knots, repmat(.5,size(knots)),'gx'抱紧关闭传说({'Data Values''适合由spapi'选择的结。。。'Knots Chosen by SPAPI'},'location','nw')

Below is the result of a much better knot choice, obtained by shifting the knot at 842 slightly to the right and the knot at 985 slightly to the left.

knots([7 12]) = [851, 971]; adjsp = spapi(knots, tau, y); holdfnplt(adjsp,'r'2)plot(knots, repmat(.54,size(knots)),'rx'抱紧关闭传说({'Data Values''适合由spapi'选择的结。。。'Knots Chosen by SPAPI''适合结调整'。。。'Adjusted Knots'},'location','nw')

Or else, simply try the standard cubic spline interpolant, supplied bycsapi。这相当于结略有不同的结。

autocs = csapi(tau, y); plot(tau, y,'ro'抱紧fnplt(autocs,'c'抱紧关闭

利用这种迅速不同的数据,即使它们中的每一个是立方样条,也很难在所有合理的嵌就中获得一致。下面的曲线显示了所有五个内嵌体,进行比较。

情节(Tau,Y,'ro'抱紧fnplt(sp,'K',tau([1 end]))% black: originalfnplt(sp2,'b',tau([1 end]))% blue: with special end conditionsfnplt(autosp,'g')% green: automatic knot choice by SPAPIfnplt(autocs,'c')% cyan: automatic knot choice by CSAPIfnplt(adjsp,'r'2)% red: knot choice by SPAPI slightly changed保持关闭传说({'Data Values''原创'“特殊的最终条件”。。。'With Knots Chosen by SPAPI''With Knots Chosen by CSAPI'。。。'With Adjusted Knots'},'location','nw')