ROOT logo

From $ROOTSYS/tutorials/math/Bessel.C

// Show the different kinds of Bessel functions available in ROOT 
// To execute the macro type in:
//
// root[0]: .x Bessel.C 
//
// It will create one canvas with the representation 
// of the  cylindrical and spherical Bessel functions
// regular and modified

//
//  Author: Magdalena Slawinska

#include "TMath.h"
#include "TF1.h"
#include "TCanvas.h"

#include <Riostream.h>
#include "TLegend.h"
#include "TLegendEntry.h"

#include "Math/IFunction.h"
#include <cmath>
#include "TSystem.h"
#include "TAxis.h"
#include "TPaveLabel.h"

void Bessel()
{
  gSystem->Load("libMathMore");

  TCanvas *DistCanvas = new TCanvas("DistCanvas", 
     "Bessel functions example", 10, 10, 1000, 800);  
  DistCanvas->SetFillColor(17);
  DistCanvas->Divide(2, 2);
  DistCanvas->cd(1);
  gPad->SetGrid();
  gPad->SetFrameFillColor(19);
  TLegend *leg = new TLegend(0.75, 0.7, 0.89, 0.89); 

  int n = 5;//number of functions in each pad
//drawing the set of Bessel J functions   
  TF1* JBessel[5];
  for(int nu = 0; nu < n; nu++)
  {
      JBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_j([0],x)", 0, 10);
      JBessel[nu]->SetParameters(nu, 0.0);
      JBessel[nu]->SetTitle("");//Bessel J functions");  
      JBessel[nu]->SetLineStyle(1);
      JBessel[nu]->SetLineWidth(3);
      JBessel[nu]->SetLineColor(nu+1);
  }
  JBessel[0]->TF1::GetXaxis()->SetTitle("x");
  JBessel[0]->GetXaxis()->SetTitleSize(0.06);
  JBessel[0]->GetXaxis()->SetTitleOffset(.7);

  //setting the title in a label style
  TPaveLabel *p1 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,
     "Bessel J functions", "NDC");
p1->SetFillColor(0);
p1->SetTextFont(22);
p1->SetTextColor(kBlack);

//setting the legend
  leg->AddEntry(JBessel[0]->DrawCopy(), " J_0(x)", "l");
  leg->AddEntry(JBessel[1]->DrawCopy("same"), " J_1(x)", "l");
  leg->AddEntry(JBessel[2]->DrawCopy("same"), " J_2(x)", "l");
  leg->AddEntry(JBessel[3]->DrawCopy("same"), " J_3(x)", "l");
  leg->AddEntry(JBessel[4]->DrawCopy("same"), " J_4(x)", "l");

  leg->Draw();
  p1->Draw();

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   DistCanvas->cd(2);
   gPad->SetGrid();
   gPad->SetFrameFillColor(19);

   TLegend *leg2 = new TLegend(0.75, 0.7, 0.89, 0.89); 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Drawing Bessel k  
   TF1* KBessel[5];
   for(int nu = 0; nu < n; nu++)
    {
      KBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_k([0],x)", 0, 10);
      KBessel[nu]->SetParameters(nu, 0.0);
      KBessel[nu]->SetTitle("Bessel K functions");  
      KBessel[nu]->SetLineStyle(1);
      KBessel[nu]->SetLineWidth(3);
      KBessel[nu]->SetLineColor(nu+1);
    }
    KBessel[0]->GetXaxis()->SetTitle("x");
    KBessel[0]->GetXaxis()->SetTitleSize(0.06);
    KBessel[0]->GetXaxis()->SetTitleOffset(.7);

    //setting title
    TPaveLabel *p2 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,
       "Bessel K functions", "NDC");
    p2->SetFillColor(0);
    p2->SetTextFont(22);
    p2->SetTextColor(kBlack);

    //setting legend
    leg2->AddEntry(KBessel[0]->DrawCopy(), " K_0(x)", "l");
    leg2->AddEntry(KBessel[1]->DrawCopy("same"), " K_1(x)", "l");
    leg2->AddEntry(KBessel[2]->DrawCopy("same"), " K_2(x)", "l");
    leg2->AddEntry(KBessel[3]->DrawCopy("same"), " K_3(x)", "l");
    leg2->AddEntry(KBessel[4]->DrawCopy("same"), " K_4(x)", "l");
//   for(int nu = 1; nu <= 4; nu++)
//   leg->AddEntry(JBessel[0]->DrawCopy("lsame"), "", "l");
  

    leg2->Draw();
    p2->Draw();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   DistCanvas->cd(3);
   gPad->SetGrid();
   gPad->SetFrameFillColor(19);
   TLegend *leg3 = new TLegend(0.75, 0.7, 0.89, 0.89); 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 //Drawing Bessel i     
   TF1* iBessel[5];
   for(int nu = 0; nu <= 4; nu++)
    {
      iBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_i([0],x)", 0, 10);
      iBessel[nu]->SetParameters(nu, 0.0);
      iBessel[nu]->SetTitle("Bessel I functions");  
      iBessel[nu]->SetLineStyle(1);
      iBessel[nu]->SetLineWidth(3);
      iBessel[nu]->SetLineColor(nu+1);
    }

    iBessel[0]->GetXaxis()->SetTitle("x");   
    iBessel[0]->GetXaxis()->SetTitleSize(0.06);
    iBessel[0]->GetXaxis()->SetTitleOffset(.7);

    //setting title
    TPaveLabel *p3 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,
       "Bessel I functions", "NDC");
    p3->SetFillColor(0);
    p3->SetTextFont(22);
    p3->SetTextColor(kBlack);

    //setting legend
    leg3->AddEntry(iBessel[0]->DrawCopy(), " I_0", "l");
    leg3->AddEntry(iBessel[1]->DrawCopy("same"), " I_1(x)", "l");
    leg3->AddEntry(iBessel[2]->DrawCopy("same"), " I_2(x)", "l");
    leg3->AddEntry(iBessel[3]->DrawCopy("same"), " I_3(x)", "l");
    leg3->AddEntry(iBessel[4]->DrawCopy("same"), " I_4(x)", "l");
//   for(int nu = 1; nu <= 4; nu++)
//   leg->AddEntry(JBessel[0]->DrawCopy("lsame"), "", "l");
  
    //iBessel[0]->Draw();
    leg3->Draw();
    p3->Draw();
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   DistCanvas->cd(4);
   gPad->SetGrid();
   gPad->SetFrameFillColor(19);
   TLegend *leg4 = new TLegend(0.75, 0.7, 0.89, 0.89); 

  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   //drawing sph_bessel
   TF1* jBessel[5];
   for(int nu = 0; nu <= 4; nu++)
   {
      jBessel[nu]= new TF1("J_0", "ROOT::Math::sph_bessel([0],x)", 0, 10);
      jBessel[nu]->SetParameters(nu, 0.0);
      jBessel[nu]->SetTitle("Bessel j functions");  
      jBessel[nu]->SetLineStyle(1);
      jBessel[nu]->SetLineWidth(3);
      jBessel[nu]->SetLineColor(nu+1);
   }
    jBessel[0]->GetXaxis()->SetTitle("x");   
    jBessel[0]->GetXaxis()->SetTitleSize(0.06);
    jBessel[0]->GetXaxis()->SetTitleOffset(.7);

    //setting title
    TPaveLabel *p4 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,
       "Bessel j functions", "NDC");
    p4->SetFillColor(0);
    p4->SetTextFont(22);
    p4->SetTextColor(kBlack);

    //setting legend

    leg4->AddEntry(jBessel[0]->DrawCopy(), " j_0(x)", "l");
    leg4->AddEntry(jBessel[1]->DrawCopy("same"), " j_1(x)", "l");
    leg4->AddEntry(jBessel[2]->DrawCopy("same"), " j_2(x)", "l");
    leg4->AddEntry(jBessel[3]->DrawCopy("same"), " j_3(x)", "l");
    leg4->AddEntry(jBessel[4]->DrawCopy("same"), " j_4(x)", "l");

    leg4->Draw();
    p4->Draw();
 
    DistCanvas->cd();
}


 Bessel.C:1
 Bessel.C:2
 Bessel.C:3
 Bessel.C:4
 Bessel.C:5
 Bessel.C:6
 Bessel.C:7
 Bessel.C:8
 Bessel.C:9
 Bessel.C:10
 Bessel.C:11
 Bessel.C:12
 Bessel.C:13
 Bessel.C:14
 Bessel.C:15
 Bessel.C:16
 Bessel.C:17
 Bessel.C:18
 Bessel.C:19
 Bessel.C:20
 Bessel.C:21
 Bessel.C:22
 Bessel.C:23
 Bessel.C:24
 Bessel.C:25
 Bessel.C:26
 Bessel.C:27
 Bessel.C:28
 Bessel.C:29
 Bessel.C:30
 Bessel.C:31
 Bessel.C:32
 Bessel.C:33
 Bessel.C:34
 Bessel.C:35
 Bessel.C:36
 Bessel.C:37
 Bessel.C:38
 Bessel.C:39
 Bessel.C:40
 Bessel.C:41
 Bessel.C:42
 Bessel.C:43
 Bessel.C:44
 Bessel.C:45
 Bessel.C:46
 Bessel.C:47
 Bessel.C:48
 Bessel.C:49
 Bessel.C:50
 Bessel.C:51
 Bessel.C:52
 Bessel.C:53
 Bessel.C:54
 Bessel.C:55
 Bessel.C:56
 Bessel.C:57
 Bessel.C:58
 Bessel.C:59
 Bessel.C:60
 Bessel.C:61
 Bessel.C:62
 Bessel.C:63
 Bessel.C:64
 Bessel.C:65
 Bessel.C:66
 Bessel.C:67
 Bessel.C:68
 Bessel.C:69
 Bessel.C:70
 Bessel.C:71
 Bessel.C:72
 Bessel.C:73
 Bessel.C:74
 Bessel.C:75
 Bessel.C:76
 Bessel.C:77
 Bessel.C:78
 Bessel.C:79
 Bessel.C:80
 Bessel.C:81
 Bessel.C:82
 Bessel.C:83
 Bessel.C:84
 Bessel.C:85
 Bessel.C:86
 Bessel.C:87
 Bessel.C:88
 Bessel.C:89
 Bessel.C:90
 Bessel.C:91
 Bessel.C:92
 Bessel.C:93
 Bessel.C:94
 Bessel.C:95
 Bessel.C:96
 Bessel.C:97
 Bessel.C:98
 Bessel.C:99
 Bessel.C:100
 Bessel.C:101
 Bessel.C:102
 Bessel.C:103
 Bessel.C:104
 Bessel.C:105
 Bessel.C:106
 Bessel.C:107
 Bessel.C:108
 Bessel.C:109
 Bessel.C:110
 Bessel.C:111
 Bessel.C:112
 Bessel.C:113
 Bessel.C:114
 Bessel.C:115
 Bessel.C:116
 Bessel.C:117
 Bessel.C:118
 Bessel.C:119
 Bessel.C:120
 Bessel.C:121
 Bessel.C:122
 Bessel.C:123
 Bessel.C:124
 Bessel.C:125
 Bessel.C:126
 Bessel.C:127
 Bessel.C:128
 Bessel.C:129
 Bessel.C:130
 Bessel.C:131
 Bessel.C:132
 Bessel.C:133
 Bessel.C:134
 Bessel.C:135
 Bessel.C:136
 Bessel.C:137
 Bessel.C:138
 Bessel.C:139
 Bessel.C:140
 Bessel.C:141
 Bessel.C:142
 Bessel.C:143
 Bessel.C:144
 Bessel.C:145
 Bessel.C:146
 Bessel.C:147
 Bessel.C:148
 Bessel.C:149
 Bessel.C:150
 Bessel.C:151
 Bessel.C:152
 Bessel.C:153
 Bessel.C:154
 Bessel.C:155
 Bessel.C:156
 Bessel.C:157
 Bessel.C:158
 Bessel.C:159
 Bessel.C:160
 Bessel.C:161
 Bessel.C:162
 Bessel.C:163
 Bessel.C:164
 Bessel.C:165
 Bessel.C:166
 Bessel.C:167
 Bessel.C:168
 Bessel.C:169
 Bessel.C:170
 Bessel.C:171
 Bessel.C:172
 Bessel.C:173
 Bessel.C:174
 Bessel.C:175
 Bessel.C:176
 Bessel.C:177
 Bessel.C:178
 Bessel.C:179
 Bessel.C:180
 Bessel.C:181
 Bessel.C:182
 Bessel.C:183
 Bessel.C:184
 Bessel.C:185
 Bessel.C:186
 Bessel.C:187
 Bessel.C:188
 Bessel.C:189
 Bessel.C:190
 Bessel.C:191
 Bessel.C:192
 Bessel.C:193
 Bessel.C:194
 Bessel.C:195
 Bessel.C:196
 Bessel.C:197
 Bessel.C:198
 Bessel.C:199
 Bessel.C:200
thumb