Virtual Sensors for Biodiesel Production in a Batch Reactor

Fossil fuel combustion produces around 98% of coal emissions. Therefore, liquid and gaseous biofuels have become more attractive due to their environmental benefits. The biodiesel production process requires measurements that help to control and supervise the variables involved in the process. The measurements provide valuable information about the operation conditions and give estimations about the critical variables of the process. The information from measurements is essential for monitoring the state of a process and verifying if it has an optimal performance. The objective of this study was the conception of a virtual sensor based on the Extended Kalman Filter (EKF) and the model of a batch biodiesel reactor for estimating concentrations of triglycerides (TG), diglycerides (DG), monoglycerides (MG), methyl ester (E), alcohol (A), and glycerol (GL) in real-time through measurement of the temperature and pH. Estimation of the TG, DG, MG, E, A, and Gl through this method eliminates the need for additional sensors and allows the use of different types of control. For the performance analysis of the virtual sensor, the data obtained from the EKF are compared with experimental data reported in the literature, with the mean square error of the estimate then being calculated. In addition, the results of this approach can be implemented in a real system, since it only uses measurements available in a reactor such as temperature and pH.


Introduction
The reduction in fossil fuel reserves and growing concerns about their environmental impact has urged the search for new energy sources forward [1][2][3][4][5].Biodiesel is an alternative to conventional diesel as a fuel source because it is biodegradable, non-toxic, and produces low emission of polluting gases, and therefore has environmental benefits [4,6].The production of biodiesel is economically competitive and represents an alternative as an ideal substitute for conventional diesel.
Biodiesel is compounded by mono-alkyl esters obtained by transesterification of an oil or fat with a methoxide [7].Studies have been conducted to reduce biodiesel production costs by controlling the reaction parameters that affect the speed and reaction mechanisms or optimizing their production processes [1,8].The biodiesel production processes require measurements that aid in controlling and monitoring process variables.The measurements provide valuable information about the operating conditions and simultaneously give an estimate for critical variables by monitoring the state of a process and verifying that performance is optimal.There are methods to monitor the progress of the reaction, such as gas and liquid chromatography [9][10][11][12], Fourier transform infrared (FTIR), near-infrared (NIR), and nuclear magnetic resonance (NMR) spectroscopy methods [10,[12][13][14][15][16][17], measurement of refractive index [18], and viscosity [19].Most of these methods require samples to be taken and offline analyses to be performed which include a pre-treatment of the sample.According to the advances made in online concentration analysers, measuring these are usually expensive and difficult to maintain, in addition to possibly introducing a time delay in the control loop [20].Therefore, the effect of an online application of a state estimation technique is to provide an estimate of the composition in real time from measurements of available sensors and the elimination of expensive sensors.This would avoid the consumption of other sensors, and being able to monitor all the online states could make the process more efficient.
Noise in processes is a common feature in chemical and biological reactors [21], and designing a nonlinear filter for a more precise estimation is necessary.The extended Kalman filter (EKF) is widely used for the estimation of states in many engineering applications for nonlinear systems and covers the problems caused by noise.In Reference [22], an extended Kalman filter was used to identify the parameters of a marine riser.In Reference [23], an implementation of a Kalman filter was performed to estimate unknown states and inputs for acceleration measurements.In Reference [20], a real-time estimation for concentrations in a distillation column was developed.In Reference [24], a Kalman filter using a Sigma point for a batch polymerization reactor was conducted.In Reference [25], extended Kalman filtering was used to estimate temperature and irradiation for maximum power point tracking of a photovoltaic module.An estimate was performed for the biomass concentration, production concentration, and substrate concentration in the fermentation process in a batch reactor by state-space modelling and usage of Kalman filters [26].In a plug flow reactor, temperature was estimated by designing a hybrid extended Kalman filter [27].In Reference [28], an estimate of the concentration of existing biogas in a continuously stirred reactor was performed using an extended Kalman filter.
The aim of this work was to design virtual sensors of the elements involved in the transesterification reaction in a reactor producing biodiesel in order to solve the online measuring problem for variables and also to provide tools for control systems.

Transesterification Model
The kinetic study of the transesterification reaction describes the reaction mechanisms and provides the parameters to predict the composition and concentration of the reaction at any point in time and under certain conditions.The reaction mechanism for producing esters is composed of three reversible consecutive reactions (Equations ( 1)-( 3)).The reaction occurs from one mole of triglyceride (TG) reacting with three moles of alcohol (A) to form the intermediate products, diglycerides (DG) and monoglycerides (MG).The final product is methyl ester (E) and glycerol (GL).
Sustainability 2017, 9, 455 3 of 11 Equation (4) shows the final balance of the process: The reaction mechanism of biodiesel production is common, regardless of the source of fatty acids.The general form of the set of differential equations governing the kinetics of the transesterification reaction of triglycerides is as follows (Equations ( 5)-( 10)) [29]: where k 1 , k 2 , k 3 , k 4 , k 5 , k 6 are the rate constants for reaction in units of M/s.The reaction rate in practice is affected by various factors, including temperature.The Arrhenius equation (Equation 11) relates the temperature in the reaction rate, and it is expressed as: where α is the pre-exponential factor, ε is an activation energy, T R is the reactor temperature, and ϑ is the gas constant.
There is a direct relationship between temperature and the transesterification reaction.A high reaction temperature increases the rate of biodiesel formation, but it is limited by the boiling points of the reaction components.The component with the lowest boiling point is methanol (64.5 • C) [29].For this reason, analyses are carried out at different temperatures to determine if the designed estimator had a suitable fit for temperature changes.In this sense, different authors have different simulations of this system model at different temperatures to analyse the effect of this variable in the conversion of triglycerides into methyl esters [29][30][31].

Extended Kalman Filter
In many engineering applications, there are variables and physical parameters that cannot be directly measured, either due to a lack of specific sensors or the expensive cost (in enormous quantities).An alternative for dealing with this issue is to obtain a dynamic estimation of the required variables or parameters by using state observers, also known as virtual sensors.The extended Kalman filter (EKF) is a reasonable estimator for this application, as it is able to handle system nonlinearities and is built to incorporate noise from measurements and processes [25].
A state observer is a dynamic algorithm which is used to estimate the variables of a process using the following: a mathematical model represented in state space, the available measurements of the process (inputs and outputs), and an error correction term to ensure the convergence of the algorithm.
The general representation of a nonlinear state space model is as follows: .
with f and h being smooth functions, where x ∈ R n is the vector of state variables, u ∈ R m is the input vector, and y ∈ R p is the vector of output variables.To derive a general structure of a state observer, let us consider the nonlinear state space model ( 12): model copy where the superscript (ˆ) at any variable denotes estimation, ŷ(t) are the online estimations, whereas K(x(t)) is the gain of the observer.Then, the design of the state observer involves choosing an appropriate gain, K(x(t)), in order to make the estimation error converge to zero.If the observation error e is defined as follows: The dynamics of the error observation are derived from Equations ( 12) and ( 13): .
The word "extended" emphasizes the fact that this observer is an extension of the original linear version to nonlinear systems.The design of the gain matrix K(x) is based on a linearized version (the linearized tangent model) of the observation error from process dynamics (computed from a Taylor series expansion of a state space model around some operating point).Considering the linearization of Equation ( 15) around the observation error e = 0, the following equation is obtained: where A(x) and C(x) are, respectively, equal to: Although the Kalman filter was originally introduced in a stochastic framework, it can also be interpreted as the approximated solution of a deterministic optimization problem.Indeed, the design of the extended Kalman filter consists of finding the gain matrix, K(x), that minimizes the mean square observation error: with the dynamical model Equation ( 16) (under the constraints of the dynamical model, in the usual notations of optimization theory).Hence, the gain is computed as follows: where S(x) is a time-varying matrix calculated through the following Riccati equation: .
For the implementation of the EKF, the concerning matrices must accomplish certain properties: S(0) = S(0) T > 0, Q = Q T ≥ 0, and R = R T ≥ 0, where Q and R are covariance matrices with elements chosen for optimal noise filtering.To ensure the convergence of the filter, one can use the following expression instead of Equation ( 16): .
where I is the identity matrix with appropriated dimensions and α is a constant parameter that provides a stability degree to the estimation.Namely, by means of this parameter, the estimation error is bounded and the estimation rate can be tuned.

Simulation of the Transesterification Reaction
The model of differential equations presented in Equations ( 5)- (10) were used to simulate the transesterification reaction of triglycerides of the oil of jatropha sp.biodiesel at 50 • C, with conditions presented in Table 1.Kinetic constants used were 1.6775, 0.1174, 2.1303, 0, 0.1961, 0.0449 with units M/s, k i for i = 1 ... 6, respectively, according to the data reported in Reference [31].The simulation was carried out in the Matlab ® simulation software using an ode45 solver.The results obtained from simulation of the nonlinear model of the transesterification reaction are shown in Figure 1.It shows that TG was consumed as the reaction proceeded, while E was formed.At the same time, intermediate reactions were carried out that produce and consume MG and DG.These behaviours coincide with the experimental results that have been reported in previous studies [29,31,32].The simulation was carried out in the Matlab ® simulation software using an ode45 solver.The results obtained from simulation of the nonlinear model of the transesterification reaction are shown in Figure 1.It shows that TG was consumed as the reaction proceeded, while E was formed.At the same time, intermediate reactions were carried out that produce and consume MG and DG.These behaviours coincide with the experimental results that have been reported in previous studies.[29,31,32].

Reaction Estimation
This section presents the setup for the simulations used to examine the performance of the proposed estimator.The simulation was carried out in the Matlab Simulink ® software.The EKF estimation was compared with experimental results that have been reported in previous studies [29,31,32].An estimation of intermediate and final products of the transesterification reaction of methanol with Jatropha oil was carried out.Constant reaction temperature (50 °C) simulations and changes of temperature by 20 °C (changing 50 °C to 70 °C and 30 °C) were performed.This was done to analyse the performance of the estimation obtained by the extended Kalman filter.
A block diagram of the Kalman filter extended for use as a virtual sensor is shown in Figure 2. Directly measuring the inlet temperature and indirectly measuring the concentration of E through a pH sensor [33] allows estimation of the six system states.

Reaction Estimation
This section presents the setup for the simulations used to examine the performance of the proposed estimator.The simulation was carried out in the Matlab Simulink ® software.The EKF estimation was compared with experimental results that have been reported in previous studies [29,31,32] A block diagram of the Kalman filter extended for use as a virtual sensor is shown in Figure 2. Directly measuring the inlet temperature and indirectly measuring the concentration of E through a pH sensor [33] allows estimation of the six system states.There is a direct relationship between pH and the transesterification reaction in the production of methyl ester.Changes in OH − concentration due to the conversion of oil to biodiesel are observed by the pH meter during the reaction.It appears that the OH − ions do not dissolve in the oil because of their polar nature, but do dissolve in the biodiesel and glycerol.As the reaction occurs, the oil concentration decreases and the concentration of methyl ester and glycerol increases, causing changes in pH derived from the stoichiometric ratio.Thus, changes in pH are directly correlated with methyl ester concentration.
The linearized model using Equation ( 17) is shown in Equation ( 23): To show the performance of the proposed EKF, the calibration values that were used are shown in Table 2.

Initial condition of S (0) =
Covariance Matrix = 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 The results of the constant reaction temperature (at 50 °C) are shown in Figure 3.It can be observed that the estimated values (green line) can track the experimental values (blue line) in a highly effectively way from one measured state and one input, which are the six concentrations obtained from the transesterification reaction.Furthermore, the mean squared error (MSE) of the signals was calculated to analyse the performance of the observer with the MSE subsequently found to be zero.Since the concentration of triglycerides and diglycerides frequently have a value of 0, it is important to display the correct estimation of the graphs logarithmically.There is a direct relationship between pH and the transesterification reaction in the production of methyl ester.Changes in OH − concentration due to the conversion of oil to biodiesel are observed by the pH meter during the reaction.It appears that the OH − ions do not dissolve in the oil because of their polar nature, but do dissolve in the biodiesel and glycerol.As the reaction occurs, the oil concentration decreases and the concentration of methyl ester and glycerol increases, causing changes in pH derived from the stoichiometric ratio.Thus, changes in pH are directly correlated with methyl ester concentration.
The linearized model using Equation ( 17) is shown in Equation ( 23): To show the performance of the proposed EKF, the calibration values that were used are shown in Table 2.

Parameters Units
1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 The results of the constant reaction temperature (at 50 • C) are shown in Figure 3.It can be observed that the estimated values (green line) can track the experimental values (blue line) in a highly effectively way from one measured state and one input, which are the six concentrations obtained from the transesterification reaction.Furthermore, the mean squared error (MSE) of the signals was calculated to analyse the performance of the observer with the MSE subsequently found to be zero.Since the concentration of triglycerides and diglycerides frequently have a value of 0, it is important to display the correct estimation of the graphs logarithmically.The results of the estimation simulation against a change in the temperature of +20 °C (50 °C to 70 °C) in minute three of simulation are shown in Figure 4.It can be seen that even with a change in the reference system, the EKF continuously provides a perfect estimation for the system.MSE values were calculated, and there was only minimal error.The estimation simulation against a −20 °C temperature change (50 °C to 30 °C) in minute 3 is shown in Figure 5.It can be seen that the slope of the concentration of MG becomes more pronounced and the formation of E and GL is less than the obtained amounts at 50 °C.Furthermore, it demonstrates how the estimator (green line) perfectly follows the change of the system (blue line).The results of the estimation simulation against a change in the temperature of +20 °C (50 °C to 70 °C) in minute three of simulation are shown in Figure 4.It can be seen that even with a change in the reference system, the EKF continuously provides a perfect estimation for the system.MSE values were calculated, and there was only minimal error.The estimation simulation against a −20 °C temperature change (50 °C to 30 °C) in minute 3 is shown in Figure 5.It can be seen that the slope of the concentration of MG becomes more pronounced and the formation of E and GL is less than the obtained amounts at 50 °C.Furthermore, it demonstrates how the estimator (green line) perfectly follows the change of the system (blue line).The estimation simulation against a −20 • C temperature change (50 • C to 30 • C) in minute 3 is shown in Figure 5.It can be seen that the slope of the concentration of MG becomes more pronounced and the formation of E and GL is less than the obtained amounts at 50 • C. Furthermore, it demonstrates how the estimator (green line) perfectly follows the change of the system (blue line).
The Kalman filter estimation is shown in Figure 6 with the introduction of random noise to the process data.In this estimation, a good filter performance is observed and the MSE was calculated in percentages (Table 3).The estimation simulation against a −20 °C temperature change (50 °C to 30 °C) in minute 3 is shown in Figure 5.It can be seen that the slope of the concentration of MG becomes more pronounced and the formation of E and GL is less than the obtained amounts at 50 °C.Furthermore, it demonstrates how the estimator (green line) perfectly follows the change of the system (blue line).The Kalman filter estimation is shown in Figure 6 with the introduction of random noise to the process data.In this estimation, a good filter performance is observed and the MSE was calculated in percentages (Table 3).In Figure 7, the Kalman filter estimation is shown after the introduction of random measurement noise (sensor), with an excellent filter performance observed and the MSE being calculated in percentages (Table 4).In Figure 7, the Kalman filter estimation is shown after the introduction of random measurement noise (sensor), with an excellent filter performance observed and the MSE being calculated in percentages (Table 4).The error of estimation of the states with measurement noise is shown in Figure 8, with the error being close to zero.The convergence of the filter is corroborated according to Reference [34].The prospects for the implementation of virtual sensors for biodiesel production involve developing a fault-tolerant control system with fault diagnosis to support the continuous operation of the production process and consequently achieve an improvement in production by doing this in a more robust and secure manner.In addition, the use of such virtual sensors will allow the implementation of control techniques that require all output variables to be measured.In addition to these, sensors can monitor the reagents and products of the reaction transesterification online.Reactors could even be designed with online readings where it can be visualized whether the reaction is complying with European Standard Automotive fuels-Fatty acid methyl esters (FAME) for diesel engines-Requirements and test methods (EN14214).The error of estimation of the states with measurement noise is shown in Figure 8, with the error being close to zero.The convergence of the filter is corroborated according to Reference [34].The error of estimation of the states with measurement noise is shown in Figure 8, with the error being close to zero.The convergence of the filter is corroborated according to Reference [34].The prospects for the implementation of virtual sensors for biodiesel production involve developing a fault-tolerant control system with fault diagnosis to support the continuous operation of the production process and consequently achieve an improvement in production by doing this in a more robust and secure manner.In addition, the use of such virtual sensors will allow the implementation of control techniques that require all output variables to be measured.In addition to these, sensors can monitor the reagents and products of the reaction transesterification online.Reactors could even be designed with online readings where it can be visualized whether the reaction is complying with European Standard Automotive fuels-Fatty acid methyl esters (FAME) for diesel engines-Requirements and test methods (EN14214).The prospects for the implementation of virtual sensors for biodiesel production involve developing a fault-tolerant control system with fault diagnosis to support the continuous operation of the production process and consequently achieve an improvement in production by doing this in a more robust and secure manner.In addition, the use of such virtual sensors will allow the implementation of control techniques that require all output variables to be measured.In addition to these, sensors can monitor the reagents and products of the reaction transesterification online.Reactors could even be designed with online readings where it can be visualized whether the reaction is complying with European Standard Automotive fuels-Fatty acid methyl esters (FAME) for diesel engines-Requirements and test methods (EN14214).
There is practical importance for implementing virtual sensors for biodiesel production, as these sensors can estimate variables that cannot be measured or are difficult to measure online, in addition to reducing instrumentation costs, since no extra sensors would be needed to measure the concentrations of MG, DG, TG, A, and GL.In addition, it would avoid the need for reading times and reagents for measuring concentrations by destructive and costly analytical techniques such as gas chromatography.The virtual sensors would have the measurements online as a first option, allowing evaluation of whether or not it meets the specifications of standard EN14214.

Conclusions
A Kalman filter was designed and implemented as a virtual sensor to estimate concentrations of the transesterification reaction of Jatropha oil with methanol (A) in a batch reactor to produce fatty acid esters (biodiesel, E) using measurements of temperature and pH sensors.The substances estimated were TG, DG, MG, E, A, and GL.To analyse the performance of the estimation system, we 3 simulated cases: (a) with the temperature inlet to 50 • C; (b) a change in temperature of +20 • C and change of −20 • C; and (c) with random noise in the system and measurement.For analysis of the estimation process, the MSE was calculated in percentages.In every case, the error was practically null, providing evidence that an excellent estimation is performed by the filter and proving its usefulness as a virtual sensor.

Figure 1 .
Figure 1.Kinetic behaviour of the transesterification reaction model at 50 °C.

Figure 1 .
Figure 1.Kinetic behaviour of the transesterification reaction model at 50 • C.
. An estimation of intermediate and final products of the transesterification reaction of methanol with Jatropha oil was carried out.Constant reaction temperature (50 • C) simulations and Sustainability 2017, 9, 455 6 of 11 changes of temperature by 20 • C (changing 50 • C to 70 • C and 30 • C) were performed.This was done to analyse the performance of the estimation obtained by the extended Kalman filter.

Figure 3 .
Figure 3. Estimation performed by the extended Kalman filter at 50 °C.

Figure 6 .
Figure 6.Estimation performed at 50 °C with random noise in the process.

Figure 6 .
Figure 6.Estimation performed at 50 • C with random noise in the process.

Figure 7 .
Figure 7. Estimation performed at 50 °C with random measurement noise.

Figure 8 .
Figure 8. Error of estimation of the states with measurement noise.

Figure 7 .
Figure 7. Estimation performed at 50 • C with random measurement noise.

Figure 7 .
Figure 7. Estimation performed at 50 °C with random measurement noise.

Figure 8 .
Figure 8. Error of estimation of the states with measurement noise.

Figure 8 .
Figure 8. Error of estimation of the states with measurement noise.

Table 3 .
Mean squared error (MSE) for reaction at 50 °C with random noise in the process.

Table 3 .
Mean squared error (MSE) for reaction at 50 • C with random noise in the process.

Table 4 .
Mean squared error (MSE) for reaction at 50 • C with random measurement noise.