![]() |
![]() |
This lesson concerns a simple one-dimensional two-point boundary value problem. The whole program can be found in Example 1 in the examples directory. We will examine it here below line by line.
#include "OFELI.h"
using namespace OFELI;
int main(int argc, char *argv[])
{
double Lmin=0, Lmax=1;
int N=10;
double f(double x);
double error(const Mesh &ms, const Vect<double> &u);
banner();
if (argc > 1)
N = atoi(argv[1]);
Mesh ms(Lmin,Lmax,N);
int NbN = N+1; ms.setVerbose(10); cout << ms;
TrMatrix<double> a(NbN); Vect<double> b(NbN);
double h = (Lmax-Lmin)/double(N);
for (int i=2; i<NbN; i++) {
double x = ms.getPtrNode(i)->getCoord(1);
a(i,i) = 2./h;
a(i,i+1) = -1./h;
a(i,i-1) = -1./h;
b(i) = f(x)*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);
cout << "\nSolution :\n" << b; cout << "Error = " << error(ms,b) << endl;
return 0; }
double f(double x)
{
double a = 10;
return 2*a*(1-2*a*x*x)*exp(-a*x*x);
}
double error(const Mesh &ms, const Vect<double> &u)
{
double err=0;
double a = 10;
for (int i=1; i<=u.getSize(); i++) {
double x = ms.getPtrNode(i)->getCoord(1);
err = max(err,fabs(u(i) - exp(-a*x*x) - x*(1-exp(-a)) + 1));
}
return err;
}
M E S H D A T A
===================
Space Dimension : 1
Number of nodes : 11
Number of elements : 10
Number of sides : 0
LIST OF ELEMENTS :
Element : 1
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 1 2
Element : 2
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 2 3
Element : 3
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 3 4
Element : 4
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 4 5
Element : 5
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 5 6
Element : 6
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 6 7
Element : 7
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 7 8
Element : 8
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 8 9
Element : 9
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 9 10
Element : 10
Shape : line ** Material : GenericMaterial ** Code : 1 ** Nodes : 10 11
LIST OF NODES :
Node : 1
Coordinates : 0.000000e+000 0.000000e+000 0.000000e+000
1 d.o.f. : 1
Codes : 0
Node : 2
Coordinates : 1.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 2
Codes : 0
Node : 3
Coordinates : 2.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 3
Codes : 0
Node : 4
Coordinates : 3.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 4
Codes : 0
Node : 5
Coordinates : 4.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 5
Codes : 0
Node : 6
Coordinates : 5.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 6
Codes : 0
Node : 7
Coordinates : 6.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 7
Codes : 0
Node : 8
Coordinates : 7.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 8
Codes : 0
Node : 9
Coordinates : 8.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 9
Codes : 0
Node : 10
Coordinates : 9.000000e-001 0.000000e+000 0.000000e+000
1 d.o.f. : 10
Codes : 0
Node : 11
Coordinates : 1.000000e+000 0.000000e+000 0.000000e+000
1 d.o.f. : 11
Codes : 0
Solution :
1 0.00000000e+000
2 1.71545445e-003
3 -1.41343078e-001
4 -3.11214412e-001
5 -4.16034601e-001
6 -4.32020322e-001
7 -3.82338044e-001
8 -2.98774350e-001
9 -2.02104670e-001
10 -1.01513714e-001
11 0.00000000e+000
Error = 1.7912959e-002
Removing Mesh instance ...
![]() |
![]() |