Repository navigation
Expand file tree
/
Copy pathdiffinie.cpp
More file actions
203 lines (135 loc) · 5.29 KB
/
Copy pathdiffinie.cpp
File metadata and controls
203 lines (135 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <vector>
#include <complex>
#include <cmath>
#include "linalg.hpp"
#include "diffinie.hpp"
using namespace std;
typedef vector<double> vecd;
typedef vector< complex<double> > veccd;
//CONSTRUCTEUR//
template <typename T>
FdDerivative<T>::FdDerivative(){}
template<typename T>
FdDerivative<T>::FdDerivative(double h, int n){
//On initialise les attributs
_stepSize = h;
_nbElements = n;
//On initialise la matrice
_d.resize(_nbElements);
for(int i = 0; i < _nbElements; i++){
_d[i].resize(_nbElements,0);
}
//Construction de la matrice
_d[0][0] = -3 / (2 * _stepSize) ; _d[0][1] = 4 / (2 * _stepSize) ; _d[0][2] = -1 / (2 * _stepSize) ;
_d[_nbElements-1][_nbElements-3] = 1 / (2 * _stepSize) ; _d[_nbElements-1][_nbElements-2] = -4 / (2 * _stepSize) ; _d[_nbElements-1][_nbElements-1] = 3 / (2 * _stepSize) ;
for(uint i = 1; i < _nbElements-1; i++ ){
_d[i][i-1] = -1 / (2 * _stepSize) ; _d[i][i+1] = 1 / (2 * _stepSize) ;
}
}
template <typename T>
CompactFdDerivative<T>::CompactFdDerivative(){}
template<typename T>
CompactFdDerivative<T>::CompactFdDerivative(double h, int n){
//On initialise les attributs
_stepSize = h;
_nbElements = n;
//On initialise les matrices
_a.resize(_nbElements);
_b.resize(_nbElements);
for(int i = 0; i < _nbElements; i++){
_a[i].resize(_nbElements,0);
_b[i].resize(_nbElements,0);
}
//Construction de la matrice
_a[0][0] = 1.; _a[0][1] = 2.;
_b[0][0] = -5 / (2. * _stepSize) ; _b[0][1] = 2 / _stepSize ; _b[0][2] = 1 / (2. * _stepSize) ;
_a[1][0] = 1/4. ; _a[1][1] = 1.; _a[1][2] = 1/4.;
_b[1][0] = -3 / (4. * _stepSize) ; _b[1][2] = 3 / (4. * _stepSize) ;
for(uint i=2; i < _nbElements-2; i++ ){
_a[i][i-1] = 1/3.; _a[i][i] = 1.; _a[i][i+1] = 1/3.;
_b[i][i-2] = -1 / (36. * _stepSize) ; _b[i][i-1] = -14 / (18. * _stepSize) ; _b[i][i+1] = 14 / (18. * _stepSize) ; _b[i][i+2] = 1 / (36. * _stepSize) ;
}
_a[_nbElements - 2][_nbElements - 3] = 1/4.; _a[_nbElements - 2][_nbElements - 2] = 1.; _a[_nbElements - 2][_nbElements - 1] = 1/4.;
_b[_nbElements - 2][_nbElements - 3] = -3 / (4. * _stepSize) ; _b[_nbElements - 2][_nbElements - 1] = 3 / (4. * _stepSize) ;
_a[_nbElements - 1][_nbElements - 2] = 2.; _a[_nbElements - 1][_nbElements - 1] = 1.;
_b[_nbElements - 1][_nbElements - 3] = -1 / (2. * _stepSize) ; _b[_nbElements - 1][_nbElements - 2] = -2/_stepSize ;
_b[_nbElements - 1][_nbElements - 1] = 5 / (2. * _stepSize) ;
}
//OPERATEUR() (DERIVE APPROCHER)//
template <typename T>
vector<T> FdDerivative<T>::operator()(vector<T> fs){
vector<T> result = matVec(_d, fs); //calcul du vecteur F'
return result;
}
template <typename T>
vector<T> CompactFdDerivative<T>::operator()(vector<T> fs){
vector<T> result(_nbElements); //creation un vecteur F'
vector<T> BVar = matVec(_b, fs); //creation un vecteur B de AF'=B où B=_b*F
vector<vector<T> > AVar = _a; //creation un vecteur A de AF'=B
LU(AVar); //decomposition LU de A
result = LU_solve(AVar,BVar); //resolution du systeme AF'= B
return result;
}
//OPERATEUR() (ERREUR)//
template <typename T>
vector<T> FdDerivative<T>::operator()(vector<T> fs, vector<T> (*f)(double,int,vector<T>), vector<T> dfs){
vector<T> error(_nbElements);
vector<T> exactdfs = f(_stepSize, _nbElements, fs); //calcul de la derivee exacte
for(int i = 0; i < _nbElements; i++){
error[i] = abs(dfs[i] - exactdfs[i]); //calcul de l'erreur
}
return error;
}
template <typename T>
vector<T> CompactFdDerivative<T>::operator()(vector<T> fs, vector<T> (*f)(double,int,vector<T>), vector<T> dfs){
vector<T> error(_nbElements);
vector<T> exactdfs = f(_stepSize, _nbElements, fs); //calcul de la derivee exacte
for(int i = 0; i < _nbElements; i++){
error[i] = abs(dfs[i] - exactdfs[i]); //calcul de l'erreur
}
return error;
}
//FONCTIONS//
vecd gunction(double h, int N){
vecd func_g(N);
double x = 0;
for(int i = 0; i < N; i++){
func_g[i] = exp(-pow(x,2)/0.2); //fonction g
x += h;
}
return func_g;
}
veccd hunction(double h, int N){
veccd func_h(N);
double x = 0;
for(int i = 0; i < N; i++){
func_h[i] = complex<double>( tanh( 10*sin(10*x) ) , tanh( 10*cos(10*x) ) ); //fonction h
x += h;
}
return func_h;
}
//DERIVEES EXACTES//
vecd exactDerivG (double h, int N, vecd gs){
vecd result(N);
double x = 0;
for(int i = 0; i < N; i++){
result[i] = (-x/0.1)*gs[i]; //derivee exacte de g
x += h;
}
return result;
}
veccd exactDerivH (double h, int N, veccd hs){
veccd result(N);
double x = 0;
for(int i = 0; i < N; i++){
result[i] = complex<double>(100 * cos(10*x) * (1 - pow( real(hs[i]),2 ) ) , - 100 * sin(10*x) * (1 - pow( imag(hs[i]),2 ) )); //derivee exacte de h
x += h;
}
return result;
}
//INSTANCIATION//
template class FdDerivative<double>;
template class FdDerivative< complex<double> >;
template class CompactFdDerivative<double>;
template class CompactFdDerivative< complex<double> >;