MATLAB Examples

## spline2D example

Initialize a clean workspace. Save your work first!

```clear all close all ```

## Set up test 2D polynomial.

```[x,y] = meshgrid(-3:4,-3:4); % independent variables % EG: Make a test 2D polynomial with coeffiecients 1, 2, 3, 4, 5, 6 % that is 2nd-order (quadratic) in x-direction, and 1st order (linear) % in y-direction. test2DPoly = polyVal2D([1,2,3,4,5,6],x,y,2,1); % a test 2D polynomial ```

## Fit test polynomial with spline (piecewise continuous polynomials).

Split region at 0 in x-direction and at -1 and 1 in y-direction, so there are 6 regions and also 6 boundaries to fit. In each of the 6 regions fit to 2nd order in x-direction and 1st order in y-direction. Use the default number of gridpoints along the boundaries (IE: don't specify).

```fit2DSpline = splineFit2D(test2DPoly,x,y,2*ones(2,3),ones(2,3),0,[-1 1]); % spline fit test2DSpline = splineVal2D(fit2DSpline,x,y,2*ones(2,3),ones(2,3),0,[-1 1]); % evaluate % another example with 9 regions and 9 boundaries! % fit2DSpline = splineFit2D(test2DPoly,x,y,2*ones(3),ones(3),[-1 1],[-1 1]); % test2DSpline = splineVal2D(fit2DSpline,x,y,2*ones(3),ones(3),[-1 1],[-1 1]); ```

## plot results

```testRange = [min(test2DPoly(:));max(test2DPoly(:))]; testRange = [testRange(1) - diff(testRange)/10; ... testRange(2) + diff(testRange)/10] * [1 1]; figure, subplot(1,2,1) scatter(x(:),test2DPoly(:),750,y(:),'.'), hold('on') plot(x',test2DSpline') plot([0 0],testRange,'--','LineWidth',2.5), hold('off'), grid title('x-direction') xlabel('x'), ylabel('test2DPoly') legstr = cellstr([repmat('y_{spline} = ',8,1),num2str((-3:4)')]); legend('poly',legstr{:}) subplot(1,2,2) scatter(y(:),test2DPoly(:),750,x(:),'.'), hold('on') plot(y,test2DSpline) plot([-1 1;-1 1],testRange,'--','LineWidth',2.5), hold('off'), grid title('y-direction') xlabel('y'), ylabel('test2DPoly') legstr = cellstr([repmat('y_{spline} = ',8,1),num2str((-3:4)')]); legend('poly',legstr{:}) ```