Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

스플라인을 생성하는 방법

이 예제에서는 Curve Fitting Toolbox™의 스플라인 함수를 사용하여 다양한 방식으로 스플라인을 생성하는 방법을 보여줍니다.

보간

csapi 명령을 사용하여 다음 지점 x에서 코사인 함수와 일치하는 3차 스플라인 보간을 생성할 수 있습니다.

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

그런 다음 fnplt를 사용하여 보간 스플라인을 볼 수 있습니다.

fnplt(cs,2);
axis([-1 7 -1.2 1.2])
hold on
plot(x,y,'o')
hold off

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

보간 확인하기

코사인 함수는 2*pi의 주기를 갖습니다. 이와 관련하여 3차 스플라인 보간이 얼마나 잘 수행될까요? 확인할 수 있는 한 가지 방법은 두 개의 끝점에서 1계 도함수의 차분을 계산하는 것입니다.

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

주기성을 적용하기 위해 csapi 대신 csape를 사용합니다.

csp = csape( x, y, 'periodic' );
hold on
fnplt(csp,'g')
hold off

Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers

확인 결과는 다음과 같습니다.

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

2계 도함수도 이제 끝점에서 일치합니다.

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

동일한 데이터에 대한 조각별 선형 보간spapi를 사용하면 됩니다. 여기에서는 이전 플롯에 조각별 선형 보간을 빨간색으로 추가합니다.

pl = spapi(2, x, y);
hold on
fnplt(pl, 'r', 2)
hold off

Figure contains an axes object. The axes object contains 4 objects of type line. One or more of the lines displays its values using only markers

평활화

데이터에 잡음이 있는 경우, 일반적으로 보간 대신 근사가 유용합니다. 예를 들어, 다음과 같은 데이터에 대해

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

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

보간을 수행하면 아래에 파란색으로 표시된 것처럼 구불구불한 보간을 얻게 됩니다.

hold on
fnplt( csapi(x, noisy_y) )
hold off

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

반면, 다음과 같이 적절한 허용오차를 사용하여 평활화하면

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

아래에 빨간색으로 표시된 것처럼 매끄러운 근사를 얻습니다.

hold on
fnplt( spaps(x, noisy_y,  tol), 'r', 2 )
hold off

Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers

근사는 구간의 끝 근처에서 훨씬 더 부정확하며, 주기성을 고려하지 않습니다. 주기성을 적용하려면 주기를 따라 데이터를 더 연장한 다음, 연장된 데이터에 맞춰 근사한 후 근사 범위를 원래 구간으로 제한하십시오.

noisy_y([1 end]) = mean( noisy_y([1 end]) );
lx = length(x);
lx2 = round(lx/2);
range = [lx2:lx 2:lx 2:lx2];
sps = spaps([x(lx2:lx)-2*pi x(2:lx) x(2:lx2)+2*pi],noisy_y(range),2*tol);

그러면 검은색으로 표시된 것처럼 거의 주기적인 근사를 얻게 됩니다.

hold on
fnplt(sps, [0 2*pi], 'k', 2)
hold off

Figure contains an axes object. The axes object contains 4 objects of type line. One or more of the lines displays its values using only markers

최소제곱 근사법

또는 작은 자유도를 가진 스플라인으로 잡음이 있는 데이터에 최소제곱 근사법을 사용할 수 있습니다.

예를 들어, 조각이 4개뿐인 3차 스플라인을 시도해 볼 수 있습니다.

spl2 = spap2(4, 4, x, noisy_y);
fnplt(spl2,'b',2);
axis([-1 7 -1.2 1.2])
hold on
plot(x,noisy_y,'x')
hold off

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

매듭 선택

spapi 또는 spap2를 사용할 경우 일반적으로 특정 스플라인 공간을 지정해야 합니다. 이렇게 하려면 매듭 시퀀스위수를 지정해야 하며, 이러한 지정이 문제가 될 수 있습니다. 하지만 위수가 k인 스플라인을 사용하여 x,y 데이터에 대해 스플라인 보간을 수행할 때, 다음 예와 같이 함수 optknt를 사용하면 적합한 매듭 시퀀스를 제공할 수 있습니다.

k = 5;   % order 5, i.e., we are working with quartic splines
x = 2*pi*sort([0 1 rand(1,10)]);
y = cos(x);
sp = spapi( optknt(x,k), x, y );
fnplt(sp,2,'g');
hold on
plot(x,y,'o')
hold off
axis([-1 7 -1.1 1.1])

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

최소제곱 근사법을 수행할 때 newknt의 도움을 받아 현재 근사를 사용하여 더 적합한 매듭 선택을 결정할 수 있습니다. 예를 들어, 지수 함수에 대한 다음 근사는 빨간색으로 플로팅된 오차에서 볼 수 있듯이 그다지 적합하지 않습니다.

x = linspace(0,10,101);
y = exp(x);
sp0 = spap2( augknt(0:2:10,4), 4, x, y );
plot(x,y-fnval(sp0,x),'r','LineWidth',2)

Figure contains an axes object. The axes object contains an object of type line.

그러나 이 초기 근사를 사용하여 매듭 수가 같고 더 적절하게 분포된 새 근사를 만들 수 있습니다. 오차는 검은색으로 플로팅되었습니다.

sp1 = spap2( newknt(sp0), 4, x, y );
hold on
plot(x,y-fnval(sp1,x),'k','LineWidth',2)
hold off

Figure contains an axes object. The axes object contains 2 objects of type line.

그리딩된 데이터

Curve Fitting Toolbox의 모든 스플라인 보간과 근사 명령은 변수 개수에 관계없이 그리딩된 데이터를 처리할 수 있습니다.

예를 들어, 다음은 멕시코 모자 함수에 대한 쌍삼차 스플라인 보간입니다.

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)
axis([-5 5 -5 5 -.5 1])

Figure contains an axes object. The axes object contains an object of type surface.

다음은 동일한 함수의 잡음 있는 값에 대한 동일한 그리드에서의 최소제곱 근사입니다.

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])

Figure contains an axes object. The axes object contains an object of type surface.

곡선

Curve Fitting Toolbox는 벡터 값 스플라인을 다룰 수 있으므로 그리딩된 데이터를 쉽게 처리할 수 있습니다. 따라서 파라미터 곡선도 쉽게 사용할 수 있습니다.

예를 들어, 다음은 아래 Figure에 표시된 점을 지나도록 3차 스플라인 곡선을 배치하여 얻은 무한대까지의 근사입니다.

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 on
plot(xy(1,:),xy(2,:),'o')
hold off

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers

다음은 동일한 곡선이지만 3차원의 동작을 갖습니다.

roller = csape( t , [ xy ;0 1/2 1 1/2 0 1/2 1 1/2 0], 'periodic');
fnplt( roller , 2, [0 4],'b' )
hold on
fnplt( roller, 2, [4 8], 'r')
plot3(0,0,0,'o')
hold off

Figure contains an axes object. The axes object contains 3 objects of type line. One or more of the lines displays its values using only markers

곡선의 두 절반이 서로 다른 색으로 플로팅되었으며, 이 2개의 날개 공간 곡선을 시각화하는 데 도움을 주기 위해 원점이 표시되어 있습니다.

곡면

R^3의 값을 갖는 이변량 텐서 곱 스플라인은 곡면을 제공합니다. 예를 들어, 다음은 원환면에 대한 적절한 근사입니다.

x = 0:4;
y = -2:2;
R = 4;
r = 2;
v = zeros(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)
axis equal, axis off

다음은 그 곡면에 대한 법선들의 크라운입니다.

nx = 43;
xy = [ones(1,nx); linspace(2,-2,nx)];
points = fnval(dough0,xy)';
ders = fnval(fndir(dough0,eye(2)),xy);
normals = cross(ders(4:6,:),ders(1:3,:));
normals = (normals./repmat(sqrt(sum(normals.*normals)),3,1))';
pn = [points;points+normals];
hold on
for j=1:nx
   plot3(pn([j,j+nx],1),pn([j,j+nx],2),pn([j,j+nx],3))
end
hold off

마지막으로, 다음은 (x,y) 평면에 대한 투영입니다.

fnplt(fncmb(dough0, [1 0 0; 0 1 0]))
axis([-5.25 5.25 -4.14 4.14]), axis off

산점 데이터

평면의 그리딩되지 않은 데이터 지점에 주어진 값을 보간할 수도 있습니다. 예를 들어, 단위 정사각형을 단위 원판에 매끄럽게 매핑하는 작업을 가정해 보겠습니다. 데이터 값(원으로 표시됨)과 대응하는 데이터 지점(x로 표시됨)을 생성합니다. 각 데이터 지점은 연관된 값에 화살표로 연결되어 있습니다.

n = 64;
t = linspace(0,2*pi,n+1); t(end) = [];
values = [cos(t); sin(t)];
plot(values(1,:),values(2,:),'or')
axis equal, axis off

sites = values./repmat(max(abs(values)),2,1);
hold on
plot(sites(1,:),sites(2,:),'xk')
quiver(sites(1,:),sites(2,:), ...
       values(1,:)-sites(1,:), values(2,:)-sites(2,:))
hold off

그런 다음 tpaps를 사용하여 이변량 보간 벡터 값을 갖는 박판 스플라인을 생성합니다.

st = tpaps(sites, values, 1);

이 스플라인은 fnplt를 통해 생성된 아래 플롯에 표시된 것처럼 실제로 단위 정사각형을 단위 원판에 매끄럽게(근사적으로) 매핑합니다 이 플롯은 st의 스플라인 맵 아래에 균일한 간격의 정사각 그리드가 겹쳐 있는 이미지를 보여줍니다.

hold on
fnplt(st)
hold off