Doxygen 1.15.0
Toolkit for Adaptive Stochastic Modeling and Non-Intrusive ApproximatioN: Tasmanian v8.2
Loading...
Searching...
No Matches
Tasmanian Sparse Grids module, example 1

Functions

void sparse_grids_example_01 ()
 Sparse Grids Example 1: integrate a simple function over a canonical domain.

Detailed Description

Example 1
Compute $ \int_{-1}^{+1} \int_{-1}^{+1} \exp(-x^2) \cos(y) dy dx $ using a sparse grid with Clenshaw-Curtis nodes and weights.

Function Documentation

◆ sparse_grids_example_01()

void sparse_grids_example_01 ( )

Sparse Grids Example 1: integrate a simple function over a canonical domain.

#endif
cout << "\n---------------------------------------------------------------------------------------------------\n";
cout << std::scientific; cout.precision(17);
cout << "Example 1: integrate f(x,y) = exp(-x^2) * cos(y),\n"
<< " using clenshaw-curtis nodes and grid of type level\n";
int dimension = 2;
int level = 6;
std::vector<double> points = grid.getPoints();
std::vector<double> weights = grid.getQuadratureWeights();
int num_points = grid.getNumPoints(); // also equal to weights.size()
double I = 0.0;
for(int i=0; i<num_points; i++){
double x = points[i*dimension];
double y = points[i*dimension+1];
I += weights[i] * std::exp(-x*x) * std::cos(y);
}
double exact = 2.513723354063905e+00;
double E = std::abs(exact - I);
cout << " at level: " << level
<< "\n the grid has: " << num_points
<< "\n integral: " << I
<< "\n error: " << E << "\n\n";
level = 7;
points = grid.getPoints();
weights = grid.getQuadratureWeights();
num_points = grid.getNumPoints();
I = 0.0;
for(int i=0; i<num_points; i++){
double x = points[i*dimension];
double y = points[i*dimension+1];
I += weights[i] * std::exp(-x*x) * std::cos(y);
}
E = std::abs(exact - I);
cout << " at level: " << level
<< "\n the grid has: " << num_points
<< "\n integral: " << I
<< "\n error: " << E << endl;
#ifndef __TASMANIAN_DOXYGEN_SKIP