Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How can i get the updated contnuous states xC in mdlDerivatives block itslef (C-S Function)

조회 수: 4 (최근 30일)
Arun
Arun 2013년 6월 13일
마감: MATLAB Answer Bot 2021년 8월 20일
I am creating a model of a component, by discretizing the component to ncells (ncells being 5 or 10). # The model is calculated in a for loop. # The inputs to the first cell is coming from workspace. # The input to the rest of the cells (2,3,4...) is basically the output (continuous state) of the previous cell. # For calculating the continuous states, two derivatives are calculated over time in mdlDerivatives(SimStruct *S). # Important point is, continuous states of all the ncells has to be calculated in one shot in a FOR loop in mdlDerivatives block and finally update the output in mdlOutputs.
The question is, Is it possible to get the updated continuous states in mdlDerivatives itself, without switching back to mdlOutputs everytime to get the continuous states. Since its a for loop, i have to update all the states of ncells before i exit the loop.
If getting the continuous states in mdlDerivatives is not possible, pls suggest a possible alternative to do my calculation in loop.
The thing i had tried already :
I also had a look at vdpmex.c where continuous states are updated in mdlDerivatives, but it doesn't have any output from the model. because they use the below code. vdpmex uses the below code.
static void mdlOutputs(SimStruct *S, int_T tid)
{
UNUSED_ARG(S); /* unused input argument */
UNUSED_ARG(tid); /* not used in single tasking mode */
}
But for my model, an output is also necessarry and hence i cant implement the method in vdpmex.
I am using S function builder and also making necessary changes in wrapper.C
Below is the main part of C code i am using for my model
void comp_Model_Outputs_wrapper(arg_1,arg_2,real_T *X1_out,real_T *X2_out,real_T *xC,..........,arg_n)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
for(i=0;i<N_CELLS;i++)
{
X1[i] = xC[i*NOUTPUTS+pos1]; // Assigning the outputs to Simulink *** X1 and X2 are outputs***
X2[i] = xC[i*NOUTPUTS+pos2]; // Assigning the outputs to Simulink
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
}
void Comp_Model_Derivatives_wrapper(arg_1,arg_2,real_T *dx, real_T *xC,...arg_n)
{
/* %%%-SFUNWIZ_wrapper_Derivatives_Changes_BEGIN --- EDIT HERE TO _END */
for(i=0;i<N_CELLS;i++) // States Initialization
{
X1vec[i] = xa_vec[i]
X2vec[i] = xb_vec[i]
}
// =========================================================================
// Initialize inputs for the first cell
// =========================================================================
Ain_cell = Ain;
Bin_cell = Bin;
Cin_cell = Cin;
//********************************************************************************/
/* loop for cells*/
//********************************************************************************/
for(i=0;i<N_CELLS;i++)
{
X1 = X1vec[i];
X2 = X2vec[i];
//Calculations of components
C1 = Param1*(Ain_cell-X1);
C2 = Param2*(Ain_cell-X1);
C3 = Param3*((Bin_cell-(2*X2)+Inp3)/dm);
C4 = Param4*(X2-Input4);
C5 = Param5*(X2*X2-Inp4*Inp4);
dx[i*NOUTPUTS+pos1] = (C2-C1)/Param6; // This is a derivative
dx[i*NOUTPUTS+pos2] = (C3+C1-C4-C5)/Param7; // This is a derivative
X1vec[i] = xC[i*NOUTPUTS+pos1]; // Getting updated continuous states
X2vec[i] = xC[i*NOUTPUTS+pos2]; // Getting updated continuous states
Ain_cell = X1vec[i]; // Using the updated continuous states as input for next cell.
Bin_cell = X2vec[i]; // Using the updated continuous states as input for next cell.
}
/* %%%-SFUNWIZ_wrapper_Derivatives_Changes_END --- EDIT HERE TO _BEGIN */
}

답변 (1개)

Kaustubha Govind
Kaustubha Govind 2013년 6월 13일
No, I don't believe what you're asking for is directly possible. The Simulink Solver performs the integration after mdlDerivatives returns the state derivatives, so whatever you have access to in the xC vector immediately is probably the states from the previous time-step.
Essentially, it seems to me that you are asking for the n-th integral of the state in a single time-step, which I don't think is technically possible. You need to wait n time-steps to get the n-th integral.

이 질문은 마감되었습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by