Main Content

Model a State-Space System Using S-Function Builder

In this example, you will use the basic methods in the S-Function Builder to model a two-input, two-output discrete state-space system with two states. The state-space matrices are parameters to the S-function, and the S-function input and output are vectors.

If you would like to study a manually written version of the created S-function, seedsfunc.c. Note that to create a S-function from an example S-Function Builder model, you need to build the model first.

Configure the S-Function Builder Settings

Specify the number of discrete states and their initial conditions, the sample mode, and the sample time of the S-function. This example contains two discrete states, each initialized to1, and a discrete sample mode with a sample time of1. Ensure that theDirect feedthroughis selected, because the current values of the S-function inputs are used to compute its outputs.

S-function builder settings with the number of discrete states set to 2, discrete states set to 1 comma 1, number of continuous states set to 0. Array layout set to column-major. Sample mode set to discrete. Sample time value is grayed out. Number of P works set to 0. Enable access to SimStruct is unchecked. Direct feedthrough is checked.

Define Ports and Parameters

Use thePorts and Parameterstable on the bottom of the editor to specify the ports and parameters of the S-function. For this example, we have one input, one output port, and four parameters.

To set or change the values of the block parameters, you can:

  • Double-click theS-Function Builderblock on the model.

  • Use theBlock Parametersfrom the context menu.

Alternatively, you can store the state-space matrices in variables in the MATLAB®workspace and enter the variable names into theValuefield for each parameter. Enter the values in the image for state-space parameters on theValuefield of theBlock Parameterstable.

S Function Builder block parameters table

Define the Output Method

In this example, TheOutputs_wrappermethod calculates the S-function output as a function of the input and state vectors and the state-space matrices. In the outputs code, reference S-function parameters using the parameter names defined on thePorts and Parameterstable. Index into 2-D matrices using a scalar index, again keeping in mind that S-functions use zero-based indexing. For example, to access the elementC(2,1)in the S-function parameterC, useC[1]in the S-function code.

void dsfunc_builder_Outputs_wrapper(const real_T *u, real_T *y, const real_T *xD, const real_T *xC, const real_T *A, const int_T p_width0, const real_T *B, const int_T p_width1, const real_T *C, const int_T p_width2, const real_T *D, const int_T p_width3) { /* Output_BEGIN */ y[0]=C[0]*xD[0]+C[2]*xD[1]+D[0]*u[0]+D[2]*u[1]; y[1]=C[1]*xD[0]+C[3]*xD[1]+D[1]*u[0]+D[3]*u[1]; /* Output_END */ }

Define the Update Method

TheUpdate_wrappermethod updates the discrete states. As with the outputs code, use the S-function parameter names and index into 2-D matrices using a scalar index, keeping in mind that S-functions use zero-based indexing. For example, to access the elementA(2,1)in the S-function parameterA, useA[1]in the S-function code. The variablexD商店final values of the discrete states. Enter the following code in theUpdate_wrapperfunction.

void dsfunc_builder_Update_wrapper(const real_T *u, real_T *y, real_T *xD, const real_T *A, const int_T p_width0, const real_T *B, const int_T p_width1, const real_T *C, const int_T p_width2, const real_T *D, const int_T p_width3) { /* Update_BEGIN */ real_T tempX[2] = {0.0, 0.0}; tempX[0]=A[0]*xD[0]+A[2]*xD[1]+B[0]*u[0]+B[2]*u[1]; tempX[1]=A[1]*xD[0]+A[3]*xD[1]+B[1]*u[0]+B[3]*u[1]; xD[0] = tempX[0]; xD[1] = tempX[1]; /* Update_END */ }

Build the State-Space System

Click the arrow underBuildand select the following options:

  • Show compile steps

  • Create a debuggable MEX-file

  • Generate wrapper TLC

To learn more about what each option does, seeBuild S-Functions Automatically Using S-Function Builder.

To build your S-function, clickBuildon the toolstrip to create an executable file for this S-function. You can now run the model and compare the output to the original discrete state-space S-function contained insfcndemo_dsfunc.

See Also

|

Related Topics