Daniel Lofaro - Matlab/Simulink NN Toolbox Tutorial

Neural Networks using Matlab’s Simulink Toolbox:

To demonstrate the ease of the Matlab Neural Network (NN) Toolbox I have taken a simple second order system, see Equation 1 below, and applied unity gain negative feedback via the use of NN.  Equation 1 below is a model of a DC motor. 


Equation 1

In order to make, and compare, a NN control we will need to make three different Simulink files.  The first file we will create will be our system model file.  Figure 1 below shows the Simulink model of Equation 1 above.  Please note that for this system there is one ‘in’ block and one ‘out’ block.  This file was saved as MVP_Motor_Model.mdl.

Figure 1: File 1: MVP_Model _Motor_Model.mdl

Because our goal is to have a closed loop system controlled via a unity gain negative feedback controller that is exactly what we are going to make in our next file.  First we are going to save MVP_Motor_Model.mdl  and then do a ‘save as’ command and make a copy of it that is called MVP_Motor_Model_pd.mdl.  We now have two identical files. 


We will now edit the MVP_Motor_Model_pd.mdl so it has a unity gain negative feedback control.  Please note that we do not need the ‘scope’ so it has been removed.  Please see Figure 2 below for the


Figure 2: File 2: MVP_Motor_Model_pd.mdl

Now that we have these two files we can create our main file.  Figure 3 below shows the main file for the system, MVP_NN_Control_R3.mdl.

Figure 3: File 3: MVP_NN_Control_R3.mdl

 In Figure 3 above you will note that we have a Model Reference Controller from the NN Toolbox in Simulink as well as our closed loop system, bottom middle, and our open loop system top right.  The two transfer function blocks are the transfer function G(s) as found in Equation 1 above.  The closed loop system is on the bottom because it will be compared against the NN controlled system later on in the demonstration.

Our first step to making a NN controller for the system is to double click on the Model Reference Controller block and then click on the “Plant Identification” button in the bottom left corner.  This will bring you to Figure 4 below.  Be careful the new window looks a lot like the one you clicked the button on.


Figure 4: Plant Identification


From here we will choose the maximum and minimum inputs as well as the maximum and minimum time intervals for the system.  The number of hidden layers, as well as delayed I/O’s must be chosen as well.  You then click on the ‘Browse’ button next to “Simulink Plant Model”.  Choose File 1 from above.  The system will automatically generate data for the NN controller.  In order to get a good amount of data I have chosen to get 6000 data points.  Click “Generate Training Data”.

After that is done set the number of Epochs to 300 and click “Train Network”  this will try to minimize the mean squared error in 300 tries.  This will take about a minute.  After it has completed you should get something to the effect of Figure 5 below.  Now click “ok”.  This will bring you back to the first screen

Figure 5: Trained Data Trainlm and 300 epocs and data saved to (Model_tr_data.mat)

Figure 6 below is the Model Reference Control Dialog box.  This is basically the same as the System Identification Dialog box, Figure 4, except this time we will load our desired control, File 2, instead of the system mode.  After generating the training data as described above you are ready to train the entire controller.  For this system 100 Epochs have been chosen for with the system being cut in to a single piece.  Click “Train Controller” .  It is important to note that this will take in excess of an hour.

After the system has been trained you will get something to the effect of Figure 8, the mean squared error reduction, and Figure 9, the I/O of the NN controller.

Figure 6: Model Reference Control Dialog Box

Figure 7: training against the desired pd model 100 epocs




Figure 8: reference model NN plot

The system is now trained.  Because the desired system and the NN controlled system are both hooked up to the scope in Figure 3 the outputs will be able to be compared.  As you can see from Figure 9 below the NN controller, blue, follows the desired controller, yellow, rather well.  Note that the input is the purple input.  It is also important to note that the system follows a ramp input as well, see Figure 10.

Figure 9: Output

Figure 10: Output to ramp input

Click here to email me