Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)

• Index
• »
• »
• » Block with textual math?

## Block with textual math?

### Block with textual math?

In block diagram simulators, is there a block in which one can write textual math, like the Formula Node (with C-code) in LabVIEW and the Matlab Function (with Matlab code) in Simulink? Such blocks could be used as an alternative to using elementary math blocks, thereby simplifying the block diagram considerably.

finnhaugen@hotmail.com  2 Posts

### Re: Block with textual math?

Hi, i'm not familiar with Formula node but i assume you want to calculate some outputs based on some inputs according to a more complex equation. you can always create your own blocks like this simple example

#### Code:

```model quadraticFunction
/* Calculate quadratic function y = ax^2 + bx + c */
Modelica.Blocks.Interfaces.RealInput x annotation(
Placement(visible = true, transformation(origin = {-106, 0}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-100, 0}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealOutput y annotation(
Placement(visible = true, transformation(origin = {104, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {98, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
parameter Real a = 1, b=2, c=3;
equation

y = a*x^2 + b*x  + c;

annotation(
Icon(graphics = {Rectangle(fillColor = {170, 170, 170}, fillPattern = FillPattern.Solid, extent = {{-100, 40}, {100, -40}}, radius = 15), Text( extent = {{-66, 15}, {66, -15}}, textString = "Quadratic function")}));
```

you can drag and drop input and output port from

#### Code:

`Modelica.Blocks.Interfaces `

(or manually type the annotation for position and visibility) so the ports will be visible on top level. In the text view you can define your equation and some other parameters.

As fas as i know there are no "blank" block that enables you to create I/O port and define an equation on the fly or from top level, but maybe i'm wrong

Hope it helps

Arinomo23  120 Posts

### Re: Block with textual math?

An alternative to the suggestion by Arinomo23 is the following:

#### Code:

```block MathExpression
extends Modelica.Blocks.Interfaces.SISO;
input Real expression=u "User defined expression" annotation(Dialog);
equation
y=expression;
end MathExpression;
```

To reduce code, it inherits the "SISO" block which already has one input "u" and one output "y".

By default, the expression sets "y=u" to keep the model balanced during code check (equations=unknowns). The "annotation(Dialog)" part enables "graphical" access to the expression input when the block is dragged into the diagram layer. Then, any expression can be entered from the GUI. You can also access public variables and parameters from other blocks in the expression, for example "2*mathExpression.u^2 + someOtherBlock.y + 23".

Note that when you edit the expression from GUI you should provide the fully qualified path to the variables, hence "mathExpression.u" rather than just "u".

If you don't need to graphically connect an output from another block to the expression, you can always use the "Modelica.Blocks.Sources.RealExpression" block from the standard library.

Edited by: justnielsen - Feb-24-22 07:17:49

### Re: Block with textual math?

#### justnielsen wrote:

An alternative to the suggestion by Arinomo23 is the following:

#### Code:

```block MathExpression
extends Modelica.Blocks.Interfaces.SISO;
input Real expression=u "User defined expression" annotation(Dialog);
equation
y=expression;
end MathExpression;
```

+1 i also preferred this solution.

I forgot that SISO block exist and i don't now about the

#### Code:

` annotation(Dialog) `

could be used that way.

Thanks for the info.

Edited by: Arinomo23 - Feb-24-22 07:34:01
Arinomo23  120 Posts

### Re: Block with textual math?

Hi Just,

Thanks. Can you provide a very simple yet complete example (as a mo-file) which I can open and modify? Say the formula to be represented is y = a*u + b where a and b are parameters with values 1 and 2 respectively, and u comes from a step input block.

(By now, I am not mastering Modelica programming. My present skills is to create block diagrams representing mathematical block diagrams of systems given as given diff eqs and PID control for these systems.)

Regards,
Finn

finnhaugen@hotmail.com  2 Posts

### Re: Block with textual math?

@Finn Haugen.

Please see attached file. It is a package containing the `MathExpression` model mentioned earlier and four simple
FunctionBlock.mo
examples of usage to show the versatility of the Modelica language.

Attachments:
• Index
• »
• »
• » Block with textual math?
There are 0 guests and 0 other users also viewing this topic
You are here: