1
votes

I have three inputs into a Matlab Function block in my Simulink Model. The inputs consist of a 1D array thetaArray and two variables currentIndex and trackingError. The Matlab Function block will output two variables called newIndex and theta.

What would be an ideal way to use the newIndex variable as my new input into the Matlab Function block?

Here is the image of my Simulink Model

And here is the code for the Matlab Function Block in the model:

function [newIndex,theta] = arraySelector(thetaArray,currentIndex,trackingError)

if currentIndex < length(thetaArray) && trackingError <= 0.00002
   newIndex = currentIndex + 1;
else
   newIndex = currentIndex;
end

theta = thetaArray(newIndex);
end

Thank you in advance.

1
Send it back through a unit delay block.Navan

1 Answers

2
votes

If theta will be the output and the input of the model you will have a model which contains an algebraic loop.

It is unclear from your question if you

  1. need to solve the algebraic loop within a single time step OR
  2. you want your model advance in time with each iteration of the loop.

For 2nd case:

This is the easier case. (more likely when studying your model). Here you will break the loop e.g. by introducing a delay block. The initial condition of the delay block will be what you have now defined in the example (block theta_1).

For 1st case:

Follow the description in How to Handle Algebraic Loops in a Model. I would recommend that you first study with an example where you use a scalar instead of an array. You need to define the initial condition for the index, e.g. by introducing an IC-Block (like you did for the trackingerror) into the loop. If simulink is still unable to solve the algebraic loop consider an alternative implementation inside your Matlab Function Block which changes the status of your input . from direct feedthrough to non-direct feedthrough