# Design Optimization Using Lookup Table Requirements for Gain Scheduling (GUI)

This example shows how to tune parameters in a lookup table in a model that uses gain scheduling to adjust the controller's response to a plant that varies. Model tuning uses the Response Optimizer app.

### Ship Steering Model

```open_system('sdoShipSteering') ```

This model implements the Nomoto model which is commonly used for ship steering. The dynamic characteristics of a ship vary significantly with factors such as the ship speed. Therefore the rudder controller should also vary with speed, in order to meet requirements for steering the ship.

To keep the ship on course, a control loop compares the ship's heading angle with the reference heading angle, and a PD controller sends command signals to the rudder. The Ship Plant block implements the Nomoto model, a second order system whose parameters vary with the ship's speed. The ship is initially traveling at its maximum speed of 15 m/s, but it will slow down when the reference trajectory specifies a turn in the water. This turning, along with the force of the engine, is used by the Force Dynamics block to compute the speed of the ship over time. The Kinematics block computes the trajectory of the ship.

Open the Controller block by double clicking it.

When the speed changes, the ship plant also changes. Therefore the PD controller gains need to change, and speed is used as the scheduling variable. The controller is in the form `K(1 + sTd)` where `K` is the overall gain and `Td` is the time constant associated with the derivative term. Gain scheduling is implemented using lookup tables, and the table data are specified by `K` and `Td`. These are vectors which specify different values for different speeds. The different speeds are specified in the lookup table breakpoint vectors `bpK` and `bpTd`.

### Design Problem

The reference specifies that at 200 seconds, the ship should turn 180 degrees and reverse course. One requirement is that the ship heading angle needs to match the reference heading angle within an envelope. For the safety and comfort of passengers, a second requirement is that the total acceleration of the ship needs to stay within a bound of 0.25 g's, where 1 g is the acceleration of gravity at Earth's surface, 9.8 m/s^2.

The controller parameter vectors `K` and `Td` will be design variables, and will be tuned to try to meet the requirements. If it is not possible to meet both requirements, then the lookup table breakpoints `bpK` and `bpTd` will also be used as design variables. In that case, we will need to specify an additional requirement that `bpK` and `bpTd` must be monotonically strictly increasing, because this is required for breakpoint vectors in Simulink lookup tables.

### Open the Response Optimizer

In the Apps tab, click Response Optimizer under Control Systems.

### Specify Design Requirements

Specify the requirements which must be satisfied. First, the ship should follow the reference trajectory. Since the reference is essentially a step change from 0 to 180 degrees, you specify a step response envelope for the ship heading angle. In the toolstrip, click New and select Step Response Envelope. Set the initial value to 0 and the final value to pi radians. Set the step time to 200 seconds. Set the rise time as 75 seconds and the rise percent to 85%. Set the settling time to 200 seconds and the settling percent to 1%. Set the percent overshoot to 5%. To specify that this requirement applies to the ship heading, click +.

In the Simulink model click the ship heading signal, which is the output of the Ship Plant block. Select this signal in the Create Signal Set dialog and click OK.

The second requirement is that for the safety and comfort of passengers, the total acceleration should not exceed 0.25 g's at any time. The total acceleration is composed of two components, a tangential component along the direction of the ship's motion, and a normal (horizontal) component. The requirement that total acceleration not exceed 0.25 g's corresponds to requiring that in the phase plane of tangential and normal acceleration, this ship's trajectory remain within a circle of radius 0.25*9.8.

In the toolstrip, click New and select Ellipse Region Constraint. Specify the name as `SafeAccel`, and the semi-axis length for both signals as 0.25*9.8 = 2.45. To specify that the requirement applies to the tangential acceleration of the ship, click the Select button. In the Simulink model click the tangential acceleration signal, which is output from the Kinematics block. Select this signal in the Create Signal Set dialog and click OK. Similarly, to specify that the requirement applies to the normal acceleration of the ship, in the Ellipse Region Constraint dialog click the other Select button, and use the Create Signal Set dialog to specify the normal acceleration signal.

### Specify Design Variables

Specify the design variables to be tuned by the optimization in order to satisfy the requirements. In the toolstrip, click the selection box next to Design Variables Set and then click New. Select the gains of the PD controller, `K` and `Td`, and click the arrow button to designate them as design variables. Use -0.1 for all entries in the value of the `K` vector, and use 50 for all entries in the value of the `Td` vector, and click OK. If the requirements can't all be satisfied, then later the breakpoint vectors `bpK` and `bpTd` can also be tried as design variables.

### Optimize Lookup Table Data

During optimization, the Simulink solver may generate a warning if the size of the time step becomes too small. Temporarily suppress this warning.

```warnState = warning('query', 'Simulink:Engine:SolverMinStepSizeWarn'); warning('off', 'Simulink:Engine:SolverMinStepSizeWarn'); ```

In the Response Optimizer, click Optimize. The ship heading angle does not meet the required step response envelope, as can be seen in the step response plot in the Response Optimizer app and in the Optimization Progress dialog, where the value at the last iteration is still positive, which indicates violation of the requirement. The requirement for safe acceleration is also not met, as seen in the Optimization Progress dialog, where the value at the last iteration is also positive.

### Optimize Lookup Table Data and Breakpoints

To try to meet the design requirements, use the optimization result from above as the start point, and tune additional variables. Add breakpoints `bpK` and `bpTd` as design variables. The ship's maximum speed is 15 m/s, and during turning it may slow to 60% of the maximum speed, or 9 m/s. Set the breakpoint initial values to be equally spaced between 9 and 15 m/s. Constrain the breakpoint minimum values to 9 m/s, and constrain the breakpoint maximum values to 15 m/s.

Breakpoints in the Simulink lookup table block must be strictly monotonically increasing. Add this to the design requirements.

In the Response Optimizer, click Optimize. This time the ship heading angle meets the required step response envelope, as can be seen in the step response plot in Response Optimizer app and in the Optimization Progress dialog, where the value at the last iteration is negative, which indicates the requirement is satisfied. The requirement for safe acceleration is also satisfied, as seen in the Optimization Progress dialog, where the value at the last iteration is also negative. Similarly, the lookup table breakpoints satisfy the monotonic requirements.

In this example, the ship plant varied with the ship speed, so the controller gains also needed to vary. Gain scheduling was implemented using lookup tables. By tuning the gains and breakpoint values in the controller, the ship was able to follow the reference heading angle, while also constraining total acceleration to ensure a safe and comfortable ride for passengers.

### Related Examples

To learn how to optimize the lookup tables in the gain scheduled controller using the `sdo.optimize` command, see Design Optimization Using Lookup Table Requirements for Gain Scheduling (Code).

```% Close the model and restore state of warnings. bdclose('sdoShipSteering') warning(warnState); % restore state of warning ```