diff --git a/Modelica/Math/Nonlinear.mo b/Modelica/Math/Nonlinear.mo index 62d6ce19e8..c827dba958 100644 --- a/Modelica/Math/Nonlinear.mo +++ b/Modelica/Math/Nonlinear.mo @@ -48,6 +48,7 @@ package Nonlinear "Library of functions operating on nonlinear equations" print("Numerical integral value = " + String(I_numerical[1], format= "2.16f")); print("Absolute difference = " + String(I_err[1], format="2.0e")); + assert(abs(I_err[1])<1e-4, "Integral should be close to desired"); print(""); print("Function 2 (integral(sin(5*x)*dx) from x=0 to x=13): "); @@ -56,6 +57,7 @@ package Nonlinear "Library of functions operating on nonlinear equations" print("Numerical integral value = " + String(I_numerical[2], format= "2.16f")); print("Absolute difference = " + String(I_err[2], format="2.0e")); + assert(abs(I_err[2])<1e-4, "Integral should be close to desired"); print(""); print("Function 3 (Elliptic integral from x=0 to pi/2): "); @@ -64,6 +66,7 @@ package Nonlinear "Library of functions operating on nonlinear equations" print("Numerical integral value = " + String(I_numerical[3], format= "2.16f")); print("Absolute difference = " + String(I_err[3], format="2.0e")); + assert(abs(I_err[3])<1e-4, "Integral should be close to desired"); annotation (Documentation(info="
@@ -200,18 +203,21 @@ The following integrals are computed: print("Analytical zero = " + String(u_analytical[1], format="2.16f")); print("Numerical zero = " + String(u_numerical[1], format="2.16f")); print("Absolute difference = " + String(u_err[1], format="2.0e")); + assert(abs(u_err[1])<1e-10, "Solution should be close to desired"); print(""); print("Function 2 (3*u - sin(3*u) - 1 = 0): "); print("Analytical zero = " + String(u_analytical[2], format="2.16f")); print("Numerical zero = " + String(u_numerical[2], format="2.16f")); print("Absolute difference = " + String(u_err[2], format="2.0e")); + assert(abs(u_err[2])<1e-10, "Solution should be close to desired"); print(""); print("Function 3 (5 + log(u) - u = 0): "); print("Analytical zero = " + String(u_analytical[3], format="2.16f")); print("Numerical zero = " + String(u_numerical[3], format="2.16f")); print("Absolute difference = " + String(u_err[3], format="2.0e")); + assert(abs(u_err[3])<1e-10, "Solution should be close to desired"); annotation (Documentation(info="
diff --git a/Modelica/Math/package.mo b/Modelica/Math/package.mo index 79882c4e68..62bd4a2df8 100644 --- a/Modelica/Math/package.mo +++ b/Modelica/Math/package.mo @@ -718,26 +718,36 @@ package Matrices "Library of functions operating on matrices" Real B3[3, 2]=[b3, -3*b3]; Real X3[3, 2]; + Real diff; algorithm print("\nDemonstrate how to solve linear equation systems:\n"); // Solve regular linear equation with a right hand side vector x1 := Math.Matrices.solve(A1, b1); - print("diff1 = " + String(Vectors.norm(x1 - x1_ref))); + diff := Vectors.norm(x1 - x1_ref); + print("diff1 = " + String(diff)); + assert(abs(diff)<1e-10, "Solution should be close to desired"); // Solve regular linear equation with a right hand side matrix X2 := Math.Matrices.solve2(A1, B2); - print("diff2 = " + String(Matrices.norm(X2 - [x1_ref, 2*x1_ref, -3*x1_ref]))); + diff := Matrices.norm(X2 - [x1_ref, 2*x1_ref, -3*x1_ref]); + print("diff2 = " + String(diff)); + assert(abs(diff)<1e-10, "Solution should be close to desired"); // Solve singular linear equation with a right hand side vector (x3,rank) := Math.Matrices.leastSquares(A3, b3); - print("diff3 = " + String(Vectors.norm(A3*x3 - b3)) + ", n = " + String( + diff := Vectors.norm(A3*x3 - b3); + print("diff3 = " + String(diff) + ", n = " + String( size(A3, 1)) + ", rank = " + String(rank)); + assert(abs(diff)<1e-10, "Solution should be close to desired"); + // Solve singular linear equation with a right hand side matrix (X3,rank) := Math.Matrices.leastSquares2(A3, B3); - print("diff4 = " + String(Matrices.norm(A3*X3 - B3)) + ", n = " + String( + diff := Matrices.norm(A3*X3 - B3); + print("diff4 = " + String(diff) + ", n = " + String( size(A3, 1)) + ", rank = " + String(rank)); + assert(abs(diff)<1e-10, "Solution should be close to desired"); annotation (Documentation(info="