first: why x in a pointer? and why this example doesn’t work:
Double_t fitf(Double_t *x, Double_t *par)
{
return sin(par[0]*x[0]);
}
void main()
{
TF1 *f = new TF1("f", fitf, -100, 100, 1);
Double_t derivate[1]; // there is only one parameter
Double x[1]; //my point
x[0] = 2.;
f->FixParameter(1,1.);
f->GradientPar(x, derivate);
std::cout << derivate[0];
}
I think that the output code is the first derivative of sin(par[0]2) respect par[0], evaluated in par[0] = 1, so it is: 2cos(1*2) \simeq -0.8, but the output of ROOT is always 0.
OK, I can reproduce the problem now with this example.
It happens when running with an interpreted function.
I have fixed the problem in the CVS head.
Thanks for the report
you get the sine function that I attached, which definitely does not look like a sine function. Why does ROOT plot the sine modulated, and can you reproduce the 0 that I am getting for the gradient? I also tried with SL6 and ROOT v 5.34/25 from CVMFS and I am getting the same result. sine_plot.pdf (13.9 KB)
You are right, increasing the number of points evaluated fixes the plot.
However it seems that when ROOT gets the gradient of sin( par[0]*x[0] ) it gets the derivative in function of par[0]:
x[0]*cos(par[0]x[0])
Setting par[0] = 1 and x[0] =2 you get 2 cos(2). However the intuitive way of doing this would be to take the derivative with respect to x[0] so that if you fix par[0] to 1 and make x[0]=2 you get:
par[0]*cos(par[0]*x[0]) = cos(2)
Why are the gradients taken with respect to the par’s instead of the x’s? If you take a derivative, which is a 1d gradient, you get the derivatives with respect to x. I mean: