/* An example of a program solving -(ku')' + cu = f in the 1D interval [0,l] using linear finite elements. */ #include #include #include "math.h" //Include our own previously written code... // in general we will be using iterative solvers, // but for this particular example (since we're // assembling the full global matrix) we'll use // the Gaussian Elimination direct solver that // we have written for Math609. #include "gaussian.cc" int n = 25; double l = 10; double h = l/n; // Include a file with functions describing the linear // finite elements. If we wish to change our program to // work with quadratic elements, here's where we'll make // the changes. #include "1d_linear.cc" // Include a file with various quadratures (currently only // the Simpson's rule for the unit interval is implemented). #include "quadrature.cc" /************************* The Coefficients ************************/ double k(double x) { return 2; } double c(double x) { return 3; } double f(double x) { return -12*x+3*x*x*x; } // an expected solution (used to check our approximation) double sol(double x) { return x*x*x; } /************************* The Main Code ************************/ int main() { double** A = new double*[n+1]; double* b = new double[n+1]; Create_Quadratures(); Quadrature *quad = &SIMPSONS_RULE; //create the empty matrix and rhs for (int i=0; i<=n; i++) { A[i] = new double[n+1]; for (int k=0; k<=n; k++) A[i][k]=0; b[i] = 0; } // cycle over each of the elements // and fill in the local contributions int num_dofs = dofs_per_element(); int* element_dofs = new int[num_dofs]; for (int ele=0; elesize; l++) { // get the values of the point and the weight double q_point = map_to_FE(quad->points[l], ele); double q_weight = quad->weights[l]; for (int i=0; i