# Fitting a surface to geometric data

Having obtained data points of the surface you are trying to fit a mesh to (digitising), and creating an initial mesh (initial-mesh) that is to be deformed to fit these points, you now need to do the fitting.

#### Read in initial geometry and calculate Xi-positions of data points

Begin by typing cm at the prompt. All of the following commands are in fit.com, which you can run from the command prompt by typing cm fit.com.
Begin by reading in the array sizes we need, defining the coorindate system, and then defining the initial mesh:

```fem def para;r;fit
fem def coord 3,1
fem def base;r;bicubic-sector
fem def node;r;epi-init
fem def elem;r;epi-init
```
We have now created the initial mesh. We can view it by typing the following at the cm prompt (This is not in the command file):
```fem def win;c on yz # Define a window on the yz plane
fem draw line
```
We should now read in the data points for the mesh to be fitted to, and then calculate the Xi positions (The positions of the data in terms of local element space) of the data:
```fem def data;r;epi
fem def xi;c close
```

#### Creating fields to store nodal & element parameters

Having set up the initial mesh and data, we now have to create space inside cm to store the fitted nodal and element parameters. You can create this by:

```fem def field;r;epi # To create a field to fit the nodal geometry
fem def elem;r;epi field # To create a field to fit the elemental geometry
```

#### Fitting the data

We can now define the fitting procedure and fit the data. Define the fitting procedure by:

```fem def fit;r;epi geometry
```
You will notice in the .ipfit file there are parameters there for smoothing via Sobolev weights on the derivatives. These weights are used to remove excessive 'wrinkling' of the fitted surface to your data. The smaller these numbers are, the closer the fit 'hugs' the data and the more wrinkled it can become. A good range is between 10.0E-1 to 10.0E-6. These parameters are the main variables that you can control to ensure a good quality fit.

Now fit the data:

```fem fit
fem update node fit # Overwrites nodes with new, fitted ones.
```

You can check out your fitting by re-drawing the lines inside cm:

```fem hide lines
fem draw lines
```

#### Repeating the fit

Having fitted the data once, you should see output from the fem fit command, giving you RMS error. If this is more than what might be desired, you can run the fit again on the new mesh to reduce the RMS error again. The amount that this decreases by is limited, however, so there is no point in doing it until your ears fall off. To repeat the process, type the following:

```fem def xi;c close # Redefine the Xi positions of the data points
fem fit
fem update node fit
```

#### Exporting the mesh for viewing in cmgui

There is a comfile that uses the files you have exported to view the mesh in cmgui. This exists in view.com. You can run it by simply typing in:

```cmgui view
```
And you should see an image like the following: #### The comfile run by this example is as follows:

```#Example 21e3
```

#### Files used by this example are:

```Name              Modified     Size

example_21e3.com  09-Feb-2001   14
epi/              13-Apr-2007    -
lv/               13-Apr-2007    -
rv/               13-Apr-2007    -
```

```Name                           Modified     Size