Added the Nerwton solver to the Polynomial classes

This commit is contained in:
jowr
2014-06-12 08:27:49 +02:00
parent 1ccceeb7a5
commit 4a02014439
2 changed files with 50 additions and 5 deletions

View File

@@ -232,17 +232,39 @@ public:
/// @param in double value that represents the current input in x (1st dimension) or y (2nd dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param min double value that represents the minimum value
/// @param max double value that represents the maximum value
/// @param axis unsigned integer value that represents the axis to solve for (0=x, 1=y)
double solve_limits(const double &in, const double &z_in, const double &min, const double &max, const int &axis);
/// @param y_in double value that represents the current input in y (2nd dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param min double value that represents the minimum value in x (1st dimension)
/// @param max double value that represents the maximum value in x (1st dimension)
double solve_limits_x(const double &y_in, const double &z_in, const double &x_min, const double &x_max){return solve_limits(y_in, z_in, x_min, x_max, 0);}
/// @param x_in double value that represents the current input in x (1st dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param min double value that represents the minimum value in y (2nd dimension)
/// @param max double value that represents the maximum value in y (2nd dimension)
double solve_limits_y(const double &x_in, const double &z_in, const double &y_min, const double &y_max){return solve_limits(x_in, z_in, y_min, y_max, 1);}
/// @param in double value that represents the current input in x (1st dimension) or y (2nd dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param guess double value that represents the start value
/// @param axis unsigned integer value that represents the axis to solve for (0=x, 1=y)
double solve_guess(const double &in, const double &z_in, const double &guess, const int &axis);
/// @param y_in double value that represents the current input in y (2nd dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param x_guess double value that represents the start value in x (1st dimension)
double solve_guess_x(const double &y_in, const double &z_in, const double &x_guess){return solve_guess(y_in, z_in, x_guess, 0);}
/// @param x_in double value that represents the current input in x (1st dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param y_guess double value that represents the start value in y (2nd dimension)
double solve_guess_y(const double &x_in, const double &z_in, const double &y_guess){return solve_guess(x_in, z_in, y_guess, 1);}
protected:

View File

@@ -222,15 +222,29 @@ double Polynomial2D::solve(const double &in, const double &z_in, int axis = -1){
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param axis unsigned integer value that represents the axis to solve for (0=x, 1=y)
double Polynomial2D::solve_limits(const double &in, const double &z_in, const double &min, const double &max, const int &axis){
Poly2DResidual res = Poly2DResidual(*this, in, axis, z_in);
std::string errstring;
double macheps = DBL_EPSILON;
double tol = DBL_EPSILON*1e3;
int maxiter = 50;
double result = Brent(res, min, max, macheps, tol, maxiter, errstring);
if (this->do_debug()) std::cout << "Brent solver message: " << errstring << std::endl;
return result;
}
return Brent(res, min, max, macheps, tol, maxiter, errstring);
/// @param in double value that represents the current input in x (1st dimension) or y (2nd dimension)
/// @param z_in double value that represents the current output in the 3rd dimension
/// @param guess double value that represents the start value
/// @param axis unsigned integer value that represents the axis to solve for (0=x, 1=y)
double Polynomial2D::solve_guess(const double &in, const double &z_in, const double &guess, const int &axis){
Poly2DResidual res = Poly2DResidual(*this, in, axis, z_in);
std::string errstring;
//set_debug_level(1000);
double tol = DBL_EPSILON*1e3;
int maxiter = 50;
double result = Newton(res, guess, tol, maxiter, errstring);
if (this->do_debug()) std::cout << "Newton solver message: " << errstring << std::endl;
return result;
}
@@ -331,8 +345,8 @@ double Poly2DResidual::call(double in){
}
double Poly2DResidual::deriv(double in){
if (targetDim==iX) return poly.derivative(in,fixed,0)-output;
if (targetDim==iY) return poly.derivative(fixed,in,1)-output;
if (targetDim==iX) return poly.dzdx(in,fixed);
if (targetDim==iY) return poly.dzdy(fixed,in);
return -_HUGE;
}
@@ -489,6 +503,15 @@ TEST_CASE("Internal consistency checks and example use cases for PolyMath.cpp","
CHECK( check_abs(c,T,acc) );
}
c = 2.0;
c = poly.solve_guess_x(0.0, d, 350);
{
CAPTURE(T);
CAPTURE(c);
CAPTURE(d);
CHECK( check_abs(c,T,acc) );
}
// T = 0.0;
// solve.setGuess(75+273.15);
// T = solve.polyval(cHeat,c);