Hi everyone. I have problem to deal, in which I have to solve a complicated non-linear equation. I plan to solve it with Bisection method (numerical algorithms). Firstly, I try solving a very simple equation with Bisection method. I read documentation RootFinder. I a little bit undersand. Therefore there is some error. Could you please help me? This is my code.
#include <iostream>
#include <Math/RootFinderAlgorithms.h>
#include <TF1.h>
#include <Math/RootFinder.h>
using namespace ROOT::Math;
using namespace std;
double myfunc(double x)
{
return 3*x - 10;
}
void find_root()
{
RootFinder *k = new RootFinder();
k->SetMethod(RootFinder::kGSL_BISECTION);
k->SetFunction(myfunc, 1, 10);
double c = k->Root();
cout << c << endl;
}
You need to use the ROOT::Math::Functor1D class to wrap your function pointer in the right function object required by the ROOT Finder and to call RootFinder::Solve().
Here is a snippet of working code:
#include <iostream>
#include <Math/RootFinderAlgorithms.h>
#include <TF1.h>
#include <Math/RootFinder.h>
#include <Math/Functor.h>
using namespace ROOT::Math;
using namespace std;
double myfunc(double x)
{
return 3*x - 10;
}
void find_root()
{
RootFinder *k = new RootFinder();
k->SetMethod(RootFinder::kGSL_BISECTION);
ROOT::Math::Functor1D f(&myfunc);
k->SetFunction(f, 1, 10);
k->Solve();
double c = k->Root();
cout << c << endl;
}
Thank you, Lorenzo. It worked well. I still have a question. When I try solve a equation with Newton method, I use the ROOT::Math::GradFunctor1D to wrap my derivative, but there is some error. Could you please put where is my mistake? Here is my code.
Hi,
for using the derivatives, here is the correct code:
RootFinder *k = new RootFinder();
k->SetMethod(RootFinder::kGSL_NEWTON);
// create gradient functor passing both a pointer to a function implementing function evaluation and
// a pointer to a function implementing the derivative
ROOT::Math::GradFunctor1D g(&myfunc, &derivative);
k->SetFunction(g, 4);
k->Solve();
Hi Lorenzo. When I solve equation system with numerical method, I wrap TF2 object by ROOT::Math::WrappedMultiTF1, but there are some warnings and no roots. What can I do to deal with the problem? Thank you.
Hi,
The code seems correct to me, apart from the printing of the ROOT. Now in the multi-dim case , a pointer to a vector of roots is returned in the function r->X().
So you should print the roots by doing :
cout << r->X()[0] << " " << r->X()[1] << endl;
I see also you are using a very old version, 5.34.36 and that might explains the warnings. I think in that case you need to compile the code and not work with cint.
Hi Lorenzo. It works done. Thanks for your help.
I am using a old version because it properly work on win 10. I spent much time installing newer version of Root (file .exe) on win 10, but there were some same errors. So that I decided to use that version.