# Calculations steps 4-6: Alignment, scale and Inclination

The Sim3 program is used to further refine the calibration by running a simulation using the dynamic log file and the parameters generated in the previous step.

The process is basically a Monte Carlo Method where the simulation tests random datapoints in a random order by injecting noise and analysing the feedback.

These calculations are very CPU intensive. The simulation will never end.

## Steps 4a, 4b and 5: GYO/ACC scale and alignment

The first 3 steps are very similar to each other. 4a and 4b calculate the Gyro and Accelerometer scales and alignments individually, and step 5 further refines the calculations by leveraging the GYO and ACC against each other.

## Judging when to continue to next step

In the top left corner, the number of calculation loops is displayed. The MAE is the last number on the top line, after the ‘=’ sign.

For steps 4a, 4b and 5, You should be looking for a slow and even drop of the MAE

If the MAE drops very fast or starts to rise or jump up and down, this may indicate a problem with your dynamic data or indicate that the calculations are beeing run at too high variance.

You can abort the simulation with the ‘Abort‘ button when the MAE is not dropping significantly anymore on each loop. Copy generated params to the “all parameters” tab and save before proceeding to the next step.

Another thing to watch are the defines being generated (the 1st column of numbers in the output).  Small changes on each run are expected, but if you start seeing big swings, for example from -0.00005 to +0.00005 in one loop, this may indicate a problem. Keep an eye on these swings and if they get worse, abort the run and try again at lower variance setting

The 3rd column in the outpout is the change that was made in the last loop. If this number begins to swing between large negative and positive values, it could also indicate bad data or too high variance. Small swings between positive and negative is normal when the solution approaches convergence.

Let the simulation run until the MAE is not dropping significantly and the change made on each loop is low. After each step, don’t forget to add the generated data to the ‘All generated parameter’ window and save it.

## Changing variance

Variance is basically the magnitude of change that the Sim3 program is allowed to make in a single loop

It may in some cases be necessary to lower variance to get the data to converge without making big swings.

To change the variance, tick the “use variance” box and enter 1e-7 (0.0000001), or 5e-7 (0.0000005) and run the calculations again. If calculations now proceed as expected, let the calculations run for 10-20 loops, abort and save params.

Then rerun the same step at default variance 1e-6 (0.000001). If the calcs proceed normal now, let them run until MAE stops to drop significantly, abort, save and proceed to next step. Otherwise lower the variance again and let the calculations run for longer.

The more you lower variance the longer it will take for Sim3 to arrive at a good solution.

## If you have problems running steps 4 to 5

If the results aren’t looking correct, there are a couple of things to try. First thing is to try and rerun the step – there is a random element in the calculations and sometimes you are just unlucky and the date diverges instead of converging. Try one more time.

Next thing to do is to lower variance, as outlined above.

If you continue to experience problems running any of these 3 steps, then it may be time to re-examine your log files or create new ones.

The dynamic log in particular (from your dance) may take several tries to perfect, and it is harder to spot anomalies in that data. If you continue having trouble with your calculations or doubts about your data, post your issue and your log graphs on the forums. Here is an example post with all the relevant graphs provided.

## Step 6: magnetic inclination estimation simulation

This last step finalizes your magnetometer calibrations. If the mags are estimated separately, the software can optimize the process and perform a lot of iterations in a short period of time.

The software will also estimate the magnetic inclination. Don’t worry if this comes up a few degrees off from what is estimated in the online inclination tool. Its most likely due to local geomagnetic variances, like iron deposits in the Earth´s crust.

Like the previous 3 steps, the simulation never ends. Abort it when the MAE stops dropping (MAE is the last number in the top line of output, after the ‘=’ sign).  It may not drop for 100 loops and then go down a little more.  Give it some time. Several 1000´s of loops are not uncommon (they run quickly!).  Again, you can abort the run, save the data to your param file, and re-start the step again — it will, essentially, continue where it left off.

When you’re happy with the result and MAE is not dropping, copy/paste the defines into your params file and save it.  A complete param file specific for your AQ board is now compiled. Congratulations! This would be a good time to make a backup of it. This file will now be used to feed the firmware with the needed information.