Choosing MATLAB Vector element based on Simulink model argument - matlab

I'm trying to parameterize one of my Simulink models, so that I will have a gain in the model whose value is equal to an element of a MATLAB workspace vector indexed by the model parameter. That is, I want to define a model argument WheelIndex and have a gain inside the model with a value AxelLoads(WheelIndex).
When I do it exactly as I've described above, I get "vector indices must be real and positive integers" error. When I change the model argument to AxelLoad(To be used directly in the gain component) and assign its value to be AxelLoads(1)(for the first wheel) I get:
Error in 'Overview/Wheel1'. Parameter '18000.0, 15000.0, 17000.0,
21000.0' setting: "18000.0, 15000.0, 17000.0, 21000.0" cannot be evaluated.
I've also tried importing the vector as a constant block into the model, and use a selector block parameterized by the WheelIndex argument to direct the right element to a multiplication block (thereby making an ugly gain block), but then Simulink complains that I'm trying to use a model argument to define an "un-tunable value".
I just want to somehow define the parameters in the MATLAB workspace to be used in each model instance, so that I can, say, calculate the total weight by adding the loads on each wheel. Simulink seems to block all workarounds I've been trying.
Thanks

Could you use a lookup table to obtain AxelLoads vs. WheelIndex?

The easiest way is if I just came over? :P
Perhaps this explaination of tunable parameters helps a little?

Related

Division by zero depending on parameter

I am using the FixedRotation component and get a division by zero error. This happens in a translated expression of the form
var = nominator/fixedRotation.R_rel_inv.T[1,3]
because T[1,3] is 0 for the chosen parameters:
n={0,1,0}
angle=180 deg.
It seems that Openmodelica keeps the symbolic variable and tries to be generic but in this case this leads to division by zero because it chooses to put T[1,3] in the denominator.
What are the modifications in order to tell the compiler that the evaluated values T[1,3] for the compilation shall be considered as if the values were hard coded? R_rel is internally in fixedRotation not defined with Evaluate=true...
Should I use custom version of this block? (when I copy paste the source code to a new model and set the parameters R_rel and R_rel_inv to Evalute=true then the simulation works without division by zero)...
BUT is there a modifier to tell from outside that a parameter shall be Evaluate=true without the need to make a new model?
Any other way to prevent division by zero?
Try propagating the parameter at a higher level and setting annotation(Evaluate=true) on this.
For example:
model A
parameter Real a=1;
end A;
model B
parameter Real aPropagated = 2 annotation(Evaluate=true);
A Ainstance(a=aPropagated);
end B;
I don't understand how the Evaluate annotation should help here. The denominator is obviously zero and this is what shall be in fact treated.
To solve division by zero, there are various possibilities (e.g. to set a particular value for that case or to define a small offset to denominator, you can find examples in the Modelica Standard Library). You can also consider the physical meaning of the equation and handle this accordingly.
Since the denominator depends on a parameter, you can also set an assert() to warn the user there is wrong parameter value.
Btw. R_rel_inv is protected and shall, thus, not be used. Use R_rel instead. Also, to deal with rotation matrices, usage of functions from Modelica.Mechanics.MultiBody.Frames is a preferrable way.
And: to use custom version or own implementation depends on your preferences. Custom version is maintained by the comunity, own version is in your hands.

Access/Index Array In Simulink

I have a 2D matrix/array in my model, as shown in image. I need to be able to index/access it randomly and pass it as a signal. How do I do this?
I can't use From File block, because the storage is forced to be double and too large for my embedded design.
It doesn't appear I can use From Workspace block...because this array is defined in my model as SoundArray.
This seem like it should be SO SIMPLE, but I just can’t figure it out. The only way I can think of doing it is in custom C code…which I don’t want to do.
Thanks
Array Definition and Model At Bottom
A matlab Function block (formerly EML-block) can pick up model workspace data if it is in "Parameter" scope and you define a Parameter input in the Function block. You could then use other inputs for controlling the random access, then return the desired position as a signal output from the Matlab function block.
function y = fcn(i,j,soundArray)
y = soundArray(i,j);
(Where soundArray is defined as a Parameter, and i and j are Inputs)
Edit:
Or define a Data Store Memory (add definition block). Then put a Data Store Read block for that memory which is routed to a selector block with 2 dimensions and "starting index (port)" for both those dimensions.
I believe you can use Model Workspace data to initialize the Data Store Memory, but I don't think that Model Workspace data is "live" during simulation.

How can I use data from the workspace in an equation?

I have two tables of data in the workspace on Matlab, and I want to use an equation that takes the first value from each table, does the calculation, then the 2nd value from each and so on... and stores these output numbers.
I could probably do this if I was just defining the data arrays myself in the code, but I'm stuck when trying to do it from the workspace...
Is it inside a function? You could pass the workspace variables as arguments to the function, or use evalin('base', 'variableName') to get the values (this second method is HIGHLY discouraged however).

How to define a variable without assign any value to it

vo is the upper limit of an integration expression and this is the variable that I want to predefine as a variable without value since it is the variable I am trying to solve for.
Everything else is defined.
%//a object's velocity is changing as a function of sine, the full cycle is 0=>speed=>0
vt=-((speed/2)*cos((2*pi/t0)*t)-(speed/2)))
%//integrate vt function so I can get distance which has given
distance=int(vt,t,0,t0)
%//the last step is I need to find how long does it takes the object to finish a full cycle of movement
time=solve(eqn,t0)
Try using the syms command in MATLAB. The syms is part of the symbolic math toolbox and you can predefine "variables" without explicitly solving for them. They form a part of the mathematics expression that you want.
Once you formulate the equation you want, you can use solve (as you have already used in your post) to solve the variable for you.

Getting the solver type and step size (for fixed step solvers)

we are trying to integrate a simulation model into Simulink as a block. We have a custom continuous block which loads an m file that contains the functions Derivatives, Outputs etc.
My question is: is there a way to find out which solver is used currently and with which parameters? Our model won't be able to support variable time solvers and I would like to give a warning. Similarly, the model requires the fixed step time for initialization.
Thanks in advance.
You can get the current solver name using
get_param('modelName', 'SolverName');
Some of the other common solver parameters are
AbsTol
FixedStep
InitialStep
ZcThreshold
ExtrapolationOrder
MaxStep
MinStep
RelTol
SolverMode
You can find other parameters you may wish to query by opening the .mdl file in your favorite text editor and digging through it.
If I'm understanding your use case correctly, you are trying to determine the type of solver (and other solver params) for the top-level simulink system containing your block.
I think the following should give you what you want:
get_param(bdroot, 'SolverType'); % //Returns 'Variable-step' or 'Fixed-step'
get_param(bdroot, 'FixedStep'); % //Returns the fixed step size
Notice that for purposes of generality/reusability, this uses bdroot to identify the top-level system (rather than explicitly specifying the name of this system).
If you want to find out more about other model parameters that you can get/set, I would check out this doc.
Additionally, I'm interested to know why it is that your model doesn't support a variable-step solver?