Optimization Using MATLAB’s Genetic Algorithm Function (Tutorial)

 Intro: How does a Genetic Algorithm (GA) work?


A genetic algorithm or “GA” follows an iterative process that mimics naive natural selection in order to optimize certain system parameters. In order to apply it for system optimization we need to break down the problem that we are trying to solve.

Here is a simple example in which the optimal geometric parameters (height, width, and length) of a simple cantilever beam are found in order to minimize the resulting deflection from an applied load. Here is a diagram of the system:

beam-2d beam-3d

Now,  we can recall from our statics course that the maximum deflection of a simple cantilever beam with a load on the free end can be calculated as:

δ = PL³/3EI


δ = maximum deflection (meters)
P = applied load (Newtons)
L = beam length (meters)
E = Young's Modulus of Elasticity (GPa)
I = moment of inertia (kg.m²)

Now we can start to create a MATLAB function that will evaluate this system and compute the maximum deflection at the free end of the beam under an applied load of 200N as shown above. First, let’s consider what we want the input and output of our objective function to be. The overall goal is to find the optimal geometric parameters (height, width, length) that will result in the smallest amount of deflection on the loaded tip of the beam. The maximum calculated deflection will be used in order to measure the “fitness” during our optimization [minimize δ(height, width, length) –> better fitness]. Here is our objective function with the maximum deflection being output with height, width, and length being the input and optimized:

beam-defl-code0Once we have our objective function we can call the built-in genetic algorithm function in Matlab in three different ways. For the purpose of this tutorial I am using a simple calling script, however, you can also use the command line or the optimization toolbox in Matlab. Here is our calling script:

calling-codeIn the calling script above we have set our objective function which will output the maximum tip deflection (@beam_defl). This is the function that the genetic algorithm will use to measure the level of fitness of the system and will work to find the optimal parameters in order to minimize the deflection. We have declared the number of variables and the upper and lower bounds (max and min) for our geometric variables. The GA “options” are set in order to set the PopulationSize and the number of Generations.

Before we run the optimization let’s make some predictions as to what we think the optimal parameters will be. We can see from the equation for maximum deflection (δ) that the three geometric parameters influence deflection differently. For a simple cantilever beam, the deflection will be reduced by increasing the height and width of the cross-section and reducing the length. For this reason we can hypothesize that the GA optimization will favor a “thicker” cross-section and a very short beam. Below we see the figure that shows the final fitness (deflection) value of 4E-17 meters. In the subplot for Current Best Individual we see that the solver finds the optimal values for width and height (variables 1 & 2) are at the upper bounds value of 100 (max width and height). The third variable which represents the beam length is found to be optimal at the lower bounds specified to be 1. So, our prediction for the optimal parameters values was correct.


This is a very simple demonstration of a very powerful tool within Matlab that can be applied to a range of optimization scenarios. I left out additional constraint options including linear and non-linear equalities and inequalities as well as a non-linear constraint function. These can be applied to further constrain the optimization. A very simple example would be to constrain the volume of the beam in order to save on materials/manufacturing cost. Additional documentation and examples are available on the Mathworks website. If I have time I will add additional information to this tutorial.