Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Bessel.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook
4/// Show the different kinds of Bessel functions available in ROOT
5/// To execute the macro type in:
6///
7/// ~~~{.cpp}
8/// root[0] .x Bessel.C
9/// ~~~
10///
11/// It will create one canvas with the representation
12/// of the cylindrical and spherical Bessel functions
13/// regular and modified
14///
15/// \macro_image
16/// \macro_code
17///
18/// \author Magdalena Slawinska
19
20#include "TMath.h"
21#include "TF1.h"
22#include "TCanvas.h"
23
24#include <Riostream.h>
25#include "TLegend.h"
26#include "TLegendEntry.h"
27
28#include "Math/IFunction.h"
29#include <cmath>
30#include "TSystem.h"
31#include "TAxis.h"
32#include "TPaveLabel.h"
33
34void Bessel()
35{
36 R__LOAD_LIBRARY(libMathMore);
37
38 TCanvas *DistCanvas = new TCanvas("DistCanvas", "Bessel functions example", 10, 10, 800, 600);
39 DistCanvas->SetFillColor(17);
40 DistCanvas->Divide(2, 2);
41 DistCanvas->cd(1);
42 gPad->SetGrid();
43 gPad->SetFrameFillColor(19);
44 TLegend *leg = new TLegend(0.75, 0.7, 0.89, 0.89);
45
46 int n = 5; //number of functions in each pad
47 //drawing the set of Bessel J functions
48 TF1* JBessel[5];
49 for(int nu = 0; nu < n; nu++)
50 {
51 JBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_j([0],x)", 0, 10);
52 JBessel[nu]->SetParameters(nu, 0.0);
53 JBessel[nu]->SetTitle(""); //Bessel J functions");
54 JBessel[nu]->SetLineStyle(1);
55 JBessel[nu]->SetLineWidth(3);
56 JBessel[nu]->SetLineColor(nu+1);
57 }
58 JBessel[0]->TF1::GetXaxis()->SetTitle("x");
59 JBessel[0]->GetXaxis()->SetTitleSize(0.06);
60 JBessel[0]->GetXaxis()->SetTitleOffset(.7);
61
62 //setting the title in a label style
63 TPaveLabel *p1 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) , "Bessel J functions", "NDC");
64 p1->SetFillColor(0);
65 p1->SetTextFont(22);
67
68 //setting the legend
69 leg->AddEntry(JBessel[0]->DrawCopy(), " J_0(x)", "l");
70 leg->AddEntry(JBessel[1]->DrawCopy("same"), " J_1(x)", "l");
71 leg->AddEntry(JBessel[2]->DrawCopy("same"), " J_2(x)", "l");
72 leg->AddEntry(JBessel[3]->DrawCopy("same"), " J_3(x)", "l");
73 leg->AddEntry(JBessel[4]->DrawCopy("same"), " J_4(x)", "l");
74
75 leg->Draw();
76 p1->Draw();
77
78 //------------------------------------------------
79 DistCanvas->cd(2);
80 gPad->SetGrid();
81 gPad->SetFrameFillColor(19);
82
83 TLegend *leg2 = new TLegend(0.75, 0.7, 0.89, 0.89);
84 //------------------------------------------------
85 //Drawing Bessel k
86 TF1* KBessel[5];
87 for(int nu = 0; nu < n; nu++){
88 KBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_k([0],x)", 0, 10);
89 KBessel[nu]->SetParameters(nu, 0.0);
90 KBessel[nu]->SetTitle("Bessel K functions");
91 KBessel[nu]->SetLineStyle(1);
92 KBessel[nu]->SetLineWidth(3);
93 KBessel[nu]->SetLineColor(nu+1);
94 }
95 KBessel[0]->GetXaxis()->SetTitle("x");
96 KBessel[0]->GetXaxis()->SetTitleSize(0.06);
97 KBessel[0]->GetXaxis()->SetTitleOffset(.7);
98
99 //setting title
100 TPaveLabel *p2 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) , "Bessel K functions", "NDC");
101 p2->SetFillColor(0);
102 p2->SetTextFont(22);
103 p2->SetTextColor(kBlack);
104
105 //setting legend
106 leg2->AddEntry(KBessel[0]->DrawCopy(), " K_0(x)", "l");
107 leg2->AddEntry(KBessel[1]->DrawCopy("same"), " K_1(x)", "l");
108 leg2->AddEntry(KBessel[2]->DrawCopy("same"), " K_2(x)", "l");
109 leg2->AddEntry(KBessel[3]->DrawCopy("same"), " K_3(x)", "l");
110 leg2->AddEntry(KBessel[4]->DrawCopy("same"), " K_4(x)", "l");
111 leg2->Draw();
112 p2->Draw();
113 //------------------------------------------------
114 DistCanvas->cd(3);
115 gPad->SetGrid();
116 gPad->SetFrameFillColor(19);
117 TLegend *leg3 = new TLegend(0.75, 0.7, 0.89, 0.89);
118 //------------------------------------------------
119 //Drawing Bessel i
120 TF1* iBessel[5];
121 for(int nu = 0; nu <= 4; nu++){
122 iBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_i([0],x)", 0, 10);
123 iBessel[nu]->SetParameters(nu, 0.0);
124 iBessel[nu]->SetTitle("Bessel I functions");
125 iBessel[nu]->SetLineStyle(1);
126 iBessel[nu]->SetLineWidth(3);
127 iBessel[nu]->SetLineColor(nu+1);
128 }
129
130 iBessel[0]->GetXaxis()->SetTitle("x");
131 iBessel[0]->GetXaxis()->SetTitleSize(0.06);
132 iBessel[0]->GetXaxis()->SetTitleOffset(.7);
133
134 //setting title
135 TPaveLabel *p3 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,"Bessel I functions", "NDC");
136 p3->SetFillColor(0);
137 p3->SetTextFont(22);
138 p3->SetTextColor(kBlack);
139
140 //setting legend
141 leg3->AddEntry(iBessel[0]->DrawCopy(), " I_0", "l");
142 leg3->AddEntry(iBessel[1]->DrawCopy("same"), " I_1(x)", "l");
143 leg3->AddEntry(iBessel[2]->DrawCopy("same"), " I_2(x)", "l");
144 leg3->AddEntry(iBessel[3]->DrawCopy("same"), " I_3(x)", "l");
145 leg3->AddEntry(iBessel[4]->DrawCopy("same"), " I_4(x)", "l");
146 leg3->Draw();
147 p3->Draw();
148 //------------------------------------------------
149 DistCanvas->cd(4);
150 gPad->SetGrid();
151 gPad->SetFrameFillColor(19);
152 TLegend *leg4 = new TLegend(0.75, 0.7, 0.89, 0.89);
153 //------------------------------------------------
154 //Drawing sph_bessel
155 TF1* jBessel[5];
156 for(int nu = 0; nu <= 4; nu++){
157 jBessel[nu]= new TF1("J_0", "ROOT::Math::sph_bessel([0],x)", 0, 10);
158 jBessel[nu]->SetParameters(nu, 0.0);
159 jBessel[nu]->SetTitle("Bessel j functions");
160 jBessel[nu]->SetLineStyle(1);
161 jBessel[nu]->SetLineWidth(3);
162 jBessel[nu]->SetLineColor(nu+1);
163 }
164 jBessel[0]->GetXaxis()->SetTitle("x");
165 jBessel[0]->GetXaxis()->SetTitleSize(0.06);
166 jBessel[0]->GetXaxis()->SetTitleOffset(.7);
167
168 //setting title
169 TPaveLabel *p4 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,"Bessel j functions", "NDC");
170 p4->SetFillColor(0);
171 p4->SetTextFont(22);
172 p4->SetTextColor(kBlack);
173
174 //setting legend
175
176 leg4->AddEntry(jBessel[0]->DrawCopy(), " j_0(x)", "l");
177 leg4->AddEntry(jBessel[1]->DrawCopy("same"), " j_1(x)", "l");
178 leg4->AddEntry(jBessel[2]->DrawCopy("same"), " j_2(x)", "l");
179 leg4->AddEntry(jBessel[3]->DrawCopy("same"), " j_3(x)", "l");
180 leg4->AddEntry(jBessel[4]->DrawCopy("same"), " j_4(x)", "l");
181
182 leg4->Draw();
183 p4->Draw();
184
185 DistCanvas->cd();
186}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:491
@ kBlack
Definition Rtypes.h:65
#define gPad
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:309
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:42
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
1-Dim function class
Definition TF1.h:214
void SetTitle(const char *title="") override
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
Definition TF1.cxx:3552
virtual void SetParameters(const Double_t *params)
Definition TF1.h:650
TAxis * GetXaxis() const
Get x axis of the function.
Definition TF1.cxx:2400
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:317
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:422
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1153
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
void Draw(Option_t *option="") override
Draw this pavelabel with its current attributes.
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)=0
const Int_t n
Definition legend1.C:16
leg
Definition legend1.C:34