# Using Matlab to fit tensor product splines to bivariate gridded data (2)

This example shows how to use the spline commands in Curve Fitting Toolbox to fit tensor product splines to bivariate gridded data.

Since Curve Fitting Toolbox can handle splines with _vector_ coefficients, it is easy to implement interpolation or approximation to gridded data by tensor product splines. Most spline construction commands in the toolbox take advantage of this.

However, you might be interested in seeing a detailed description of how approximation to gridded data by tensor products is actually done for bivariate data. This will also come in handy when you need some tensor product construction not provided by the commands in the toolbox.

## Model File Tree

## Software Used

### Matlab

**MATLAB** (**mat**rix **lab**oratory) is a multi-paradigm programming numerical computing environment and fourth-generation programming language. A proprietary programming language developed by MathWorks, MATLAB allows matrix manipulations, plotting of function and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C , C++, C Sharp, Java, Fortran and Python.

For more details see Matlab .

## Results

Here is an example of Least-Squares Approximation to Gridded Data. Lets take some gridded data from Franke's sample function. Note that the grid is somewhat denser near the boundary, to help pin down the approximation there.

Next lets find the choice of spline space in the Y-direction. Figure 2 shows the simultaneous approximation to all curves in the Y-direction.

Note that, for each |x(i)|, both the first two and the last two values are zero since both the first two and the last two sites in |yy| are outside the basic interval for the spline |sp|. Also note the "ridges" that run along the y-direction, most noticeable near the peaks of the surface. They confirm that we are plotting smooth curves in one direction only.

Now lets move from curves to a surface, lets choosing a spline space in the X-direction. Here is the plot of the spline approximant.

There are some more efficient alternatives. Since the matrices |spcol(knotsx,kx,xv)| and |spcol(knotsy,ky,yv)| are banded, it may be more efficient for "large" |xv| and |yv| (though perhaps more memory-consuming) to make use of |fnval|. In fact, |fnval| and |spmak| can deal directly with multivariate splines. Better yet, the construction of the approximation can be done by _one_ call to |spap2|, therefore we can obtain these values directly from the given data. Here is a plot of the error, i.e., the difference between the given data value and the value of the spline approximation at those data sites.

Simultaneous Approximation to All Curves in the X-Direction :-

The Spline Interpolant:

Interpolation error:

## Related Models

Following are some related models available for cloning/copying by anyone:

- Matlab Surface Fitting With Custom Equations to Biopharmaceutical Data
- Matlab Surface Fitting With Custom Equations to Biopharmaceutical Data (876)
- Matlab curve fitting example
- Matlab curve fitting example (4)
- Matlab surface fitting example
- Matlab surface fitting example using cftool GUI
- Matlab surface fitting example using cftool GUI (67)
- Use Matlab to Fit Smooth Surfaces To Investigate Fuel Efficiency
- Use Matlab to Fit Smooth Surfaces To Investigate Fuel Efficiency (1)

Click on the category links at the bottom of this page to navigate to a full list of simulation models in similar subject area or similar computational methodology.