# Robust Algorithm Software for NACA 4-Digit Airfoil Shape Optimization Using the Adjoint Method
## 3 Methodology
### Adjoint Equation  
The progress made in design procedures relies on the cost function (\(I\)). Parameters such as the pressure distribution on an object or the lift-to-drag ratio in reverse problems may be used to evaluate the cost function. When dealing with the flow around aerodynamic structures or wings, the cost function (\(I\)) represents aerodynamic features and involves the flow field \(w\) and the boundary geometry \(\mathcal{F}\). Therefore, the cost function may be viewed as a function of these two variables.  
\[I=I(w,\mathcal{F}). \tag{5}\]  
As a result, modifying the boundary \(\mathcal{F}\) can cause a corresponding alteration in the cost function  
\[\delta I=\left[\frac{\partial I}{\partial w}\right]^{T}\delta w+\left[\frac{ \partial I}{\partial\mathcal{F}}\right]^{T}\delta\mathcal{F}. \tag{6}\]  
The initial term pertains to alterations in the cost function with the flow variables, whereas the subsequent term pertains to modifications in boundary geometry variables. When using control theory, flow field equations should not generate multiple solutions, ensuring the removal of \(\delta w\) from Equation (6). If we denote the flow equations as \(R\), their general forms can be categorized into two types, based on the flow and geometric variables  
\[R=R(w,\mathcal{F}). \tag{7}\]  
As a result, modifying the boundary \(\mathcal{F}\) leads to  
\[\delta R=\left[\frac{\partial R}{\partial w}\right]\delta w+\left[\frac{ \partial R}{\partial\mathcal{F}}\right]\delta\mathcal{F}=0. \tag{8}\]  
When applying the finite difference method, one can obtain \(\delta w\) from Equation (8) and insert it into Equation (6). However, this approach is not practical in complex and multivariate problems, such as fluid flow fields, and can lead to issues with convergence and obtaining the correct solution. The adjoint method offers an alternative solution by removing \(\delta w\)-dependent terms from Equation (6). This can be achieved by defining the Lagrangian coefficients \(\psi\) and assuming that \(\delta R=0\). Then, \(\delta I\) can be expressed as \[\delta I =\delta I-\psi^{T}\delta R \tag{9}\] \[\delta I =\left[\frac{\partial I}{\partial w}\right]^{T}\delta w+\left[\frac{ \partial I}{\partial\mathcal{F}}\right]^{T}\delta\mathcal{F}\quad.\] \[-\psi^{T}\bigg{(}\left[\frac{\partial R}{\partial w}\right] \delta w+\left[\frac{\partial R}{\partial\mathcal{F}}\right]\delta\mathcal{F} \bigg{)}\]  
The above sentences can be rearranged as  
\[\delta I =\left(\left[\frac{\partial I}{\partial w}\right]^{T}-\psi^{T} \left[\frac{\partial R}{\partial w}\right]\right)\delta w \tag{10}\] \[+\left(\left[\frac{\partial I}{\partial\mathcal{F}}\right]^{T}- \psi^{T}\left[\frac{\partial R}{\partial\mathcal{F}}\right]\right)\delta \mathcal{F}.\]  
The arbitrariness of the Lagrangian coefficients \(\psi\) allows for the first component of Equation (10) to be zeroed, as  
\[\left[\frac{\partial I}{\partial w}\right]^{T}-\psi^{T}\left[\frac{\partial R }{\partial w}\right]=0. \tag{11}\]  
By eliminating the dependencies of the changes of the cost function \(\delta I\) on the flow variable \(\delta w\), only the geometric boundary variable remains. Equation (11) is referred to as the adjoint equation, where the Lagrangian coefficients \(\psi\) are the unknowns. When this equation is applied to Equation (10), \(\mathcal{G}\), the gradient of the cost function (with respect to the boundary geometric variables) is obtained as follows  
\[\mathcal{G}=\frac{\delta I}{\delta\mathcal{F}}=\left[\frac{\partial I}{ \partial\mathcal{F}}\right]^{T}-\psi^{T}\left[\frac{\partial R}{\partial \mathcal{F}}\right]. \tag{12}\]