extract newton's method

This commit is contained in:
Matthew Kennedy 2023-03-07 00:47:02 -08:00
parent 773bb5da24
commit c26cda0d37
3 changed files with 11 additions and 23 deletions

View File

@ -87,26 +87,14 @@ float HellenBoardIdSolver::solve(float Tc1, float Tc2, float x0, float y, float
k3 = iC * (Tc1 - Tc2);
// the same method works for R (if C is known) or C (if R is known)
float Xcur, Xnext;
Xnext = x0;
auto result = NewtonsMethodSolver::solve(x0, deltaX, 20);
// since we had https://github.com/rusefi/rusefi/issues/4084 let's add paranoia check
// All real cases seem to converge in <= 5 iterations, so we don't need to try more than 20.
int safetyLimit = 20;
do {
if (safetyLimit-- < 0) {
firmwareError(OBD_PCM_Processor_Fault, "hellen boardID is broken");
break;
}
Xcur = Xnext;
Xnext = Xcur - fx(Xcur) / dfx(Xcur);
if (!result) {
firmwareError(OBD_PCM_Processor_Fault, "hellen boardID is broken");
return 0;
}
#ifdef HELLEN_BOARD_ID_DEBUG
efiPrintf ("* %f", Xnext);
#endif /* HELLEN_BOARD_ID_DEBUG */
} while (absF(Xnext - Xcur) > deltaX);
return Xnext;
return result.Value;
}
float HellenBoardIdFinderBase::findClosestResistor(float R, bool testOnlyMajorSeries, int *rIdx) {

View File

@ -27,15 +27,15 @@ public:
// We need to solve the following equation for R or C:
// X^Td - X^(Tc1+Td) + X^(Tc2-Tc1) - 1 = 0
// where: X = exp(-1/(RC))
class HellenBoardIdSolver
class HellenBoardIdSolver : public NewtonsMethodSolver
{
public:
float fx(float x) {
float fx(float x) override {
return exp(k1 / x) - exp(k2 / x) + exp(k3 / x) - 1.0;
}
// first-order derivative
float dfx(float x) {
float dfx(float x) override {
return (-1.0f / (x * x)) * (k1 * exp(k1 / x) - k2 * exp(k2 / x) + k3 * exp(k3 / x));
}

@ -1 +1 @@
Subproject commit 8e65946de929c7fafa00ca23f57c86b4e9c4177f
Subproject commit 163cee4279c83f46efccd279ce22006d039fb2bb