ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Interpolation.C
Go to the documentation of this file.
1 //More Information for R interpolation in
2 //http://stat.ethz.ch/R-manual/R-patched/library/stats/html/approxfun.html
3 //Author: Omar Zapata
4 //NOTE: this example illustrates an interpolation with random points given from ROOT
5 //and procedures made in R's environment.
6 
7 #include<TRInterface.h>
8 #include<TRandom.h>
9 #include<vector>
10 
12 {
14 //Creating points
15  TRandom rg;
16  std::vector<Double_t> x(10),y(10);
17  for(int i=0;i<10;i++)
18  {
19  x[i]=i;
20  y[i]=rg.Gaus();
21  }
22 
23  r["x"]=x;
24  r["y"]=y;
25 
26 
27 // do plotting only in non-batch mode
28  if (!gROOT->IsBatch() ) {
29 
30  r<<"dev.new()";//Required to activate new window for plot
31 //Plot parameter. Plotting using two rows and one column
32  r<<"par(mfrow = c(2,1))";
33 
34 //plotting the points
35  r<<"plot(x, y, main = 'approx(.) and approxfun(.)')";
36 
37 //The function "approx" returns a list with components x and y
38 //containing n coordinates which interpolate the given data points according to the method (and rule) desired.
39  r<<"points(approx(x, y), col = 2, pch = '*')";
40  r<<"points(approx(x, y, method = 'constant'), col = 4, pch = '*')";
41  }
42  else {
43  r << "print('Interpolated points')";
44  r << "print(approx(x,y,n=20))";
45  }
46 
47 //The function "approxfun" returns a function performing (linear or constant)
48 //interpolation of the given data.
49 //For a given set of x values, this function will return the corresponding interpolated values.
50  r<<"f <- approxfun(x, y)";
51 //using approxfun with const method
52  r<<"fc <- approxfun(x, y, method = 'const')";
53 
54  if (!gROOT->IsBatch() ) {
55  r<<"curve(f(x), 0, 11, col = 'green2')";
56  r<<"points(x, y)";
57 
58  r<<"curve(fc(x), 0, 10, col = 'darkblue', add = TRUE)";
59 // different interpolation on left and right side :
60  r<<"plot(approxfun(x, y, rule = 2:1), 0, 11,col = 'tomato', add = TRUE, lty = 3, lwd = 2)";
61  r<<"dev.off()";//Required to close new window for plot
62  }
63  else {
64  r << "x2=x+0.5";
65  r << "print('Result of approxfun with default method')";
66  r << "print(paste('x = ',x,' f(x) = ',f(x2)))";
67  r << "print('Result of approxfun with const method')";
68  r << "print(paste('x = ',x,' f(x) = ',fc(x2)))";
69  }
70 }
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
#define gROOT
Definition: TROOT.h:344
Double_t x[n]
Definition: legend1.C:17
void Interpolation()
Definition: Interpolation.C:11
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
ROOT::R::TRInterface & r
Definition: Object.C:4
Double_t y[n]
Definition: legend1.C:17
static TRInterface & Instance()
static method to get an TRInterface instance reference