Model a State-Space System Using S-Function Builder - MATLAB & Simulink (original) (raw)
In this example, you will use the basic methods in the S-Function Builder block 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, see[dsfunc.c](https://mdsite.deno.dev/matlab:sfunddg%5Fcb%5Fedit%28'dsfunc'%29)
. 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 to 1
, and a discrete sample mode with a sample time of1
. Ensure that the is selected, because the current values of the S-function inputs are used to compute its outputs.
Define Ports and Parameters
Use the Ports and Parameters table 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 the S-Function Builder block icon in the model.
- Use the from the context menu.
Alternatively, you can store the state-space matrices in variables in the MATLABĀ® workspace and enter the variable names into the Value field for each parameter. Enter the values in the image for state-space parameters on theValue field of the Block Parameters table.
Define the Output Method
In this example, The Outputs_wrapper
method 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 Parameters table. 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 element C(2,1)
in the S-function parameterC
, use C[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
The Update_wrapper
method 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 element A(2,1)
in the S-function parameter A
, useA[1]
in the S-function code. The variable xD
stores the final values of the discrete states. Enter the following code in theUpdate_wrapper
function.
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 under Build
and select the following options:
To learn more about what each option does, see Build S-Functions Automatically Using S-Function Builder.
To build your S-function, click Build on 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 in [sfcndemo_dsfunc](https://mdsite.deno.dev/matlab:open%5Fsystem%28[matlabroot,'/toolbox/simulink/sfuntemplates/sfcndemo%5Fdsfunc']%29)
.
See Also
Blocks
Functions
- Simulink.SFunctionBuilder.setSettings | Simulink.SFunctionBuilder.add | Simulink.SFunctionBuilder.build