Calling a Simulink model by using feval

I am working on a Simulink model. There is a trim code which is calling this model by using feval as follows.
feval(model,0,x0,u0,'compile')
Y = feval(model,0,x,u,'outputs')
Xd = feval(model,0,x,u,'derivs')
feval(model,[],[],[],'term')
However, it only gives the outputs of the model but I need the outputs from to workspace blocks too. How can I obtain these outputs?
And also I would like to ask is there any documentation about this kind of usage of feval.

6 comentarios

Luca Ferro
Luca Ferro el 18 de Abr. de 2023
is there a reason why you use feval() instead of sim()?
Yusuf Bayrak
Yusuf Bayrak el 18 de Abr. de 2023
Because feval() is way more faster than sim().
Paul
Paul el 18 de Abr. de 2023
How about using findop and linearize?
Luca Ferro
Luca Ferro el 19 de Abr. de 2023
do you have data that supports the claim that feval is actually faster than sim? Anyways, let's assume you are right, is speed really a concern for you? if not, sim does exactly what you need in one line
Yusuf Bayrak
Yusuf Bayrak el 19 de Abr. de 2023
Findop and linearize may be helpfull if the model I have is a basic one. However, there are too many states, inputs and outputs that are irrevelant with trimming the model so it is too hard to use that kind of functions.
Also I tried to use the sim but it takes more that 2 seconds for every iteration. However, feval takes about 0.08 seconds for every iteration. I need far too much iterations to trim the model properly so I need this speed. It may take hours to trim what sim is used.
Sara Nadeau
Sara Nadeau el 19 de Abr. de 2023
The sim function does much more than what you're calling inside feval here, which is part of the time difference that you're seeing. When you tried sim, how did you configure the simulation?
Depending on what you are doing, you might be able to use options such as fast restart to cut down on the amount of time the sim requires. When you enable fast restart, the software compiles the model only for the first simulation. Subsequent simulations skip the compilation step, which can save time.
To ensure that skipping compilation still produces accurate results, the software prevents you from modifying the model in ways that would require recompilation while fast restart is enabled for the model.
If you are running iterative simulations and you don't need to make such changes, fast restart can probably help speed up the usage of sim here.

Iniciar sesión para comentar.

 Respuesta aceptada

Sara Nadeau
Sara Nadeau el 18 de Abr. de 2023

0 votos

I am not sure that feval is really the relevant part of what is happening here. feval calls another function. In this case, this doc page covers the functionality that it looks like you're using here: https://www.mathworks.com/help/simulink/ug/simulink-model-command.html
To get the values for the To Workspace blocks, I suspect you need to connect the signals to Outport blocks in the top model. It looks like the model name interface returns values for "model outputs" which usually means the values for top-level Outport blocks.

2 comentarios

Yusuf Bayrak
Yusuf Bayrak el 19 de Abr. de 2023
Thank you for the answer. The document you sent is similar to what I am using but it uses directly the model name to call the simulink model.
It may work if it is possible to connect it to the top-level outports but it is not. I have to get the "to workspace" outputs programmaticaly without terminating the model. Is there any way to define the "to workspace" block as an output of the model?
Sara Nadeau
Sara Nadeau el 19 de Abr. de 2023
I believe that the only way to define a model output is to connect the signal to a top-level Outport block, unfortunately. This is just how that model name interface works. If you are not able to connnect the signal that the To Workspace block logs to a top-level Outport block and you need that data, the model name interface might not be the right option for you.
I am not aware of any documentation specific to using the model name interface within feval. There might not be anything special about the combination of these features. Unless you're seeing something unexpected, I believe that the feval function calls the model name function the way it would call any other function, and the model name function works the same as it does when you call it directly.
If you have access to Simulink Control Design, it might be worth exploring the options available in that product, such as findop. For trimming and linearization, Simulink has only basic options, including this model name interface, and Simulink Control Design is what's generally recommended. The trimming and linearization page in Simulink mostly directs people to Simulink Control Design: https://www.mathworks.com/help/simulink/trimming-and-linearization.html
I hope this information is helpful!

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2022b

Preguntada:

el 17 de Abr. de 2023

Comentada:

el 19 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by