0% found this document useful (0 votes)
116 views21 pages

Using Fmincon As An Optimization Tool For Calibrations

The document discusses using the MATLAB function fmincon to optimize calibrations for dynamic models. It provides guidance on using fmincon to minimize a cost function comparing simulated and measured data for a feed-forward HC-dosing model. The document outlines setting up the optimization problem, discusses important fmincon settings, and shows the optimized calibration maps achieved a better fit than hand-tuned maps.

Uploaded by

api-576590803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views21 pages

Using Fmincon As An Optimization Tool For Calibrations

The document discusses using the MATLAB function fmincon to optimize calibrations for dynamic models. It provides guidance on using fmincon to minimize a cost function comparing simulated and measured data for a feed-forward HC-dosing model. The document outlines setting up the optimization problem, discusses important fmincon settings, and shows the optimized calibration maps achieved a better fit than hand-tuned maps.

Uploaded by

api-576590803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1

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.

Part 1: 'Fmincon' General Information

1.1 What is ‘Fmincon’?


'Fmincon' is a MATlab function from the Optimization Toolbox that minimizes a
nonlinear function with given constraints and outputs the variables that contributed to said
minimized function. The constraints range from inequality constraints to lower and upper
bounds, all of which can be set by the user [1]. It attempts to minimize functions by finding the
location of the minimum gradient/derivative (optimality) of a given function by means of
gradient descent. 'Fmincon' has many different options to allow the function to minimize more
precisely based on problem needs, the main ones to worry about are the optimality threshold
settings and the step size settings; you can read all about them on the mathworks website [1]. I
will discuss these settings and important notes later in the document. If you do not have
constraints, then consider using ‘Fminunc’, or an unconstrained ‘Fmincon’. This function will
estimate a trust region and will iterate through and converge to a smaller and smaller trust region
until the function is minimized [4].

1.2 Using 'Fmincon' With Simulations


Since our models are established in Simulink, the easiest solution I’ve found is to treat
the Simulink model as a blackbox function that is used to calculate a RMS error, which is all
wrapped inside the 'Fmincon ' function. It is important to note that since our models are based on
plant data, the function that 'Fmincon' will analyze will more than likely be non-smooth, which
3

would necessitate a larger step size than the default step size, see provided document for more
info [3].

1.3 ‘Fmincon’ Settings to Consider


You can see a full list of settings in the provided documents [1]

1. FiniteDifferenceStepSize (FinDiffRelStep in MATlab 2014b)


a. If 'fmincon' does not attempt to find another minimum after 1 point, it is very
likely that the step size needs to be increased, try 1e-3 first and iterate from there
[3]
2. OptimalityTolerance (TolFun in MATlab 2014b)
a. If the optimality is not converging to 0 to your liking, you can lower the
optimality threshold so that fmincon continues looking for a minimum, though
everything lower than 1e-6 will be treated as 0
b. Also, consider diminishing returns based on the scale of your calibrations. For
example, if a small change contributes very little to minimizing your error, it may
be wise to raise your optimality threshold to save running time.
3. Display - ‘Iter’
a. Turn this on to see the function values at each iteration for troubleshooting
4. FiniteDifferenceType
a. If you want a more accurate gradient estimate, use ‘central’ instead of the default,
‘forward’. It will take longer, however.
5. PlotFcn - ‘optimplotx’
a. If you want to see how your optimized value is changing as ‘fmincon’ iterates,
use this function and choose the ‘optimplotx’ option

Part 2: Fmincon for Feed Forward HC-dosing Maps

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

Figure 1. Instrumentation configuration of DPF

It is important to note that the instrumentation configuration was accidentally flipped


during installation ie. TC 1 is now the outlet while TC 7 is now the inlet. From previous data
analysis, the dynamics of TC 1 seemed to be incorrect, so TC 2 was chosen as the outlet instead.

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):

1. Set the desired map values as an array variable


2. Allow the Simulink model to use variable map
3. Write the cost function (RMS error) and connect the output of the simulink model to that
cost function
4. Use fmincon to minimize the cost function and output the optimized map
a. The settings of fmincon have to be played around with for correct usage– this will
be discussed in the next section
5. Fit the data to a hyperbolic line of best fit
6. Enjoy your optimized calibration!
5

Figure 1. Block diagram of process of calibrating FF HC-dosing


(If you would like to see the code, look in the appendix!)

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:

Table 1. Hand Calibrated Map Values


Flow Rate (kg/hr) DOC_midbrick_elmt_tm_cnst DOC_elmt_tm_cnst_map_out
_mo

0 100 75

250 60 24
6

300 48 21

350 40 19

400 34 16.5

500 24 15

600 16 13.7

700 10.2 12.8

800 8 12

900 7.5 11.1

1000 7 9

1200 6.8 8.3

1400 6.3 7.14

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

Figure 2. Map values from hand calibration

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:

Table 2. Optimizer Raw Map Values and Curve Fitted Values


Flow Rate DOC_midbrick_ DOC_elmt_tm_c DOC_midbrick_ DOC_elmt_tm_c
(kg/hr) elmt_tm_cnst_m nst_map_out elmt_tm_cnst_m nst_map_out
o o Curve Fit Curve Fit

0 1.019 1.019 0.998 0.998

250 59.834 59.177 25.024 18.649

300 42.871 46.033 9.160 17.015

350 39.187 37.667 26.200 15.644

400 28.827 31.874 7.980 14.478

500 28.139 24.376 19.005 12.599

600 16.458 19.734 12.162 11.152


8

700 15.203 16.577 10.187 10.003

800 11.769 14.291 12.515 9.069

900 14.219 12.559 8.649 8.294

1000 12.056 11.202 6.659 7.641

1200 9.628 9.210 7.851 6.602

1400 7.200 7.820 5.653 5.812

1800 6.703 6.007 5.049 4.689

2400 4.329 4.457 3.329 3.635

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.

Figure 3. Graph showing the map values

Table 3. RMS Error of Hand Calibrated Maps Table 4. RMS Error of Optimized Maps
9

DOC_midbrick_elm DOC_elmt_tm_cnst DOC_midbrick_elm DOC_elmt_tm_cnst


t_tm_cnst_mo Hand _map_out Hand t_tm_cnst_mo _map_out
Calibration RMS Calibration RMS Optimizer RMS Optimizer RMS
Error Error Error Error

9.56 15.65 8.19 10.98

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.

Part 3: Fmincon with Different Applications

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 Using ‘Fmincon’ to Calibrate a Band-Pass Filter For Transient Data

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.

So, the entire process includes:

1. Note the indices in which the transient data is in


a. Create a binary mask to filter the unwanted steady state data
2. Write fmincon function for the increment boost map (see Figure 4.)
a. Allow the Simulink model to use variable map
b. Write the cost function (RMS error) and connect the output of the simulink model
to that cost function
i. Make sure cost function (RMS error) leaves actual data without an offset
c. Use fmincon to minimize the cost function and output the optimized map
3. Write fmincon function for the decrement boost map (see Figure 4.)
a. Allow the Simulink model to use variable map
b. Write the cost function (RMS error) and connect the output of the simulink model
to that cost function
i. Make sure cost function (RMS error) includes actual data offset of +20
KPa
c. Use fmincon to minimize the cost function and output the optimized map
4. Adjust ‘Fmincon’ settings if needed
5. Generate optimal maps with given fitted polynomials
6. Enjoy your optimized calibration!
12

Figure 4. Block diagram of the process

Results

Table. 5 Original increment Map Table. 6 Original decrement Map


(DOC_elmt_tm_cnst_map_out) (as_des_imap_filt_fact_dec_out)
Engine Speed Filter Factor Engine Speed Filter Factor
600 0.01 600 0.01

700 0.0117 700 0.0117

800 0.0133 800 0.0133

900 0.015 900 0.015

1000 0.0167 1000 0.0167

1100 0.0183 1100 0.0183

1200 0.02 1200 0.02

1300 0.0217 1300 0.0217

1400 0.0233 1400 0.0233

1500 0.025 1500 0.025


13

1600 0.0267 1600 0.0267

1700 0.0283 1700 0.0283

1800 0.03 1800 0.03

1900 0.0317 1900 0.0317

2000 0.0333 2000 0.0333

2100 0.035 2100 0.035

2200 0.0367 2200 0.0367

2300 0.0383 2300 0.0383

2400 0.04 2400 0.04

To visualize these maps, the values are plotted in figure 5. (See appendix for Simulink
outputs),

Figure 5. Graph showing pre-optimized map values


14

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. 5 Optimized Increment Map Table. 6 Optimized Decrement Map


(DOC_elmt_tm_cnst_map_out) (as_des_imap_filt_fact_dec_out)
Engine Speed Filter Factor Engine Speed Filter Factor
600 0.016581 600 0.031629

700 0.016827 700 0.04305

800 0.01711 800 0.056229

900 0.017432 900 0.071165

1000 0.017791 1000 0.087858

1100 0.018188 1100 0.106308

1200 0.018623 1200 0.126516

1300 0.019096 1300 0.14848

1400 0.019607 1400 0.172202

1500 0.020155 1500 0.197681

1600 0.020741 1600 0.224916

1700 0.021366 1700 0.25391

1800 0.022027 1800 0.28466

1900 0.022727 1900 0.317167

2000 0.023465 2000 0.351432

2100 0.02424 2100 0.387454

2200 0.025053 2200 0.425233

2300 0.025904 2300 0.464769

2400 0.026793 2400 0.506062

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 6. Graph showing optimized map values

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

2. Script to optimize FF HC-dosing maps


18

3. Script to optimize Filter Factor for VGT Boost control


19
20

3.1 Initial Map Data for VGT boost (Pressure vs. Time)
21

3.1 Optimized Map Data for VGT boost (Pressure vs. Time)

You might also like