Reputation: 61
Let's say I have vectors
Real x[5]={1,2,3,4,5};
and
Real y[5]={0,0,0,0,0};
I want to assign values to elements of y
given some condition on x
, say for example, for every element in x
greater than or equal to 3
, corresponding y
should be set to 1
, for every x<3: y=sin(x)
In matlab, I would have written as follows:
y(x>=3)=1;
y(x<3)=sin(x(x<3));
so
x=[1 2 3 4 5]
Results in
y=[sin(1) sin(2) 1 1 1]
Can something similar be done in Modelica, and if so, how?
Upvotes: 2
Views: 324
Reputation: 6655
Unfortunately there is no array filtering in Modelica, so x>=3
does not work and there is nothing available that is comparable to that.
But still there are some ways get the resulting vectors, depending if you want an equation or an algorithm. In a model using equations you could e.g. use an array constructor with one iterator:
model DemoModel
Real x[5]={1,2,3,4,5};
Real y[:]= {if i >= 3 then 1 else sin(i) for i in x};
end DemoModel;
With this method all elements are assigned at once. In models its not possible to update only the values which match a criteria (or at least I don't know how to do so), since:
With a filter the number of equations would vary dynamically, depending on the values of x
and the condition.
Inside functions you have more possibilities (you can have flexible array sizes), but when you use the output of the function in a model you are facing the same limitations again.
Upvotes: 4