Message Boards Message Boards

0
|
5060 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:
GROUPS:

minimum attribute versus assert function

Posted 10 years ago

I wanted to ask if I want to make sure that a certain parameter is accepted only for a specific range, i.e. length and distances must be always positive. What kind of function should I use ? What I understand so far that assert function is used to generate an error message when the user enters a wrong value. So what about the minimum attribute ? What is the difference between both of them ?

POSTED BY: Mohamed Mahmoud
3 Replies
Posted 10 years ago

You can use the min and max attributes for parameters, like so:

parameter Modelica.SIunits.Length l1(min=0.4, max=1.5)=1 "A typical length";

If you try to enter a value for l1 that's outside of the range, a warning will appear in Model Center.

POSTED BY: Johan Rhodin
Posted 10 years ago

Thanks Sean for your reply. I am asking about the Modelica language. The code you are showing is Mathematica code, and I don't think it can be used inside SystemModeler.

POSTED BY: Mohamed Mahmoud

I'm not sure I understand what you mean by the "minimum attribute". Can you give an example of what you mean?

I normally do this by defining a pattern to be matched in the definition of the function

f[x_/;x>0] := x+1  (*Only runs if x>0*)
f[___] := Throw["Invalid input to f"]  (*Otherwise throw an error*)
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract