How to run a simulink model from a matlab file with different inputs each step?

4 次查看(过去 30 天)
I am using someone's simulation to represent a plant for control system development. The algorithms I want to run are quite complex to implement within simulink so I would prefer to do everything from a matlab file.
Is there a way to run this in some type of for loop where at each step I compute the control input in the matlab function and then call the model as a function just to perform a single step update?
I have seen the sim function, but don't see a straightforward way to do this for a step at a time.
  5 个评论
Sam Chak
Sam Chak 2023-9-28
Could you please provide a brief description of the type of controller you've designed? It appears to be a state-feedback controller.
inputs_k = controller(system_state)
Sebastian
Sebastian 2023-9-28
Thanks for the question - the controller is rather more complicated than pure state feedback. Indeed a more accurate representation would be something like
controller.compute_inputs(system_state)
Where the controller itself has some internal variables. It's also important to note that the system_state in this case is actually a scalar and the size of the controller's internal state vector is higher.
Let's think of it as a more involved MPC together with some state and parameter estimation. Othewise I would've implemented in Simulink quite straightforwardly. I am also try to test a variety of different models for the system, meaning my state variable (as far as the controller sees it) would have to be determined based on some config file.
I also considered simply using an interpreted MATLAB function but this is quite a painful feature to use since it only allows for function with a single input and single output.

请先登录,再进行评论。

回答(1 个)

Sam Chak
Sam Chak 2023-9-28
Thank you for your clarifications. Now that we understand your controller has internal state dynamics, it's important to note that the Interpreted MATLAB function does not support modeling Model State Dynamics. If you are working with nonlinear or general dynamic controllers, you might want to consider modeling the controller using the code-level S-function block. While it may have a steeper learning curve for some, as it involves implementing a set of callback methods, it offers the flexibility you need to implement your dynamic controller.
You can find more information and resources on S-functions in the following links:
  1 个评论
Sebastian
Sebastian 2023-9-29
Thanks for that very detailed answer. The S-function block aslo seems pretty involved. I'm really looking for a more straighforward workaround.
Basically I'm trying to avoid dealing with all the language specific pains of MATLAB/Simulink while I develop and only have to worry about this once I have something working (tbh I wouldn't be using any of these packages if not for the fact the plant simulator I have is already developed). Are we basically saying that I can't do what I want? (i.e. create some kind of matlab object that let's me step through the simulation programatically?)

请先登录,再进行评论。

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by