Using Fmincon As An Optimization Tool For Calibrations
Using Fmincon As An Optimization Tool For Calibrations
Jonathan Ye
7/26/22
2
Introduction
This document will give you a detailed guide on using MATlab and the built-in function
'fmincon' as a means to optimize calibrations for models used in controls. The way the program
is currently (as of July 2022) set to optimize the calibrations is by minimizing a cost function
using a root mean squared (RMS) error between the plant and model data, though other means of
analyzing the error between the data can be used as well, depending on your needs.
In the first part, some general information will be covered on 'Fmincon' and its usage
with Simulink models as well as some discussion on what settings to consider.
In the second part, I will discuss the use of ‘fmincon’ in the specific use of the feed
forward (FF) HC-dosing Simulink model. Since the FF HC-dosing Simulink model is currently
using MATlab R2014b, I will discuss the function and variable names as shown in that version.
The names may change in later versions, but the function should be the same. General results of
the performance of the optimizer will be presented and a discussion of what settings worked best
for the specific use case will be brought to the table.
Finally, in part three, I will discuss the use of fmincon for other models that are different
from the FF HC-dosing use case.
would necessitate a larger step size than the default step size, see provided document for more
info [3].
2.1 Background
The FF HC-dosing map relates flow rate to a filter factor simulating thermal mass. The
FF HC-dosing map corresponds to a FF HC-dosing model that is written in Simulink. To
calibrate this map, we obtained test cell data of a cycle of varying flow rates. We then feed the
obtained data to the Simulink model and fit the model curve to the test cell data. Originally, we
would have to hand calibrate this map, and since the data is in time series, it is hard to precisely
calibrate the dynamics and timing of each map value. So, an automated optimization strategy can
be used to more precisely calibrate the desired map. The plant data that we chose to fit the model
to in this case is thermocouple (TC) 2. Where the TCs are are depicted in the figure below.
4
2.2 Solution
To automate the calibrations we must compare the model curve to the test cell cycle we
obtained with a cost function and use fmincon to minimize that cost function. In our case we
decided that a root mean squared (RMS) error would sufficiently determine the “closeness” of
the fit between the two curves. Additionally, because the flow rate has an inverse relationship
with thermal mass, the map was fitted to a hyperbolic curve to avoid overfitting the optimization
to a specific set of data. So, the entire process includes (see Fig. 1):
There are two maps that need to be calibrated in the FF HC-dosing model:
DOC_elmt_tm_cnst_map_out and DOC_midbrick_elmt_tm_cnst_mo which correspond to the
thermal mass at the outlet and mid-brick of the DOConDPF can. So, two separate runs need to be
run to optimize each map one at a time. The code is meant to be general enough where only a
couple of lines need to be changed between both runs.
The initial values used were all ones for each map value. This was to see if we can
automate the calibration process by starting at really inaccurate map values.
FiniteDifferenceStepSize used for the midbrick and outlet maps were 1e-2 and 5e-2, respectively.
All map values were constrained with an upper and lower bounds of 0 to 100.
2.3 Results
To use as a basis of comparison, hand calibrated map values will be used against the map
values obtained from the optimizer. The general strategy for hand calibrations is to find a flow
rate at steady state in the middle of the range and calibrate that specific map value with the plant
data. After that one map value is calibrated, a theoretical hyperbolic curve is fit to that map value
point. Then, each theoretical map value is tweaked based on the dynamics of the plant data at
each flow rate range. For each map the following map values were obtained:
0 100 75
250 60 24
6
300 48 21
350 40 19
400 34 16.5
500 24 15
600 16 13.7
800 8 12
1000 7 9
1800 6 5.56
2400 6 4.17
Visually, we can see the map calibrations on a graph in Fig. 2. Notably, the outlet map
should be about half due to heat transfer physics, but it is much more than half, and even
surpasses the mid-brick map at some points (about 700 to 1500 kg/hr).
7
With the hand calibrated map calibrations, we can now use the MATlab based optimizer
to find the optimized and fitted maps. We can see the results in the following table:
Like the hand calibrated map values, we can visualize the map values on a graph in Fig.
3. With the optimizer the map values can jump up and down as the flow rate increases, which
can be due to an overfit from not using enough data. Though, this is mitigated by fitting the
points to a theoretical hyperbolic curve, which is shown in the dark and light blue.
Table 3. RMS Error of Hand Calibrated Maps Table 4. RMS Error of Optimized Maps
9
To see how the optimizer improved the map calibrations, we can see how the RMS error
decreased for each map. However, it is important to note that the hand calibrations that are used
here were based on using thermocouple 25 as DPF_out instead of thermocouple 2 as DPF_out,
so the hand calibrations could have been better, but due to time constraints, the hand calibrations
were left alone.
2.4 Discussion
The optimizer successfully found a minima in the data with the given constraints. The
initial values used were all ones because ‘fmincon’ uses a forward difference approximation, so
bad map values on the smaller side of the spectrum were used. The FiniteDifferenceStepSizes
used were determined from trial and error. ‘Fmincon’ uses a finite difference step size to estimate
a gradient for its optimizations. Since our model with cost function is most likely not entirely
smooth, a step size too small will not properly estimate a gradient, as there may be
inconsistencies throughout the function as the map values step. These inconsistencies can
prevent ‘fmincon’ from continuing because of the function. Though, it is important to note that
the step size should not be too large either, since if it is too large, ‘fmincon’ may not estimate an
accurate enough gradient.
The optimizer did successfully improve both the calibrations of the midbrick and the
outlet because it reduced the RMS error for the mid brick and it made the outlet map fit much
closer to the physics related to the map.
This optimizer successfully calibrated the map values for the FF HC-dosing model and
even automated the process. In the future, this optimizer can be used for similar applications, but
with different models. This will be discussed further in part 3.
Running the optimization tool for this application took about 45 min - 1 hour.
3.1 Introduction
By using ‘fmincon’ and treating the optimal function as a black box function, we can use
this strategy for many different calibration applications. The applications that may be the most
useful for are calibrations with time-series’ as it is much harder to calibrate with the dynamics
shifting the cycle with each calibration.
10
3.2.1 Background
For VGT boost control on the 13x engine, a desired IMAP pressure is used. However,
this desired IMAP pressure can be very noisy, decreasing the stability of the VGT controls, so a
band-pass filter is used to reduce the noise in the desired IMAP pressure channel. This band-pass
filter uses two maps to correlate engine speed to a filter factor that will be applied to the filter.
The two maps that are used are as_des_imap_filt_fact_out and as_des_imap_filt_fact_dec_out
for when the pressure increases and decreases, respectively. Ideally, the desired pressure should
lead the actual pressure by about 20 KPa for the controller to correctly control the boost. So,
during an increment transient, the desired curve should be above the actual boost curve, while
during a decrement transient, the desired curve should be below the actual boost curve. The most
important aspect to this calibration is the stability of the VGT control, so after a map is
generated, it should be tested in the test cell for proper validation.
3.2.2 Solution
The solution was to first pre-process the data to filter out the steady state portion of the
test cycle. This was done by hand picking the indices of the transient data using Diadem and
generating a binary mask from these indices. Since the desired curve should be leading in both
the increment and decrement transient, the cost function was adjusted so that the RMS error was
calculated based off of an offset actual boost curve. This offset was done by adding or
subtracting pressure from the actual boost channel. After some trial, which will be discussed in a
later section, it was determined that splitting up the two maps into two different Fmincon
optimizer runs with differing cost functions resulted in the most optimal map with our given
constraints of the desired boost leading by 20 KPa. Since the two maps affect the behavior of the
entire desired boost curve regardless of whether it is increasing or decreasing, the offsets of the
actual boost channel for the cost functions would have to be played with to determine one that is
appropriate for our needs. After some trial, it was determined that an offset of +0 KPa and +20
KPa will be used for the increment and decrement maps respectively. This offset will allow
‘Fmincon’ to search for the map that best fits the offset. The following options were used for
‘Fmincon’ :
Because the data is very noisy and small variations in the map can cause large
optimalities, a larger than default finite difference step size was used. The initial maps used were
1 by 19 maps of values of 1e-5 with an upper and lower bound of 1 and 0. Additionally, due to
the noise, the data can be easily overfit, so to prevent this, ‘Fmincon’ will fit the map to a
polynomial with coefficients a, b, and c
11
2
𝑀𝑎𝑝𝑉𝑎𝑙𝑢𝑒 = 𝑎𝑥 + 𝑏𝑥 + 𝑐 , where x is the engine speed from 6 to 24 (600 to 2400 rpm
divided by 100; this is to increase coefficients and avoid fmincon’s stop criteria of too small of a
step size).
Another important note is that the decrement map was initialized before any ‘Fmincon’
optimizers were run because the ‘Fmincon’ for the increment map needed a baseline for the
decrement map for an accurate calibration, since the maps influence each other.
Results
To visualize these maps, the values are plotted in figure 5. (See appendix for Simulink
outputs),
From figure 5., we can observe that both the maps are the same and they are both linear
with the equation shown in the figure.
Table 6 and 7 show the optimized maps. To visualize these maps, the values are plotted
in figure 6. (See appendix for Simulink outputs),
15
Figure 7. Before running through optimizer (Top) Graph of desired and actual IMAP, (Bottom)
Graph of VGT movement
16
Figure 8. After running through optimizer (Top) Graph of desired and actual IMAP, (Bottom)
Graph of VGT movement
Discussion
After the map values were run through the optimizer, the map values became much more
quadratic, especially with the decrement map. This can make sense because with the original
map values, as the engine speed increases, we can observe that the decrement transient portion of
the curve starts to lag the actual IMAP values (see appendix). So, a higher filter factor will make
the decrement transient faster, but it will introduce more noise.
Initially, the maps were optimized in a singular ‘Fmincon’ function, due to the maps
influencing each other. However, optimizing both maps at the same time and fitting them to a
2nd order polynomial seemed to constrain the minimization too much and the resulting maps
were two horizontal lines. From analyzing the maps with the Simulink model, it has been
observed that as the engine speed increases, the filter factor should increase with increasing
engine speeds. So, the optimization was split into two different ‘Fmincon’ functions to give the
optimization more flexibility into finding map values that are more reasonable. In the future
however, it may be more advantageous to use an optimizer that combines both the increment and
decrement map as they will influence each other. Scripts have been made with both combined
and separated maps, so you may need to try each method out when desired.
Additionally, because ‘fmincon’ attempts to converge by incrementally decreasing the
normal step size, and the given channels are extremely noisy, it may be very difficult for
‘Fmincon’ to find a minimum. This may be due to the function not being smooth with multiple
local minima, so a different function that searches globally may be more beneficial (see Global
Optimization Toolbox) [5].
References
17
[1] https://www.mathworks.com/help/optim/ug/fmincon.html
[2]https://www.mathworks.com/help/optim/ug/optimizing-a-simulation-or-ordinary-differential-e
quation.html
[3]
https://www.mathworks.com/help/optim/ug/constrained-nonlinear-optimization-algorithms.html#
[4] https://www.mathworks.com/help/optim/ug/fminunc.html
[5]https://www.researchgate.net/profile/Olli-Tahvonen/publication/262956005_On_the_economi
cs_of_optimal_timber_production_in_boreal_Scots_pine_stands/links/573068a208ae744151911
731/On-the-economics-of-optimal-timber-production-in-boreal-Scots-pine-stands.pdf
Appendix
3.1 Initial Map Data for VGT boost (Pressure vs. Time)
21
3.1 Optimized Map Data for VGT boost (Pressure vs. Time)