What type of declaration is this?
Real x = time^2;
I can put it in a model before any equation or algorithm section.
The issue for me is that it is not a static parameter, but rather it has a formula attached to it that works non-statically - the value is set every time step.
What kind of declaration is it? Short model or short function definition? New instances of a class? A 'type' class?
Any help (especially with a reference to either Fritzon's or Tiller's book) will help me sleep at night.
That is a binding equation in a variable declaration and it will become a normal equation during compilation of the model. See https://modelica.org/documents/ModelicaSpec34.pdf, chapter 8.
If you want to modify the variable the Dialog annotation might be convenient, e.g., Real x = time^2 annotation(Dialog);
Related
In Matlab, it is easy to generate "help" for a function, as follows.
function out = foo()
% helpful information about foo
end
When we execute help foo, we get "helpful information about foo".
However, suppose we would like to define help for a variable, probably as a definition. How could we do such a thing? It would be nice if we could do something like
x = 3; % m ... position
help x
and get "m ... position". However, I don't believe such functionality exists.
The only reasonable way I see around this is to define every variable as a struct with keys value and description.
x.value = 3;
x.description = 'm/s ... position';
This requires we define every variable as a struct, which is kind of annoying and, I worry (should I?), unperformant (it's simulation code and these variables are accessed repeatedly).
Is there another solution I'm not considering? Should I be worried about making every variable a struct?
Your code should be self-documenting. Instead of variable name x, use position.
Furthermore, all variables should be local, so you can easily look for its definition (with comment) within the function you are editing.
Variables declared further away (with larger scope within the function) should have longer, more self-explanatory names than variables with a smaller scope (e.g. use within a short loop.
There are only two three cases where variables are declared outside the function’s scope:
Class properties. You can actually document these.
In a script, you have access to variables that already existed before the script started. A good reason not to use scripts or depend on the base namespace in larger projects.
Global variables. You should never use global variables for many reasons. Just don’t.
I am writing a signal processing program using matlab. I know there are two types of float-pointing variables, single and double. Considering the memory usage, I want my code to work with only single type variable when the system's memory is not large, while it can also be adapted to work with double type variables when necessary, without significant modification (simple and light modification before running is OK, i.e., I don't need runtime-check technique). I know this can be done by macro in C and by template in C++. I don't find practical techniques which can do this in matlab. Do you have any experience with this?
I have a simple idea that I define a global string containing "single" or "double", then I pass this string to any memory allocation method called in my code to indicate what type I need. I think this can work, I just want to know which technique you guys use and is widely accepted.
I cannot see how a template would help here. The type of c++ templates are still determined in compile time (std::vector vec ...). Also note that Matlab defines all variables as double by default unless something else is stated. You basically want runtime checks for your code. I can think of one solution as using a function with a persistent variable. The variable is set once per run. When you generate variables you would then have to generate all variables you want to have as float through this function. This will slow down assignment though, since you have to call a function to assign variables.
This example is somehow an implementation of the singleton pattern (but not exactly). The persistent variable type is set at the first use and cannot change later in the program (assuming that you do not do anything stupid as clearing the variable explicitly). I would recommend to go for hardcoding single in case performance is an issue, instead of having runtime checks or assignment functions or classes or what you can come up with.
function c = assignFloat(a,b)
persistent type;
if (isempty(type) & nargin==2)
type = b;
elseif (isempty(type))
type = 'single';
% elseif(nargin==2), error('Do not set twice!') % Optional code, imo unnecessary.
end
if (strcmp(type,'single'))
c = single(a);
return;
end
c = double(a);
end
Ok, so I have something like this:
model MolarAmount
import SI = Modelica.SIunits;
SI.AmountOfSubstance nu "moles of stuff";
parameter Real lambda = 42 "some variable on which nu depends";
equation
nu = 1 - lambda;
end MolarAmount;
This runs just fine, but it complains about incompatible units, as expected. How do I get it to ignore this?
That is odd. I always thought that Dymola threated literals (1) and variables without units (lambda) as wildcards in the unit checking. You might try setting the units attribute on lambda to be "1" (I thought that was the default). You might also take a look at the diode model in the standard library. It uses a parametric formulation that has to address this kind of unit checking problem as well.
Sorry for not validating any of these suggestions. I don't really have a copy of Dymola handy to test with.
HTH
What version of Dymola are you running? Dymola 2015 (2014-04) accepts this model.
I would guess setting lambda(unit="mol") would solve your problems. It also leads to less magic in the unit checking.
I had a function in a file harmonic.m in my matlab path with prototype:
function D = harmonic(A,B,C)
where, importantly, A is expected to be a matrix of type double.
In version r2014a, apparently MATLAB has created a new builtin class method double.harmonic. Thus when I call my function I get an error inside the wrong harmonic. It doesn't help that my harmonic is closer in the path list (which harmonic reveals my path) because my first input is A and harmonic(A,B,C) seems to be equivalent to A.harmonic(B,C).
Is there any way to call my function directly? To ignore this double.harmonic function? I know I can create a function handle from the path, but that's nasty. I'm looking for a more elegant solution or workaround. The most obvious being change my function's name, but then I'll feel bullied : - (.
Put your version of harmonic into a folder #double, and make sure that your folder #double is above \toolbox\symbolic\symbolic\#double on the path (this new double.harmonic is from Symbolic Toolbox).
That will force your function to become a method of double i.e. it will be double.harmonic, rather than a generic function harmonic. When deciding which thing to dispatch to, MATLAB will consider methods first, then generic functions later. Since your double.harmonic and the other one are both methods, and yours is ahead on the path, yours will win. BAM - eat that, MATLAB!
How to simplify a given boolean expression with many variables (>10) so that the number of occurrences of each variable is minimized?
In my scenario, the value of a variable has to be considered ephemeral, that is, has to recomputed for each access (while still being static of course). I therefor need to minimize the number of times a variable has to be evaluated before trying to solve the function.
Consider the function
f(A,B,C,D,E,F) = (ABC)+(ABCD)+(ABEF)
Recursively using the distributive and absorption law one comes up with
f'(A,B,C,E,F) = AB(C+(EF))
I'm now wondering if there is an algorithm or method to solve this task in minimal runtime.
Using only Quine-McCluskey in the example above gives
f'(A,B,C,E,F) = (ABEF) + (ABC)
which is not optimal for my case. Is it save to assume that simplifying with QM first and then use algebra like above to reduce further is optimal?
I usually use Wolfram Alpha for this sort of thing.
Try Logic Friday 1
It features multi-level design of boolean circuits.
For your example, input and output look as follows:
You can use an online boolean expression calculator like https://www.dcode.fr/boolean-expressions-calculator
You can refer to Any good boolean expression simplifiers out there? it will definitely help.