Demo: A 1-D elliptic problem
This very simple code illustrates the numerical solution of a 1-D elliptic problem using P1 finite element method using the library OFELI.
#include "OFELI.h" |
using namespace OFELI; |
int main(int argc, char *argv[]) { |
const double L = 1; int N = 10; |
banner(); |
if (argc > 1) N = atoi(argv[1]); |
Mesh ms(L,N); |
int NbN = N+1; |
try { |
TrMatrix<double> A(NbN); Vect<double> b(ms); |
b.set("16*pi*pi*sin(4*pi*x)"); |
double h = L/double(N); |
for (int i=2; i<NbN; i++) { A(i,i ) = 2./h; A(i,i+1) = -1./h; A(i,i-1) = -1./h; } b *= h; |
A(1,1) = 1.; A(1,2) = 0.; b(1) = 0; A(NbN,NbN) = 1.; A(NbN-1,NbN) = 0.; b(NbN) = 0; |
A.solve(b); |
Vect<double> sol(ms); sol.set("sin(4*pi*x)"); cout << "Max-Norm error = " << (b-sol).Norm(NORM_MAX) << endl; |
} CATCH_EXCEPTION |
return 0; } |