How to run a MATLAB Script once for each Simulink Loop.

8 views (last 30 days)
I'm trying to build a Simulink model that tracks the instantaneous position of a 6DOF dynamic mechanical system, based on a series of inputs which are 6x6 matrices (which describe behaviour), 1x6 vectors (intial) and one time-dependent impulse (a simulated impact), and with an ouput of a 1x6 matrix describing the position of one element in all the degrees of freedom, which should be plotted over time as the simulation runs.
Currently I calculate the forces acting on the system using an equation, which requires an Add Block, and a subsequent Matrix Multiply block to remove mass and inertia and get the acceleration.
My problem is that some of these matrices change with position and velocity (specifically related to inertia and coriolis forces), but I use a toolbox to generate them, so a direct translation into Simulink is not possible.
As I use a MATLAB script to set up these matrices, I thought I could just change the instantaenous position and velocity using the To Workspace Block from the simulaiton outputs and run the script again before each simulation.
I currently can't find a way to get this to work as all the Blocks I've seen so far require problematic conditions. Interpreted MATLAB Fcn runs specific functions with defined inputs and outputs, but I'd like to run the whole script based on the output of the previous loop. Stop Fcn will only run the code when the simulation has ended, but I want to follow the position over many loops until it converges.
Is there any way to do this? Run a simulation that switches between Simulink flow and MATLAB script and back with each loop?

Accepted Answer

rubindan
rubindan on 29 Jun 2020
If you do not need to generate code from this Simlink model, you can use a Matlab Function block,
In you case inputs to the block will be the instantaenous position and velocity, and outputds will be the computed matrices.
  2 Comments
Kieran McMullen
Kieran McMullen on 29 Jun 2020
I've tried this, and seem to be getting a bit further along as the process being performed is still explicit, however I'm now having a problem with the dimensions of my signals. It seems obvious to me that the outputs should be 6x6 matrices, but Simulink seems confused about what outputs to give.
Is there some way I can set Simulink to expect the matrix dimensions I know it should be processing?
rubindan
rubindan on 1 Jul 2020
You can either specify the dimensions of inputs and outputs explicitly in the Ports and Data Manager, or preallocate them using e.g. zeros(6,6) before doing the computations.

Sign in to comment.

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!