# Derivative respect a param

I have a TF1 object with several parameters. I can compute the derivative respect the variable with:

``f->Derivative(x)``

but if I want the first derivative respect a parameter?

The problem is that I want to propagate the error of the function from the error of the x (the data) and the parameters (stimated from a fit).

Rene

thanks, but I’m not able to use this function, I can’t understad (I think ROOT is not very documentated).

``void GradientPar(const Double_t *x, Double_t *grad, Double_t eps) ``

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.);

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.

You might be right.
Code:

void GradientPar(const Double_t *x, Double_t *grad, Double_t eps)

TF1::Gradient should work for 1-d, 2-d and 3-d functions.

You have two errors in your code
1- replace

` Double x[1]; //my point `by

2- replace:

` f->FixParameter(1,1.); `by

Rene

sorry, but it doesn’t work:

``````#include <iostream>

Double_t fitf(Double_t *x, Double_t *par)
{
return sin(par[0]*x[0]);
}

void derpar()
{
TF1 *f = new TF1("funzioneFit",fitf,-100,100,1);
Double_t derivate[1];
Double_t x[1];
x[0] = 2.;
f->FixParameter(0,1.);

std::cout << derivate[0] << std::endl;
}``````

the output is always 0 (on ROOT 5.10). I need it to propagate error.

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

Rene

well done, now it works, the output is -0.83… how it should be

Hi,

I just tried the code posted. Im using a mac with OSX 10.10.3 and ROOT version 5.34 and 6.05/01 and I still get the zero mentioned.

Furthermore if you do f->Draw() at the very end, i.e.

[code]#include

Double_t fitf(Double_t *x, Double_t *par)
{
return sin(par[0]*x[0]);
}

void derpar()
{
TF1 *f = new TF1(“funzioneFit”,fitf,-100,100,1);

``````Double_t derivate[1];
Double_t x[1];
x[0] = 2.;
f->FixParameter(0,1.);

std::cout << derivate[0] << std::endl;

f->Draw();
``````

}[/code]

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)

1. Right after the line “TF1 *f = new TF1(…);” add:
f->SetNpx(1000); // set 1000 or more points

2. If a paramter is fixed, the gradient on this parameter = 0. Thus, instead of:
f->FixParameter(0, 1.);
use:
f->SetParameter(0, 1.);

Hi,

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:

[code]#include
#include “TMath.h”
#include “TF1.h”

Double_t fitf(Double_t *x, Double_t *par)
{
return sin(x[0]*par[0]);
}

void derpar()
{
TF1 *f = new TF1(“funzioneFit”,fitf,-100,100,1);
f->SetNpx(1000); // set 1000 or more points

``````Double_t derivate[1];
Double_t x[1]={2};

f->SetParameter(0,1);