Create a nonlinear MPC controller with four states, two outputs, and one input.

In standard cost function, zero weights are applied by default to one or more OVs because there are fewer MVs than OVs.

Specify the sample time and horizons of the controller.

Specify the state function for the controller, which is in the file `pendulumDT0.m`

. This discrete-time model integrates the continuous time model defined in `pendulumCT0.m`

using a multistep forward Euler method.

The prediction model uses an optional parameter, `Ts`

, to represent the sample time. Specify the number of parameters.

Specify the output function of the model, passing the sample time parameter as an input argument.

Define standard constraints for the controller.

Validate the prediction model functions.

Model.StateFcn is OK.
Model.OutputFcn is OK.
Analysis of user-provided model, cost, and constraint functions complete.

Only two of the plant states are measurable. Therefore, create an extended Kalman filter for estimating the four plant states. Its state transition function is defined in `pendulumStateFcn.m`

and its measurement function is defined in `pendulumMeasurementFcn.m`

.

Define initial conditions for the simulation, initialize the extended Kalman filter state, and specify a zero initial manipulated variable value.

Specify the output reference value.

Create an `nlmpcmoveopt`

object, and specify the sample time parameter.

Run the simulation for `10`

seconds. During each control interval:

Correct the previous prediction using the current measurement.

Compute optimal control moves using `nlmpcmove`

. This function returns the computed optimal sequences in `nloptions`

. Passing the updated options object to `nlmpcmove`

in the next control interval provides initial guesses for the optimal sequences.

Predict the model states.

Apply the first computed optimal control move to the plant, updating the plant states.

Generate sensor data with white noise.

Save the plant states.

Plot the resulting state trajectories.