Hi,
Please, how to use the ROOT::Math::Polynomial or another function to get the polynomial parameters: P(x) = p[0] + p[1]*x + p[2] *x 2 + … + p[n] x n . I have a polynomial of n degree and i want to extract the parameters p[0], p[1] , p[2] … I need a simple example, please.
Thank you very much for your answer, I made a mistake in my question. Indeed, after introducing the parameters (SetParameters(params), how to extract the solutions (x) with ROOT::Math::Polynomial of my P(n) and not the parameters.
For evaluating the function you can just call the operator() as shown in the example above.
You can also pass the parameter pointer to the operator() ,
double x = 2;
double params = {1,2,3,4};
// evaluare polynomial for x with stored parameter values
std::cout << p(x) << std::endl;
// evaluare polynomial for x with given parameter values
double par = {4,3.2.1};
std::cout << p(x, par) << std::endl;
I know that my question is very stupid; in your example you fixed the x=2. but if I have this equation (where I know the value of n, and the parameters):
p[0] + p[1]*x + p[2] *x * 2 + … + p[n] x n = 10 for example
and I am looking for the solution x (unknown). so you know p[0] , p[1], p[2] … p[n] and you look for the solutions of your polynomial P(n). what is the solution, please?
Hi,
You can use the Polynomial::FindRoots() function, which returns a std::vectorstd::complex > since not all solutions are real
For example to find the roots for x^3 - 7x + 6 = 0, you do :
ROOT::Math::Polynomial p(3);
// the number of parameters is n (polynomial degree ) + 1
double par[] = { 6,-7,0,1};
p.SetParameters(par);
auto r = p.FindRoots();
for (size_t i = 0; i < r.size(); ++i) { std::cout << r[i] << std::endl; }