1

I just started using boost today and found this post to be extremely helpful. I am attempting to use boost::bisect to solve a parametric equation for a series of values. The following works if I want to solve for a value of 0.8:

#include <boost/math/tools/roots.hpp>   

//declare constants
const double Y_r = 0.2126;
const double Y_g = 0.7152;
const double Y_b = 0.0722;

struct FunctionToApproximate  {
  double operator() (double x)  {
      return (pow (x, 2.4) * Y_r) + (pow ((x*0.6)+0.4, 2.4) * Y_g) + (1 * Y_b) - 0.8;
  }
};

struct TerminationCondition  {
  bool operator() (double min, double max)  {
    return fabs(min - max) <= t_c;
  }
};

using boost::math::tools::bisect;

std::pair<double, double> result = bisect(FunctionToApproximate(), 0.0, 1.0, TerminationCondition());

double root = (result.first + result.second) / 2;

I would like to wrap this in a loop so I could solve for values other than 0.8. How would I go about this?

Many thanks!

gmculp
  • 135
  • 1
  • 10

1 Answers1

1

You can add a state/data member to FunctionToApproximate to hold the value that you want to substract in each call:

struct FunctionToApproximate  {
    FunctionToApproximate(double val) :val(val){}
    double operator() (double x)  {
        return (pow (x, 2.4) * Y_r) + (pow ((x*0.6)+0.4, 2.4) * Y_g) + (1 * Y_b) - val;
    }
    double val;
};

Then wrap the calculations inside a loop should be straight forward.

for(double i = 0.1; i <= 1.0; i+=1.0) {
    std::pair<double, double> result = bisect(FunctionToApproximate(i), 0.0, 1.0, TerminationCondition());

    double root = (result.first + result.second) / 2;
}
gchen
  • 1,173
  • 1
  • 7
  • 12
  • you are amazing! Thank you! – gmculp May 06 '18 at 01:58
  • I have two different equations I would like to solve for a series of values. If I had functions for these equations, how could I add them as another input to the struct? Would it be best to just use two separate structs? – gmculp May 06 '18 at 17:51
  • 1
    @gmculp if those functions are completely different from each other, I'd say yes, create new struct is better. – gchen May 06 '18 at 18:03