Segmentation violation in TF1

Hello,
My problem is as follows: I wanted to implement the Lagrange interpolation and defined a double to create a TF1. Everything works fine as long as I don’t try drawing the interpolation function. The code and the error report are shown in the figure.
Thanks in advance.



_ROOT Version:6.24/08

Welcome to the ROOT forum,

Can you post your code as text so we can copy/paste it in order to try it?
Thanks

Of course. Thanks for your kindness.

const Int_t num = 11;

Double_t la(Int_t n, Double_t *x, Double_t *par)
{
Double_t ln = 1;
for(Int_t i = 0; i < num; i++)
{
if(i != n)
ln = ln * (x[0] - par[i]) / (par[n] - par[i]);
}
ln = ln * par[n+num];
return ln;
}

Double_t lp(Double_t *x, Double_t *par)
{
Double_t La = 0;
for(Int_t i = 0; i < num; i++)
{
La += la(i, x, par);
}
return La;
}

void lagrange_interpolation()
{
Double_t X[num];
Double_t Y[num];
for(Int_t i = 0; i < num; i ++)
{
X[i] = -1 + 0.2 * i;
Y[i] = 1 / (1 + 25 * pow(X[i],2));
}
Double_t par[2*num];
for(Int_t i = 0; i < num ;i ++)
{
par[i] = X[i];
par[i+num] = Y[i];
}
TF1 f1 = new TF1(“f1”, "1/(1+25(x)^2)", -2, 2);
TF1 *la = new TF1(“la”, lp, -2,2);
la → SetParameters(par);
TCanvas *c1 = new TCanvas(“lagrange_interpolation”,“lagrange_interpolation”,1);
c1 → Divide(2,1);
c1 → cd(1);
f1 → Draw();
c1 → cd(2);
la → Draw();
}

I fixed some problems:

const Int_t num = 11;

Double_t la(Int_t n, Double_t *x, Double_t *par) {
   Double_t ln = 1;
   for (Int_t i = 0; i < num; i++) {
      if(i != n)
      ln = ln * (x[0] - par[i]) / (par[n] - par[i]);
   }
   ln = ln * par[n+num];
   return ln;
   return 0;
}

Double_t lp(Double_t *x, Double_t *par) {
   Double_t La = 0;
   for (Int_t i = 0; i < num; i++) La += la(i, x, par);
   return La;
}

void lagrange_interpolation()
{
   Double_t X[num];
   Double_t Y[num];
   for (Int_t i = 0; i < num; i ++) {
      X[i] = -1 + 0.2 * i;
      Y[i] = 1 / (1 + 25 * pow(X[i],2));
   }
   Double_t par[2*num];
   for (Int_t i = 0; i < num ;i ++) {
      par[i]     = X[i];
      par[i+num] = Y[i];
   }
   auto f1 = new TF1("f1", "1./(1+25*x*x)", -2., 2.);
   auto la = new TF1("la", lp, -2.,2.);
   la->SetParameters(par);

   auto c1 = new TCanvas("lagrange_interpolation","lagrange_interpolation",1);
   c1->Divide(2,1);
   c1->cd(1); f1->Draw();
//   c1->cd(2); la->Draw();  // This line crashes the function "la" defined above is wrong.
//    cout << la->Eval(0.) << endl; // This line also crash.. same reason
}

But your funtion la is malformed. You should revisit/debug it.

Thank you for your help!
I tried to use files in the turorials to find the problem. I find that the error code will occur if I do not indicate the number of parameters when creating a TF1 with a user function. My problem remained unsolved however after I rewrote my code.
After I intergrated la and lp, the problem were solved somehow. Yet I cannot figure out the inner mechanism.
Thanks again for your help. By the way, September 29th is the Chinese Mid-Autumn Festival. Wish you a happy Mid-Autumn Festival!

Good if your problem is solved !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.