Skip to content

Commit

Permalink
more robust rounding & tolerances definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
istefanis committed Feb 7, 2024
1 parent e131222 commit a325b9f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 68 deletions.
16 changes: 11 additions & 5 deletions math/numericalAnalysis/numericalAnalysisService.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { Complex } from "../../assets/lib/Complex/Complex.js";
import findRoots from "../../assets/lib/durand-kerner/roots.js";
import {
roundDecimal,
roundDecimalDigitsNumericalAnalysis,
toleranceNumericalAnalysisSmall,
zeroRootsFromPolynomialTermsArray,
} from "../../util/commons.js";

Expand All @@ -40,8 +42,6 @@ import {
//

let maxLoopCounter = 0;

export const tolerance = 0.0001;
const maxLoops = 1000;

/**
Expand Down Expand Up @@ -69,7 +69,10 @@ const computeFixedPoint = function (f, firstGuess) {
} else {
const next = f(guess);
// console.log(next);
if (Math.abs(guess - next) < tolerance || maxLoopCounter > maxLoops) {
if (
Math.abs(guess - next) < toleranceNumericalAnalysisSmall ||
maxLoopCounter > maxLoops
) {
return next;
} else {
if (isNaN(next)) {
Expand All @@ -94,7 +97,7 @@ const computeFixedPoint = function (f, firstGuess) {
export const halfIntervalMethod = function (f, a, b) {
const loop = function (an, bn, n) {
const m = (an + bn) / 2;
if (n > maxLoops || Math.abs(bn - an) < tolerance) {
if (n > maxLoops || Math.abs(bn - an) < toleranceNumericalAnalysisSmall) {
return m;
} else {
return f(an) * f(m) < 0 ? loop(an, m, n + 1) : loop(m, bn, n + 1);
Expand Down Expand Up @@ -191,7 +194,10 @@ export const findComplexRootsOfPolynomial = function (termsArray) {
if (r && r[0]) {
for (let i = 0; i <= r[0].length - 1; i++) {
//real & imag parts respectively
complexRoots.push([roundDecimal(r[0][i], 3), roundDecimal(r[1][i], 3)]);
complexRoots.push([
roundDecimal(r[0][i], roundDecimalDigitsNumericalAnalysis),
roundDecimal(r[1][i], roundDecimalDigitsNumericalAnalysis),
]);
}
}
return complexRoots.sort(function (x1, x2) {
Expand Down
8 changes: 5 additions & 3 deletions test/custom/simplificationAlgorithms.customTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import {
animationSpeedCoeff,
setAnimationSpeedCoeff,
} from "../../view/navbarView.js";
import { areEqualArraysRoundDecimal } from "../../util/commons.js";
import {
areEqualArraysRoundDecimal,
roundDecimalDigitsTests,
} from "../../util/commons.js";
import { logMessages } from "../../util/loggingService.js";
import { simplificationAlgorithmsTests } from "../definitions/simplificationAlgorithmsTests.js";

let testsBlock = new Block();
const roundingDigits = 3;

const runSimplificationAlgorithmsCustomTest = async (t) => {
testsBlock.clearState();
Expand All @@ -28,7 +30,7 @@ const runSimplificationAlgorithmsCustomTest = async (t) => {
const testCondition = areEqualArraysRoundDecimal(
actualValue,
expectedValue,
roundingDigits
roundDecimalDigitsTests
);

logMessages(
Expand Down
94 changes: 46 additions & 48 deletions test/definitions/plotsTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
* Test / Definitions / PlotsTests
*/

import { findComplexRootsOfPolynomial } from "../../math/numericalAnalysis/numericalAnalysisService.js";
import {
tolerance,
findComplexRootsOfPolynomial,
} from "../../math/numericalAnalysis/numericalAnalysisService.js";
toleranceTestsSmall,
toleranceTestsMedium,
toleranceTestsLarge,
} from "../../util/commons.js";
import BodePlot from "../../view/plots/bodePlot.js";
import NyquistPlot from "../../view/plots/nyquistPlot.js";

const toleranceSmall = tolerance; //0.0001
const toleranceMedium = 0.2;
const toleranceLarge = 3;

const bodeSteps = (numeratorTermsArray, denominatorTermsArray) => {
const { magnitudeCurvePoints, phaseCurvePoints, characteristicNumbers } =
new BodePlot(
Expand Down Expand Up @@ -65,25 +63,25 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"60 [dB/dec] (low), 60 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "270", toleranceSmall],
["Phase at wMin", phaseCurvePoints[0][1], "270", toleranceTestsSmall],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"270",
toleranceSmall,
toleranceTestsSmall,
],
["Real part at wMin", nyquistCurvePoints[0][1], 0, toleranceSmall],
["Real part at wMin", nyquistCurvePoints[0][1], 0, toleranceTestsSmall],
[
"Imag part at w=0",
nyquistCurvePoints[Math.ceil(nyquistCurvePoints.length / 2)][2],
0,
toleranceSmall,
toleranceTestsSmall,
],
[
"Real part at wMax",
nyquistCurvePoints[nyquistCurvePoints.length - 1][1],
0,
toleranceSmall,
toleranceTestsSmall,
],
["Stable", stability, "no"],
],
Expand All @@ -106,26 +104,26 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"-60 [dB/dec] (low), -60 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "-270", toleranceSmall],
["Phase at wMin", phaseCurvePoints[0][1], "-270", toleranceTestsSmall],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-270",
toleranceSmall,
toleranceTestsSmall,
],
["Real part at wMin", nyquistCurvePoints[0][1], 0, toleranceSmall],
["Imag part at wMin", nyquistCurvePoints[0][2], 0, toleranceSmall],
["Real part at wMin", nyquistCurvePoints[0][1], 0, toleranceTestsSmall],
["Imag part at wMin", nyquistCurvePoints[0][2], 0, toleranceTestsSmall],
[
"Real part at wMax",
nyquistCurvePoints[nyquistCurvePoints.length - 1][1],
0,
toleranceSmall,
toleranceTestsSmall,
],
[
"Imag part at wMax",
nyquistCurvePoints[nyquistCurvePoints.length - 1][2],
0,
toleranceSmall,
toleranceTestsSmall,
],
["Stable", stability, "no"],
],
Expand All @@ -146,12 +144,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), 40 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceSmall],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsSmall],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"180",
toleranceSmall,
toleranceTestsSmall,
],
],
},
Expand All @@ -171,12 +169,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), -40 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceSmall],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsSmall],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-180",
toleranceSmall,
toleranceTestsSmall,
],
],
},
Expand All @@ -196,12 +194,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), -40 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-180",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -221,12 +219,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"-20 [dB/dec] (low), -20 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceSmall],
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceTestsSmall],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-90",
toleranceSmall,
toleranceTestsSmall,
],
],
},
Expand All @@ -241,12 +239,12 @@ export const plotsTests = {
phaseCurvePoints,
characteristicNumbers
) => [
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"90",
toleranceLarge,
toleranceTestsLarge,
],
],
},
Expand All @@ -261,12 +259,12 @@ export const plotsTests = {
phaseCurvePoints,
characteristicNumbers
) => [
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"90",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -281,12 +279,12 @@ export const plotsTests = {
phaseCurvePoints,
characteristicNumbers
) => [
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"0",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -301,12 +299,12 @@ export const plotsTests = {
phaseCurvePoints,
characteristicNumbers
) => [
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"0",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -326,12 +324,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), 20 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"90",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -351,12 +349,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), 0 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-180",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -376,12 +374,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), 0 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "-180", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "-180", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"0",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -396,12 +394,12 @@ export const plotsTests = {
phaseCurvePoints,
characteristicNumbers
) => [
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-360",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -421,12 +419,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"-20 [dB/dec] (low), -100 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "-90", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-450",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -448,12 +446,12 @@ export const plotsTests = {
phaseCurvePoints,
characteristicNumbers
) => [
["Phase at wMin", phaseCurvePoints[0][1], "-720", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "-720", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-720",
toleranceMedium,
toleranceTestsMedium,
],
],
},
Expand All @@ -475,12 +473,12 @@ export const plotsTests = {
characteristicNumbers?.rollOffText,
"0 [dB/dec] (low), -40 [dB/dec] (high)",
],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceMedium],
["Phase at wMin", phaseCurvePoints[0][1], "0", toleranceTestsMedium],
[
"Phase at wMax",
phaseCurvePoints[phaseCurvePoints.length - 1][1],
"-1260",
toleranceLarge,
toleranceTestsLarge,
],
],
},
Expand Down
Loading

0 comments on commit a325b9f

Please # to comment.