Modelica.Media.Examples.SolveOneNonlinearEquation

Demonstrate how to solve one non-linear algebraic equation in one unknown

Information


This package demonstrates how to solve one non-linear algebraic equation in one unknown with function Modelica.Media.Common.OneNonLinearEquation.

Extends from Modelica.Icons.ExamplesPackage (Icon for packages containing runnable examples).

Package Content

NameDescription
Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sine Inverse_sine Solve y = A*sin(w*x) for x, given y
Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sh_T Inverse_sh_T Solve h = h_T(T), s = s_T(T) for T, if h or s is given for ideal gas NASA
Modelica.Media.Examples.SolveOneNonlinearEquation.InverseIncompressible_sh_T InverseIncompressible_sh_T inverse computation for incmpressible media
Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sh_TX Inverse_sh_TX Solve h = h_TX(TX) for T, if h is given for ideal gas NASA


Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sine Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sine

Solve y = A*sin(w*x) for x, given y

Information


This models solves the following non-linear equation

   y = A*sin(w*x); -> determine x for given y

Translate model "Inverse_sine" and simulate for 0 sec. The result is printed to the output window.

Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
Realy_zero0.5Desired value of A*sin(w*x)
Realx_min-1.7Minimum value of x_zero
Realx_max1.7Maximum value of x_zero
RealA1 
Realw1 
f_nonlinear_DatadataInverse_sine_definition.f_no... 

Modelica definition

model Inverse_sine "Solve y = A*sin(w*x) for x, given y"
   import Modelica.Utilities.Streams.print;
   extends Modelica.Icons.Example;

   parameter Real y_zero = 0.5 "Desired value of A*sin(w*x)";
   parameter Real x_min = -1.7 "Minimum value of x_zero";
   parameter Real x_max = 1.7 "Maximum value of x_zero";
   parameter Real A = 1;
   parameter Real w = 1;
   parameter Inverse_sine_definition.f_nonlinear_Data data=
             Inverse_sine_definition.f_nonlinear_Data(A=A, w=w);
   Real x_zero "y_zero = A*sin(w*x_zero)";

encapsulated package Inverse_sine_definition 
    "Define sine as non-linear equation to be solved"
     import Modelica;
   extends Modelica.Media.Common.OneNonLinearEquation;

   redeclare record extends f_nonlinear_Data 
      "Data for nonlinear equation"
      Real A;
      Real w;
   end f_nonlinear_Data;

   redeclare function extends f_nonlinear 
      "Non-linear equation to be solved"
   algorithm 
       y := f_nonlinear_data.A*Modelica.Math.sin(f_nonlinear_data.w*x);
   end f_nonlinear;

   // Dummy definition has to be added for current Dymola (advice from Hans)
   redeclare function extends solve 
      "Solution algorithm of non-linear equation"
   end solve;
end Inverse_sine_definition;

equation 
   x_zero = Inverse_sine_definition.solve(y_zero, x_min, x_max, f_nonlinear_data=data);

   print("x_zero = " + String(x_zero) + ", y_zero = " + String(y_zero) + ", A*sin(w*x_zero) = " +
         String(data.A*Modelica.Math.sin(data.w*x_zero)));
end Inverse_sine;

Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sh_T Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sh_T

Solve h = h_T(T), s = s_T(T) for T, if h or s is given for ideal gas NASA

Information


                               

Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
TemperatureT_min300Vary temperature linearly from T_min (time=0) upto T_max (time=1) [K]
TemperatureT_max500Vary temperature linearly from T_min (time=0) upto T_max (time=1) [K]
Pressurep1.0e5Fixed pressure in model [Pa]

Modelica definition

model Inverse_sh_T 
  "Solve h = h_T(T), s = s_T(T) for T, if h or s is given for ideal gas NASA"
   import SI = Modelica.SIunits;
   extends Modelica.Icons.Example;

   replaceable package Medium = Modelica.Media.Air.DryAirNasa
         constrainedby Modelica.Media.IdealGases.Common.SingleGasNasa 
    "Medium model";

  parameter SI.Temperature T_min = 300 
    "Vary temperature linearly from T_min (time=0) upto T_max (time=1)";
  parameter SI.Temperature T_max = 500 
    "Vary temperature linearly from T_min (time=0) upto T_max (time=1)";
  parameter SI.Pressure p = 1.0e5 "Fixed pressure in model";
  final parameter SI.SpecificEnthalpy h_min = Medium.h_T(Medium.data,T_min) 
    "Specific enthalpy at T_min";
  final parameter SI.SpecificEnthalpy h_max = Medium.h_T(Medium.data,T_max) 
    "Specific enthalpy at T_max";
  final parameter SI.SpecificEntropy s_min = Medium.specificEntropy(Medium.setState_pT(p,T_min)) 
    "Specific entropy at T_min";
  final parameter SI.SpecificEntropy s_max = Medium.specificEntropy(Medium.setState_pT(p,T_max)) 
    "Specific entropy at T_max";
  SI.SpecificEnthalpy h1 "Pre-defined specific enthalpy";
  SI.SpecificEnthalpy h2 "Specific enthalpy computed from T (= h1 required)";
  SI.SpecificEntropy s1 "Pre-defined specific entropy";
  SI.SpecificEntropy s2 "Specific entropy computed from T (= h1 required)";
  SI.Temperature Th "Temperature computed from h1";
  SI.Temperature Ts "Temperature computed from s1";

protected 
  constant SI.Time timeUnit = 1.0;

equation 
   // Define specific enthalpy and specific entropy
   h1 = if time < 0 then h_min else 
        if time > 1 then h_max else 
           h_min + time/timeUnit*(h_max - h_min);
   s1 = if time < 0 then s_min else 
        if time > 1 then s_max else 
           s_min + time/timeUnit*(s_max - s_min);

   // Solve for temperature
   Th = Medium.temperature_phX(p, h1, fill(0.0,0));
   Ts = Medium.temperature_psX(p, s1, fill(0.0,0));

   // Check (h2 must be identical to h1)
   h2 = Medium.specificEnthalpy_pTX(p, Th, fill(0.0,0));
   s2 = Medium.specificEntropy(Medium.setState_pT(p,Ts));
end Inverse_sh_T;

Modelica.Media.Examples.SolveOneNonlinearEquation.InverseIncompressible_sh_T Modelica.Media.Examples.SolveOneNonlinearEquation.InverseIncompressible_sh_T

inverse computation for incmpressible media

Information



                               

Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
TemperatureT_minMedium.T_minVary temperature linearly from T_min (time=0) upto T_max (time=1) [K]
TemperatureT_maxMedium.T_maxVary temperature linearly from T_min (time=0) upto T_max (time=1) [K]
Pressurep1.0e5Fixed pressure in model [Pa]

Modelica definition

model InverseIncompressible_sh_T 
  "inverse computation for incmpressible media"
   import SI = Modelica.SIunits;
   import Cv = Modelica.SIunits.Conversions;
  extends Modelica.Icons.Example;

  replaceable package Medium =
       Modelica.Media.Incompressible.Examples.Glycol47 "Medium model";

  parameter SI.Temperature T_min = Medium.T_min 
    "Vary temperature linearly from T_min (time=0) upto T_max (time=1)";
  parameter SI.Temperature T_max = Medium.T_max 
    "Vary temperature linearly from T_min (time=0) upto T_max (time=1)";
  parameter SI.Pressure p = 1.0e5 "Fixed pressure in model";
  final parameter SI.SpecificEnthalpy h_min = Medium.h_T(Medium.T_min) 
    "Specific enthalpy at T_min";
  final parameter SI.SpecificEnthalpy h_max = Medium.h_T(Medium.T_max) 
    "Specific enthalpy at T_max";
  final parameter SI.SpecificEntropy s_min = Medium.specificEntropy(Medium.setState_pT(p,T_min)) 
    "Specific entropy at T_min";
  final parameter SI.SpecificEntropy s_max = Medium.specificEntropy(Medium.setState_pT(p,T_max)) 
    "Specific entropy at T_max";

  SI.SpecificEnthalpy h1 "Pre-defined specific enthalpy";
  SI.SpecificEnthalpy h2 "Specific enthalpy computed from T (= h1 required)";
  SI.SpecificEntropy s1 "Pre-defined specific entropy";
  SI.SpecificEntropy s2 "Specific entropy computed from T (= h1 required)";
  SI.Temperature Th "Temperature computed from h1";
  SI.Temperature Ts "Temperature computed from s1";

protected 
  constant SI.Time timeUnit = 1.0;

equation 
  // Define specific enthalpy
  h1 = if time < 0 then h_min else 
    if time > 1 then h_max else 
    h_min + time/timeUnit*(h_max - h_min);
  s1 = if time < 0 then s_min else 
    if time > 1 then s_max else 
    s_min + time/timeUnit*(s_max - s_min);

  // Solve for temperature
  Th = Medium.temperature_phX(p, h1, fill(0.0,0));
  Ts = Medium.temperature_psX(p, s1, fill(0.0,0));

  // Check (h2 must be identical to h1)
  h2 = Medium.specificEnthalpy_pTX(p, Th, fill(0.0,0));
  s2 = Medium.specificEntropy(Medium.setState_pT(p, Ts));
end InverseIncompressible_sh_T;

Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sh_TX Modelica.Media.Examples.SolveOneNonlinearEquation.Inverse_sh_TX

Solve h = h_TX(TX) for T, if h is given for ideal gas NASA

Information



Extends from Modelica.Icons.Example (Icon for runnable examples).

Parameters

TypeNameDefaultDescription
TemperatureT_min300Vary temperature linearly from T_min (time=0) upto T_max (time=1) [K]
TemperatureT_max500Vary temperature linearly from T_min (time=0) upto T_max (time=1) [K]
Pressurep1.0e5Fixed pressure in model [Pa]

Modelica definition

model Inverse_sh_TX 
  "Solve h = h_TX(TX) for T, if h is given for ideal gas NASA"
   import SI = Modelica.SIunits;
   extends Modelica.Icons.Example;

   replaceable package Medium =
       Modelica.Media.IdealGases.MixtureGases.FlueGasLambdaOnePlus
         constrainedby Modelica.Media.IdealGases.Common.MixtureGasNasa 
    "Medium model";

  parameter SI.Temperature T_min = 300 
    "Vary temperature linearly from T_min (time=0) upto T_max (time=1)";
  parameter SI.Temperature T_max = 500 
    "Vary temperature linearly from T_min (time=0) upto T_max (time=1)";
  parameter SI.Pressure p = 1.0e5 "Fixed pressure in model";
  SI.SpecificEnthalpy h_min = Medium.h_TX(T_min,X) "Specific enthalpy at T_min";
  SI.SpecificEnthalpy h_max = Medium.h_TX(T_max,X) "Specific enthalpy at T_max";
  SI.SpecificEntropy s_min = Medium.specificEntropy(Medium.setState_pTX(p,T_min,Medium.reference_X)) 
    "Specific entropy at T_min";
  SI.SpecificEntropy s_max = Medium.specificEntropy(Medium.setState_pTX(p,T_max,Medium.reference_X)) 
    "Specific entropy at T_max";
  SI.SpecificEnthalpy h1 "Pre-defined specific enthalpy";
  SI.SpecificEnthalpy h2 "Specific enthalpy computed from T (= h1 required)";
  SI.SpecificEntropy s1 "Pre-defined specific entropy";
  SI.SpecificEntropy s2 "Specific entropy computed from T (= h1 required)";
  SI.Temperature Th "Temperature computed from h1";
  SI.Temperature Ts "Temperature computed from s1";
  SI.MassFraction[4] X "mass fraction vector";

protected 
  constant SI.Time timeUnit = 1.0;

equation 
  X = Medium.reference_X;
   // Define specific enthalpy
   h1 = if time < 0 then h_min else 
        if time > 1 then h_max else 
           h_min + time/timeUnit*(h_max - h_min);
   s1 = if time < 0 then s_min else 
        if time > 1 then s_max else 
           s_min + time/timeUnit*(s_max - s_min);

   // Solve for temperature
   Th = Medium.temperature_phX(p, h1, X);
   Ts = Medium.temperature_psX(p, s1, X);

   // Check (h2 must be identical to h1)
   h2 = Medium.specificEnthalpy_pTX(p, Th, X);
   s2 = Medium.specificEntropy(Medium.setState_pTX(p,Ts,X));
end Inverse_sh_TX;

Automatically generated Fri Nov 12 16:31:28 2010.