# Build a TF function from TFormula

**URL:** <https://root-forum.cern.ch/t/build-a-tf-function-from-tformula/62290>\
**Category:** Newbie\
**Tags:** root\
**Created:** [November 21, 2024, 2:11pm UTC](https://root-forum.cern.ch/t/build-a-tf-function-from-tformula/62290 "2024-11-21T14:11:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrSi1ence](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@MrSi1ence](https://root-forum.cern.ch/u/MrSi1ence)\
**Post date:** [November 21, 2024, 2:11pm UTC](https://root-forum.cern.ch/t/build-a-tf-function-from-tformula/62290/1 "2024-11-21T14:11:58Z")

</div>

I want to build a TF1 function from 2 TFormula function:(for newbie practice)

```auto
{
   auto fa1 = new TFormula("fa1","[0]*cos([1]*x)");
   fa1->SetParameter(0,7);
   fa1->SetParameter(1,6);
   auto fa2 = new TFormula("fa2","[2]*sin([3]*x)");
   fa2->SetParameter(2,5);
   fa2->SetParameter(3,4);
   auto f1 = new TF1("f1","fa1+fa2",-TMath::Pi(),TMath::Pi());
   auto f2 = new TF1("f2","7*cos(6*x)+5*sin(4*x)",-TMath::Pi(),TMath::Pi());
   f2->SetLineColor(kBlue);
   f1->Draw();
   f2->Draw("same")
}

```

However, it seems that the f1 function only contains the fa1 part, and whatever I do, fa2 can’t be correctly added to f1(means f1 only draws the cosine plot) .I’m curious about why I set parameter in the same way but have different effects. And what should I do to modify the code?  
By the way, what’s the difference between TF1 and TFormula class? I don’t know how to choose them.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [November 21, 2024, 2:52pm UTC](https://root-forum.cern.ch/t/build-a-tf-function-from-tformula/62290/2 "2024-11-21T14:52:27Z")

</div>

Use SetParameter on `f1` instead of on `fa1,fa2`

```auto
{
   auto fa1 = new TFormula("fa1","[0]*cos([1]*x)");
   auto fa2 = new TFormula("fa2","[2]*sin([3]*x)");
   auto f1 = new TF1("f1","fa1+fa2",-TMath::Pi(),TMath::Pi());
   f1->SetParameter(0,7);
   f1->SetParameter(1,6);
   f1->SetParameter(2,5);
   f1->SetParameter(3,4);
   f1->SetLineWidth(2);
   auto f2 = new TF1("f2","7*cos(6*x)+5*sin(4*x)",-TMath::Pi(),TMath::Pi());
   f2->SetLineStyle(10);
   f2->SetLineColor(kBlue);
   f1->Draw();
   f2->Draw("same");
}

```

 ![c1](https://root-forum.cern.ch/uploads/default/original/3X/a/4/a43fef04c8c18d9ee9fbac6dd8c783bde0a56db7.png)

---

<div class="post-metadata">

**Author:** ![MrSi1ence](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@MrSi1ence](https://root-forum.cern.ch/u/MrSi1ence)\
**Post date:** [November 22, 2024, 12:21am UTC](https://root-forum.cern.ch/t/build-a-tf-function-from-tformula/62290/4 "2024-11-22T00:21:51Z")

</div>

Oh, I think I‘ve got the point, thank you!
