From 92670acc9ff4ad27f5ddb141b0e279049f6feeaa Mon Sep 17 00:00:00 2001 From: AHaumer Date: Fri, 27 Feb 2026 19:10:42 +0100 Subject: [PATCH 1/2] der(erf) and der(erfc) --- Modelica/Math/Special.mo | 26 ++++++++++++++++--- ModelicaTest/Math.mo | 23 ++++++++++++++++ .../Math/TestErf/comparisonSignals.txt | 7 +++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt diff --git a/Modelica/Math/Special.mo b/Modelica/Math/Special.mo index 7988a2ef71..d2840efeb7 100644 --- a/Modelica/Math/Special.mo +++ b/Modelica/Math/Special.mo @@ -55,7 +55,8 @@ package Special "Library of special mathematical functions" y :=-y; end if; - annotation (Documentation(info=" + annotation (derivative=erfDer, + Documentation(info="

Syntax

 Special.erf(u);
@@ -137,7 +138,8 @@ erf(0.5)  // = 0.520499877813047
     else
        y := 0;
     end if;
-    annotation (Documentation(info="
+    annotation (derivative=erfcDer,
+      Documentation(info="
 

Syntax

 Special.erfc(u);
@@ -214,7 +216,8 @@ erfc(0.5)  // = 0.4795001221869534
        y :=Internal.erfInvUtil(u, 1 - u);
     end if;
 
-    annotation (smoothOrder=1, Documentation(info="
+    annotation (smoothOrder=1,
+      Documentation(info="
 

Syntax

 Special.erfInv(u);
@@ -304,7 +307,8 @@ erfInv(0.9999999999) // = 4.572824958544925
        y :=Internal.erfInvUtil(1-u, u);
     end if;
 
-    annotation (Documentation(info="
+    annotation (
+      Documentation(info="
 

Syntax

 Special.erfInv(u);
@@ -372,6 +376,20 @@ erfInv(1.999999)   // = -3.4589107372909473
 "));
   end erfcInv;
 
+  function erfDer "Derivative of erf-function"
+    extends Modelica.Icons.Function;
+    input Real u "Input argument";
+    input Real der_u "Derivative of input";
+    output Real y= 2/sqrt(Modelica.Constants.pi)*exp(-u^2)*der_u "Derivative of erf(u)";
+  end erfDer;
+
+  function erfcDer "Derivative of erfc-function"
+    extends Modelica.Icons.Function;
+    input Real u "Input argument";
+    input Real der_u "Derivative of input";
+    output Real y=-2/sqrt(Modelica.Constants.pi)*exp(-u^2)*der_u "Derivative of erfc(u)";
+  end erfcDer;
+
   function sinc "Unnormalized sinc function: sinc(u) = sin(u)/u"
     extends Modelica.Icons.Function;
     input Real u "Input argument";
diff --git a/ModelicaTest/Math.mo b/ModelicaTest/Math.mo
index 26d07b7cc9..7a08ac7da6 100644
--- a/ModelicaTest/Math.mo
+++ b/ModelicaTest/Math.mo
@@ -838,6 +838,29 @@ extends Modelica.Icons.ExamplesPackage;
     annotation (experiment(StopTime=5));
   end TestInterpolateTimeVarying;
 
+  model TestErf "Test error function"
+    extends Modelica.Icons.Example;
+    import Modelica.Math.Special;
+    Real x=time;
+    Real y1=Special.erf(x);
+    Real dy1=der(y1) "der(erf)";
+    Real y1i "integral(dy1) == erf ?";
+    Real y2=Special.erfc(x) "erfc == 1 - erf ?";
+    Real dy2=der(y2) "der(erfc)";
+    Real y2i "integral(dy2) == erfc ?";
+  initial equation
+    y1i=Special.erf(x);
+    y2i=Special.erfc(x);
+  equation
+    der(y1i)=dy1;
+    der(y2i)=dy2;
+    annotation (experiment(
+        StartTime=-10,
+        StopTime=10,
+        Interval=0.0001,
+        Tolerance=1e-06));
+  end TestErf;
+
   package Random
     function randomNumbers
       "Demonstrate the generation of uniform random numbers in the range 0..1"
diff --git a/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt b/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt
new file mode 100644
index 0000000000..940df8db01
--- /dev/null
+++ b/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt
@@ -0,0 +1,7 @@
+time
+y1
+dy1
+y1i
+y2
+dy2
+y2i

From aed7698657909836d484103144f52d849d51d24f Mon Sep 17 00:00:00 2001
From: AHaumer 
Date: Tue, 21 Apr 2026 15:48:13 +0200
Subject: [PATCH 2/2] erf

---
 Modelica/Math/Special.mo                      |  2 ++
 ModelicaTest/Math.mo                          | 20 +++++++++----------
 .../Math/TestErf/comparisonSignals.txt        |  4 ----
 3 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/Modelica/Math/Special.mo b/Modelica/Math/Special.mo
index d2840efeb7..424e941a70 100644
--- a/Modelica/Math/Special.mo
+++ b/Modelica/Math/Special.mo
@@ -381,6 +381,7 @@ erfInv(1.999999)   // = -3.4589107372909473
     input Real u "Input argument";
     input Real der_u "Derivative of input";
     output Real y= 2/sqrt(Modelica.Constants.pi)*exp(-u^2)*der_u "Derivative of erf(u)";
+    annotation(smoothOrder=100);
   end erfDer;
 
   function erfcDer "Derivative of erfc-function"
@@ -388,6 +389,7 @@ erfInv(1.999999)   // = -3.4589107372909473
     input Real u "Input argument";
     input Real der_u "Derivative of input";
     output Real y=-2/sqrt(Modelica.Constants.pi)*exp(-u^2)*der_u "Derivative of erfc(u)";
+    annotation(smoothOrder=100);
   end erfcDer;
 
   function sinc "Unnormalized sinc function: sinc(u) = sin(u)/u"
diff --git a/ModelicaTest/Math.mo b/ModelicaTest/Math.mo
index 7a08ac7da6..38337844d1 100644
--- a/ModelicaTest/Math.mo
+++ b/ModelicaTest/Math.mo
@@ -843,22 +843,22 @@ extends Modelica.Icons.ExamplesPackage;
     import Modelica.Math.Special;
     Real x=time;
     Real y1=Special.erf(x);
-    Real dy1=der(y1) "der(erf)";
-    Real y1i "integral(dy1) == erf ?";
-    Real y2=Special.erfc(x) "erfc == 1 - erf ?";
-    Real dy2=der(y2) "der(erfc)";
-    Real y2i "integral(dy2) == erfc ?";
+    Real y1i "integral(der(y1)) == y1 ?";
+    Real y2=Special.erfc(x);
+    Real y2i "integral(der(y1)) == y1 ?";
   initial equation
-    y1i=Special.erf(x);
-    y2i=Special.erfc(x);
+    y1i=y1;
+    y2i=y2;
   equation
-    der(y1i)=dy1;
-    der(y2i)=dy2;
+    der(y1i)=der(y1);
+    der(y2i)=der(y2);
+    assert(abs(y1 - y1i)<1e-7, "The derivative function for erf is not correct");
+    assert(abs(y2 - y2i)<1e-7, "The derivative function for erfc is not correct");
     annotation (experiment(
         StartTime=-10,
         StopTime=10,
         Interval=0.0001,
-        Tolerance=1e-06));
+        Tolerance=1e-09));
   end TestErf;
 
   package Random
diff --git a/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt b/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt
index 940df8db01..d046686840 100644
--- a/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt
+++ b/ModelicaTest/Resources/Reference/ModelicaTest/Math/TestErf/comparisonSignals.txt
@@ -1,7 +1,3 @@
 time
-y1
-dy1
 y1i
-y2
-dy2
 y2i