Example: Modeling a Pitch Controller in Simulink

## Physical setup and system equations

The equations governing the motion of an aircraft are a very complicated set of six non-linear coupled differential equations. However, under certain assumptions, they can be decoupled and linearized into the longitudinal and lateral equations. Pitch control is a longitudinal problem, and in this example, we will design an autopilot that controls the pitch of an aircraft. In this example, we will begin with the linearized state-space equation model which was obtained in the MATLAB Tutorials: pitch control example This model, with numerical values substituted in, is as follows:

in state-space form, the equations are as follows:
Which can be written as:

## Building the state-space model

We will model these state equations explicitly in Simulink taking advantage of vector lines to carry the state vector signal X. Vector lines are produced automatically in Simulink when blocks input or output vector signals. First, we will represent the derivative of the state.
• Open a new model window in Simulink.
• Insert an Integrator block from the Linear block library.
• Draw a line from the output of the Integrator and label it "X". To label a line, double click near the line where you want the label to appear.
• Draw a line leading to the input of the Integrator and label it "d/dt(X)"

Now, we will represent the first state equation, which is d/dt(X)=Ax+Bu.

• Insert a Matrix Gain block from the Linear block library below the integrator.
• Flip this block left-to-right. To do this, select it with the mouse (single-click) and select Flip Block from the Format menu (or hit Ctrl-F).
• Edit the Matrix Gain block (by double clicking). Change the Gain Matrix field to "A" (which we will define later in MATLAB) and close the dialog box.
• Change the label of this block from Matrix Gain to "A" by single clicking on the existing label.
• Tap a line off the state signal, X (hold Ctrl while drawing the line to tap) and connect it to the input of the A block. This creates the signal Ax.
• Insert a Sum block (from the Linear block library) to the left of the integrator and connect it's output to the line leading to the Integrator's input.
• Draw a line from the output of the A matrix block to the lower (positive) input of the Sum block.
• Insert another Matrix Gain block to the left of the Sum block. Change it's Matrix Gain value to "B" and change it's label to "B".
• Connect the B matrix output to the other (positive) input of the Sum block.
• Draw a line leading to the input of the Sum block. This is the input signal, u.

Now we will form the output signal which is equal to Cx+Du.

• Insert a Matrix Gain block to the right of the integrator and attach the integrator's output to the input of the Matrix Gain.
• Change the Matrix Gain value to "C" and change it's label to "C".
• Insert another Matrix Gain block below the "C" block, and change both it's value and label to "D".
• Tap a line off the u signal (the input line of the B matrix block) and attach it to the input of the "D" matrix block.
• Insert a Sum block to the right of the C matrix block.
• Connect the outputs of the C and D matrix blocks to the inputs of the Sum block.

Next, we will apply inputs and extract outputs from this system. We will use In and Out blocks for this purpose. These blocks allow the system to be extracted into MATLAB and they allow the system to be placed into a Subsystem block for use within another model.

• Insert an In block (from the Connections block library) and connect it to the open line which connects to the input of the B matrix block.
• Change the label of the In block to "deltac" (corresponding to the angle of the elevator).
• Insert an Out block (from the Connections block library) and connect it to the output of the rightmost Sum block.
• Change the label of this Out block to "theta" (corresponding to the pitch angle of the plane).
• Insert an Out block above the Theta block, and change it's label to "X" corresponding to the state vector of the system. (This will be used later to implement full-state feedback.
• Tap a line off the X signal (leading from the second integrator) and connect it to the X Out block.

## Open-loop response

To generate the open-loop response, it is first necessary to contain this model in a subsystem block.
• Create a new model window (select New from the File menu in Simulink or hit Ctrl-N).
• Insert a Subsystem block from the Connections block library.
• Open the Subsystem block by double clicking on it. You will see a new model window labeled "Subsystem".
• Open your previous model window named pitch.mdl. Select all of the model components by selecting Select All from the Edit menu (or hit Ctrl-A).
• Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit Ctrl-C).
• Paste the model into the Subsystem window by selecting Paste from the Edit menu (or hit Ctrl-V) in the Subsystem window
• Close the Subsystem window. You will see the Subsystem block in the untitled window with one input terminal labeled deltac and two output terminals labeled theta and x.
• Resize the Subsystem block to make the labels visible by selecting it and dragging one of the corners.
• Label the Subsystem block "Plane Pitch Model".
• Insert a Step block (from the Sources block library) and connect it to the input of the Plane Pitch Model.
• Edit the Step block (by double clicking on it to bring up the dialog box) and change the Step Time value to 0. Close the Step block dialog box.
• Insert a Scope block (from the Sinks block library) and connect it to the theta output of the Plane Pitch Model.

Before obtaining a step response, we must set the constants in the matrices A, B, C, and D. Enter the following commands at the MATLAB prompt.

```A=[-0.313 56.7 0; -0.0139 -0.426 0; 0 56.7 0];
B=[0.232; 0.0203; 0];
C=[0 0 1];
D=[0];
```
We are now ready to run the simulation. If you like, you can download our version of the open-loop system here. Start the simulation by selecting Start from the Simulation menu (or hit Ctrl-t). When the simulation is finished, open the Scope by double clicking on it and hit the Scope's autoscale button. You will see the following response.

## Extracting the model into MATLAB

The Simulink model can be extracted into an equivalent state-space model in MATLAB. This is done through the use of In and Out Connection blocks and the MATLAB function linmod.

To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as pitch.mdl. In this model, one input, deltac (the elevator angle) and two outputs, theta (pitch angle) and X (the state vector), were defined. We must truncate the output and feedthrough matrices (Co and Do) to provide only the first input.

At the MATLAB prompt, enter the following commands

```  [Ao,Bo,Co,Do]=linmod('pitch');
Ao
Bo
Co=Co(1,:)
Do=Do(1,:)
```
You will see the following output providing the open-loop model of the system.
```Ao =

-0.3130   56.7000         0
-0.0139   -0.4260         0
0   56.7000         0

Bo =

0.2320
0.0203
0

Co =

0     0     1

Do =

0
```
Note that the matrices Ao, Bo, Co, and Do are the same at the original matrices A, B,C, and D.

## Building a full-state feedback controller

In the CTMS Example: Pitch Controller -- State Space Method page, a full-state feedback controller was designed using the LQR method. We will now construct this controller in Simulink.
• Insert a Matrix Gain block below the Plane Pitch Model and change both its Matrix Gain value and it's label to "K".
• Flip the K matrix block left-to-right.
• Insert a Sum block to the left of the Plane Pitch Model and change it's value to "+-".
• Connect the state vector X output of the Plane Pitch Model to the input of the K matrix block.
• Connect the output of the K matrix block to the second (negative) input of the Sum block.
• Connect the Step block to the first (positive) input of the Sum block.
• Connect the Sum block's output to the input of the Plane Pitch Model.

## Closed-loop response

Before simulating the closed-loop system, we must first determine the gain matrix K using the LQR method. Execute the following commands at the MATLAB prompt.
```  p=50;
Q=[0 0 0;
0 0 0;
0 0 p];
[K]= lqr (A,B,Q,1)
```
Start the simulation in Simulink. Open the scope window and hit the Autoscale button. You should see the following response.

Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Pitch Controller Examples
Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

Tutorials
MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples