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