# Reactor Optimization with fmincon!

MIT 10.37 students and other chemical engineers in reactor enegineering might find this post interesting. Here I’m going to talk about how to apply `fmincon`

to typical reactor engineering optimization problems.

### Basic `fmincon`

usage

Consider the optimization problem below,

\[\underset{X}{\text{minimize}}\ 100 \left( x_2-x_1^2 \right) ^2 + \left( 1-x_1\right) ^2\] \[s.t.\ x_1 + 2x_2 \leq 4\] \[2x_1 + x_2 = 1\] \[0 \leq x_1 \leq 0.4\] \[0 \leq x_2 \leq 0.4\]it only takes three steps to translate into matlab code as shown below.

There’s an alternative way of translating constraints, instead of using `A`

, `b`

, `Aeq`

, `beq`

, `lb`

, and `ub`

, we can use `c`

and `ceq`

.

Matlab tutorial has well covered typical ways of using `fmincon`

to perform optimization. Please check it out if you want to know more.

### Reactor optimization example

Now with the basic knowledge of using `fmincon`

, let’s apply it to our reactor design domain. A good example is 2016-02-22’s 10.37 recitation problem. It has a typical reaction pathway where reactant goes to desired product, which however will react to form an undesired product. As chemical engineers, we should manipulate knobs, e.g., temperature, residence time, etc., in order to maximize molar yiled of desired product.

#### Define objective function

As we discussed, here we want to maximize molar yield of `D`

(diglyceride) by tuning \(T\) (temperature), \(C_{T,0}\), \(C_{CH_3O,0}\), \(C_{CH_3OH,0}\) and \(\tau\). Putting it into mathematic term would be

Translating it to matlab, we can define in below:

#### Set up constraints

Almost all the real-life chemical engineering optimization problems have certain number of constraints. They are usually from `mass balance`

, `kinetics rules`

, `physical bounds`

, etc.

- Mass balance

For convenience, we assume volumetric flowrate is not changed from inlet to outlet. so we have the following equations for major species’ mass balance in CSTR.

\[0 = - C_D + (3C_T - 2C_D)*kC_{CH_3O}\tau\] \[0 = - C_{T,0} - C_T - 3C_T*kC_{CH_3O}\tau\] \[0 = C_{CH_3O,0} - C_{CH_3O}\] \[0 = C_{CH_3OH,0} - C_{CH_3OH} - (3C_T - 2C_D)*kC_{CH_3O}\tau\]- Kinetics rules

Kinetics coefficient `k`

has temperature dependence according to Arrhenius law.

- Physical bounds

Physical bounds are those constraints that seem obvious to you but can largely reduce the search space for Matlab. So in order to get correct answer more quickly, we should provide as much extra information of physics as possible.

For instance, the concentrations and residence time can not be negative, and temperature should always be grater than 273 K (otherwise frozen). Here we will add another constraint where temperature cannot go beyound 350 C because of heating power limitation.

In summary we have,

\[0 \leq C_T\] \[0 \leq C_{CH3O}\] \[0 \leq C_{CH3OH}\] \[0 \leq \tau\] \[273 K \leq T \leq 623 K\]Put all these constraints together in Matlab using `c`

and `ceq`

,

#### call `fmincon`

to optimize

Simply run the optimization by calling `fmincon`

and feeding `objFun`

and `allcon`

we defined early.

Eventually you will have optimal yield of diglyceride to be 30%. You can find the Matlab codes here.