>> % cleaned-up diary of matlab computations done by Carl de Boor during >> % the 02dec99 class of CS 412 >> % first part: end conditions in cubic spline interpolation. >> % The gui, called splinetool, is accessible to you provided you add >> % the following directory to your matlabpath: >> % /u/d/e/deboor/public/matlab/splines >> % For this to work properly, you would have to be (a) on afs, i.e., on one >> % of the CS department's work stations, and (b) would have to put this >> % directory ahead in the path, i.e., use a command like >> % path = path('/u/d/e/deboor/public/matlab/splines',path); >> splinetool >> % second part: least-squares approximation by cubic splines >> % If you want to read more about this, try the last two pages of >> % http://www.cs.wisc.edu/~deboor/412/story6a.ps >> % (albeit with slightly different notation than used in class). >> % 1. the noisy data: >> x = linspace(0,2*pi,101); >> y = sin(x) + (rand(size(x))-.5)/5; >> plot(x,y,'o') >> % 2. choosing the breakpoints for the cubic spline: >> xi = linspace(0,2*pi,5); >> % 3. constructing the `Vandermonde-like' matrix: >> % since the spline command can deal with vector-valued data, we can >> % evaluate all the Lagrange spline in one call: >> Atrans = spline(xi,eye(5),x); >> size(Atrans) ans = 5 101 >> % the rows of this matrix contain the values at the x's of the various >> % `Lagrange splines', as the following plot shows: >> plot(x,Atrans(1,:)) >> hold on >> % plot the xi's >> plot(xi,zeros(size(xi)),'x') >> plot(x,Atrans(2,:),'r') >> % plot also the points (xi,1), to be certain these Lagrange splines do >> % what they are supposed to: >> plot(xi,ones(size(xi)),'x') >> % plot one more of these Lagrange splines >> plot(x,Atrans(4,:),'k') >> hold off >> % 4. we are ready to compute the least-squares fit: >> a = Atrans'\y >> ??? Error using ==> \ >> Matrix dimensions must agree. >> >> % ok, since matlab only deals with matrices, there is always the problem >> % of knowing whether some vector is a row or a column; a column was >> % expected here: >> a = Atrans'\y(:) a = -0.1454 0.9732 0.0043 -0.9634 0.1670 >> % These are just the coefficients, but they are the coefficients in the >> `Lagrange form' for the cubic spline, i.e., the values of that cubic >> spline at the breaks xi. So, to get the spline itself, we simply interpolate >> to those data: >> f = spline(xi,a); >> % replot the data and the value of the least-squares spline fit on top of it: >> plot(x,y,'o') >> hold on >> fx = ppval(f,x); >> plot(x,fx,'r','linewi',2) >> % that's not bad, considering that we used the cubic spline with the >> % not-a-knot condition, i.e., only the middle one of our 3 interior breaks is >> % actually active. >> % We would have done much better with a break sequence >> % xi = [0,1/4,1/2,1,3/2,7/4,1] >> % which does have 3 interior active breaks. Try it out :-) >> diary off