How to update a new step size in S-Function after keeping every two steps equal

4 visualizaciones (últimos 30 días)
I have given the program flow chart below. The main technical problems of using S-Function to realize the function of this program are as follows : First, S-Function uses variable step size simulation ( this is easy to implement, that is, S-Function is set : ' ts = [ -20 ] ; ' ) ; secondly, it is required that every two steps are equal. For example, the first step and the second step are equal, the third step changes according to the input 'v ' of S-Function, but the fourth step and the third step are equal, and so on. I 've solved this problem logically through three flags ( flagForUpdateFrequency, flagForToggleOutputOne, flagForToggleOutputTwo ) in the given program flow, but I don 't know how to implement this logic in S-Function, because I can 't seem to simply add these three flags in S-Function ( it will be reported wrong ), so I hope to get your help.

Respuesta aceptada

Yash
Yash el 18 de Jul. de 2023
To implement the logic you described using S-Function, you can utilize the S-Function's work vector (work) to store and update the flags. The work vector is a double array that can be used to store additional data within the S-Function. Here's an approach to implementing the logic using the work vector:
  1. In the S-Function's initialization function (mdlInitializeSizes), you need to allocate memory for the work vector to store the flags. You can use ssSetNumPWork(S, N) to specify the number of elements in the work vector (N) and allocate memory for it using ssGetPWork(S)[i] = (void*)malloc(N * sizeof(double)).
  2. Initialize the flags in the work vector in the S-Function's initialization function. For example, you can set all flags to zero.
  3. In the S-Function's update function (mdlUpdate), you can access the work vector using ssGetPWork(S)[i] and update the flags accordingly.
  4. To ensure every two steps are equal, you can use the modulus operator (%) to determine the even and odd steps. If the step is even, update the output based on the input v; if the step is odd, keep the output the same as the previous step. You can use the flag to toggle between the two output values.
  5. Finally, in the S-Function's termination function (mdlTerminate), free the memory allocated for the work vector using free(ssGetPWork(S)[i]).
#define NUM_FLAGS 3
void mdlInitializeSizes(SimStruct *S) {
// Allocate memory for work vector
if (!ssSetNumPWork(S, NUM_FLAGS)) {
return;
}
for (int i = 0; i < NUM_FLAGS; i++) {
ssGetPWork(S)[i] = (void*)malloc(sizeof(double));
*((double*)ssGetPWork(S)[i]) = 0.0; // Initialize flags to zero
}
}
void mdlUpdate(SimStruct *S, int_T tid) {
// Get flags from work vector
double* flagForUpdateFrequency = (double*)ssGetPWork(S)[0];
double* flagForToggleOutputOne = (double*)ssGetPWork(S)[1];
double* flagForToggleOutputTwo = (double*)ssGetPWork(S)[2];
// logic for your s-function implementation.
}
void mdlTerminate(SimStruct *S) {
// Free memory for work vector
for (int i = 0; i < NUM_FLAGS; i++) {
free(ssGetPWork(S)[i]);
}
}
I hope this helps.

Más respuestas (0)

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by