# Can a variable be used as the parameter k in a Gain block?

I am trying to model a system where gain is variable. I have created a simple two block model to first investigate the possibility of using a variable as the gain parameter. If this works, I would like to add blocks to compute the gain.

The first block is a sinusoidal source with an amplitude of 10 and offset of 5 and a frequency of 1.

The second block is a gain block. The input is connected to the sinusoidal source. The gain parameter is set to a variable named gainValue.

The variable named gainValue with an initial value of 1 and the Fixed property set to False.

The code is below:

  model ExploratoryModelForVariableGain "Sandbox model to use a variable as the gain parameter"

Modelica.Blocks.Sources.Sine V1(amplitude = 10, freqHz = 1, offset = 5) "Simple AC Voltage with DC Offset" annotation(Placement(visible = true, transformation(origin = {-97.915, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));

Modelica.Blocks.Math.Gain Amp1(k = gainValue) annotation(Placement(visible = true, transformation(origin = {-50, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));

Real gainValue(start = 1, fixed = false) = 1 annotation(Dialog(group = "Variables", showStartAttribute = true));

equation
connect(V1.y, Amp1.u) annotation(Line(visible = true, origin = {-74.457, 0}, points = {{-12.457, -0}, {12.457, 0}}, color = {1, 37, 163}));
annotation(Diagram(coordinateSystem(extent = {{-148.5, -105}, {148.5, 105}}, preserveAspectRatio = true, initialScale = 0.1, grid = {5, 5})));

end ExploratoryModelForVariableGain;


The Error: "Component Amp1.k of variability PARAM has a binding gainValue of higher variability VAR" appears during Validation.

Note: Changing the variable to a parameter removes this error, but leaves warnings that initial values are not set (even though an Initial Value is specified for the parameter on the Parameters tab).

I am also seeing Internal Errors thrown whenever component modifications are attempted.

• What language do you use? Here is the forum Mathematica, the Wolfram language. Sep 28, 2018 at 4:21
• @AlexTrounev The language is Modelica, the language used in Wolfram SystemModeler. Questions about it were voted to be on-topic for this forum: mathematica.meta.stackexchange.com/questions/1336/… Sep 28, 2018 at 10:57

I believe you are looking for the Modelica.Blocks.Math.Product component, rather than the Gain. The Gain, as you already discovered, only allows a constant gain (not time-varying).

model ExploratoryModelForVariableGain "Sandbox model to use a variable as the gain parameter"
Modelica.Blocks.Sources.Sine V1(amplitude = 10, freqHz = 1, offset = 5) "Simple AC Voltage with DC Offset" annotation(Placement(visible = true, transformation(origin = {-95, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Real gainValue(start = 1, fixed = false) = 1 annotation(Dialog(group = "Variables", showStartAttribute = true));
Modelica.Blocks.Sources.Sine V2(amplitude = 10, freqHz = 2, offset = 5) "Simple AC Voltage with DC Offset" annotation(Placement(visible = true, transformation(origin = {-95, -40}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.RealOutput y annotation(Placement(visible = true, transformation(origin = {-16.565, -20}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-11.155, -19.048}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Blocks.Math.Product product1 annotation(Placement(visible = true, transformation(origin = {-50, -20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
connect(V1.y, product1.u1) annotation(Line(visible = true, origin = {-76, -7}, points = {{-8, 7}, {-3, 7}, {-3, -7}, {14, -7}}, color = {1, 37, 163}));
connect(V2.y, product1.u2) annotation(Line(visible = true, origin = {-76, -33}, points = {{-8, -7}, {-3, -7}, {-3, 7}, {14, 7}}, color = {1, 37, 163}));
connect(product1.y, y) annotation(Line(visible = true, origin = {-27.783, -20}, points = {{-11.217, 0}, {11.217, 0}}, color = {1, 37, 163}));
annotation(Diagram(coordinateSystem(extent = {{-148.5, -105}, {148.5, 105}}, preserveAspectRatio = true, initialScale = 0.1, grid = {5, 5})));
end ExploratoryModelForVariableGain;