| 1 |
#include "headcpp.h" |
| 2 |
#include "math.h" |
| 3 |
|
| 4 |
#include "tbl.h" |
| 5 |
#include "matrice.h" |
| 6 |
#include "cheby.h" |
| 7 |
|
| 8 |
double f_left (double) { |
| 9 |
return 1. ; |
| 10 |
} |
| 11 |
|
| 12 |
double f_right (double) { |
| 13 |
return 0. ; |
| 14 |
} |
| 15 |
|
| 16 |
double f_source (double x) { |
| 17 |
if (x<0) |
| 18 |
return 1. ; |
| 19 |
else |
| 20 |
if (x>0) return 0. ; |
| 21 |
else |
| 22 |
return 0.5 ; |
| 23 |
} |
| 24 |
|
| 25 |
double f_solution (double x) { |
| 26 |
double ee = exp(1.) ; |
| 27 |
static double bmoins = -1./8./(1+ee*ee) - ee*ee/8./(1+ee*ee*ee*ee) ; |
| 28 |
static double bplus = ee*ee*ee*ee/8.*(ee*ee/(1+ee*ee*ee*ee)-1./(1+ee*ee)) ; |
| 29 |
|
| 30 |
if (x<0) |
| 31 |
return 0.25 -(ee*ee/4.+bmoins*ee*ee*ee*ee)*exp(2*x) |
| 32 |
+ bmoins*exp(-2*x) ; |
| 33 |
else |
| 34 |
return bplus*(exp(-2*x)-exp(2*x)/ee/ee/ee/ee) ; |
| 35 |
} |
| 36 |
|
| 37 |
//******************* |
| 38 |
// First derivative |
| 39 |
//******************* |
| 40 |
Tbl ope_der (const Tbl& so) { |
| 41 |
// Verification of size : |
| 42 |
assert (so.get_ndim() == 1) ; |
| 43 |
int size = so.get_dim(0) ; |
| 44 |
|
| 45 |
// The result is set to zero |
| 46 |
Tbl res (size) ; |
| 47 |
res.annule_hard() ; |
| 48 |
|
| 49 |
// the computation |
| 50 |
for (int n=0 ; n<size ; n++) |
| 51 |
for (int p=n+1 ; p<size ; p++) |
| 52 |
if ((p+n)%2 == 1) |
| 53 |
res.set(n) += p*so(p)*2 ; |
| 54 |
|
| 55 |
// Normalisation of the first coef : |
| 56 |
res.set(0) /= 2. ; |
| 57 |
|
| 58 |
return res ; |
| 59 |
} |
| 60 |
//******************* |
| 61 |
// Second derivative |
| 62 |
//******************* |
| 63 |
Tbl ope_der_sec (const Tbl& so) { |
| 64 |
// Verification of size : |
| 65 |
assert (so.get_ndim() == 1) ; |
| 66 |
int size = so.get_dim(0) ; |
| 67 |
|
| 68 |
// The result is set to zero |
| 69 |
Tbl res (size) ; |
| 70 |
res.annule_hard() ; |
| 71 |
|
| 72 |
// the computation |
| 73 |
for (int n=0 ; n<size ; n++) |
| 74 |
for (int p=n+2 ; p<size ; p++) |
| 75 |
if ((p+n)%2 == 0) |
| 76 |
res.set(n) += p*(p*p-n*n)*so(p) ; |
| 77 |
|
| 78 |
// Normalisation of the first coef : |
| 79 |
res.set(0) /= 2. ; |
| 80 |
|
| 81 |
return res ; |
| 82 |
} |
| 83 |
|
| 84 |
//************************************** |
| 85 |
// The operator (multi domain case) |
| 86 |
//************************************** |
| 87 |
|
| 88 |
Matrice multi_ope (int n) { |
| 89 |
|
| 90 |
// The result : |
| 91 |
Matrice res(n,n) ; |
| 92 |
res.set_etat_qcq() ; |
| 93 |
|
| 94 |
// Work arrays : |
| 95 |
Tbl so (n) ; |
| 96 |
Tbl dd_so (n) ; |
| 97 |
|
| 98 |
// Column by column : |
| 99 |
for (int col=0 ; col<n ; col++) { |
| 100 |
so.annule_hard() ; |
| 101 |
so.set(col) = 1 ; |
| 102 |
|
| 103 |
// The derivative |
| 104 |
dd_so = ope_der_sec(so) ; |
| 105 |
|
| 106 |
// Put in the matrix : |
| 107 |
for (int line=0 ; line<n ; line++) |
| 108 |
res.set(line, col) = -4*dd_so(line) + 4*so(line) ; |
| 109 |
} |
| 110 |
|
| 111 |
return res ; |
| 112 |
} |
| 113 |
|
| 114 |
int main () { |
| 115 |
|
| 116 |
int nr ; |
| 117 |
cout << "Please enter the number of coefficients :" << endl ; |
| 118 |
cin >> nr ; |
| 119 |
|
| 120 |
cout << "The operator matrix is " << endl ; |
| 121 |
Matrice operator_mat (multi_ope(nr)) ; |
| 122 |
cout << operator_mat << endl ; |
| 123 |
|
| 124 |
// The non degenerare operator : |
| 125 |
Matrice nondege (nr-2, nr-2) ; |
| 126 |
nondege.set_etat_qcq() ; |
| 127 |
for (int lig=0 ; lig<nr-2 ; lig++) |
| 128 |
for (int col=0 ; col<nr-2 ; col++) |
| 129 |
nondege.set(lig, col) = operator_mat(lig, col+2) ; |
| 130 |
nondege.set_lu() ; |
| 131 |
cout << "The inverted matrix is :" << endl ; |
| 132 |
cout << nondege << endl ; |
| 133 |
|
| 134 |
// Computation of the particular solution on the left : |
| 135 |
Tbl colocation(coloc_cheb(nr)) ; |
| 136 |
Tbl so_left (nr) ; |
| 137 |
so_left.set_etat_qcq() ; |
| 138 |
for (int i=0 ; i<nr ; i++) |
| 139 |
so_left.set(i) = f_left (0.5*(colocation(i)-1)) ; |
| 140 |
Tbl coef_so_left (coef_cheb(so_left)) ; |
| 141 |
|
| 142 |
cout << "Coefficients of the source on the left : " << endl ; |
| 143 |
cout << coef_so_left << endl ; |
| 144 |
|
| 145 |
Tbl auxi_so_left (nr-2) ; |
| 146 |
auxi_so_left.set_etat_qcq() ; |
| 147 |
for (int i=0 ; i<nr-2 ; i++) |
| 148 |
auxi_so_left.set(i) = coef_so_left(i) ; |
| 149 |
Tbl inv_left (nondege.inverse(auxi_so_left)) ; |
| 150 |
Tbl sp_left (nr) ; |
| 151 |
sp_left.set_etat_qcq() ; |
| 152 |
sp_left.set(0) = 0 ; |
| 153 |
sp_left.set(1) = 0 ; |
| 154 |
for (int i=0 ; i<nr-2 ; i++) |
| 155 |
sp_left.set(i+2) = inv_left(i) ; |
| 156 |
|
| 157 |
cout << "Particular solution on the left :" << endl ; |
| 158 |
cout << sp_left << endl ; |
| 159 |
|
| 160 |
// Computation of the particular solution on the right : |
| 161 |
Tbl so_right (nr) ; |
| 162 |
so_right.set_etat_qcq() ; |
| 163 |
for (int i=0 ; i<nr ; i++) |
| 164 |
so_right.set(i) = f_right (0.5*(colocation(i)+1)) ; |
| 165 |
Tbl coef_so_right (coef_cheb(so_right)) ; |
| 166 |
|
| 167 |
cout << "Coefficients of the source on the right : " << endl ; |
| 168 |
cout << coef_so_right << endl ; |
| 169 |
|
| 170 |
Tbl auxi_so_right (nr-2) ; |
| 171 |
auxi_so_right.set_etat_qcq() ; |
| 172 |
for (int i=0 ; i<nr-2 ; i++) |
| 173 |
auxi_so_right.set(i) = coef_so_right(i) ; |
| 174 |
Tbl inv_right (nondege.inverse(auxi_so_right)) ; |
| 175 |
Tbl sp_right (nr) ; |
| 176 |
sp_right.set_etat_qcq() ; |
| 177 |
sp_right.set(0) = 0 ; |
| 178 |
sp_right.set(1) = 0 ; |
| 179 |
for (int i=0 ; i<nr-2 ; i++) |
| 180 |
sp_right.set(i+2) = inv_right(i) ; |
| 181 |
|
| 182 |
cout << "Particular solution on the right :" << endl ; |
| 183 |
cout << sp_right << endl ; |
| 184 |
|
| 185 |
// Computation of the homogeneous solutions : |
| 186 |
Tbl val_sh_plus_left (nr) ; |
| 187 |
val_sh_plus_left.set_etat_qcq() ; |
| 188 |
for (int i=0 ; i<nr ; i++) |
| 189 |
val_sh_plus_left.set(i) = exp(colocation(i)-1) ; |
| 190 |
Tbl coef_sh_plus_left (coef_cheb(val_sh_plus_left)) ; |
| 191 |
|
| 192 |
Tbl val_sh_moins_left (nr) ; |
| 193 |
val_sh_moins_left.set_etat_qcq() ; |
| 194 |
for (int i=0 ; i<nr ; i++) |
| 195 |
val_sh_moins_left.set(i) = exp(-colocation(i)+1) ; |
| 196 |
Tbl coef_sh_moins_left (coef_cheb(val_sh_moins_left)) ; |
| 197 |
|
| 198 |
Tbl val_sh_plus_right (nr) ; |
| 199 |
val_sh_plus_right.set_etat_qcq() ; |
| 200 |
for (int i=0 ; i<nr ; i++) |
| 201 |
val_sh_plus_right.set(i) = exp(colocation(i)+1) ; |
| 202 |
Tbl coef_sh_plus_right (coef_cheb(val_sh_plus_right)) ; |
| 203 |
|
| 204 |
Tbl val_sh_moins_right (nr) ; |
| 205 |
val_sh_moins_right.set_etat_qcq() ; |
| 206 |
for (int i=0 ; i<nr ; i++) |
| 207 |
val_sh_moins_right.set(i) = exp(-colocation(i)-1) ; |
| 208 |
Tbl coef_sh_moins_right (coef_cheb(val_sh_moins_right)) ; |
| 209 |
|
| 210 |
cout << "Various homogeneous solutions : " << endl ; |
| 211 |
cout << coef_sh_plus_left << endl ; |
| 212 |
cout << coef_sh_moins_left << endl ; |
| 213 |
cout << coef_sh_plus_right << endl ; |
| 214 |
cout << coef_sh_moins_right << endl ; |
| 215 |
|
| 216 |
// Construction of the matching matrix : |
| 217 |
Matrice matching (4,4) ; |
| 218 |
matching.annule_hard() ; |
| 219 |
// Boundary condition on the left : |
| 220 |
matching.set(0,0) = exp(2.) ; |
| 221 |
matching.set(0,1) = exp(-2.) ; |
| 222 |
// matching of the solution accross the boundary : |
| 223 |
matching.set(1,0) = 1 ; |
| 224 |
matching.set(1,1) = 1 ; |
| 225 |
matching.set(1,2) = -1 ; |
| 226 |
matching.set(1,3) = -1 ; |
| 227 |
// matching of the first derivative : |
| 228 |
matching.set(2,0) = 1 ; |
| 229 |
matching.set(2,1) = -1 ; |
| 230 |
matching.set(2,2) = 1 ; |
| 231 |
matching.set(2,3) = -1 ; |
| 232 |
// boundary on the right : |
| 233 |
matching.set(3,2) = exp(2.) ; |
| 234 |
matching.set(3,3) = exp(-2.) ; |
| 235 |
|
| 236 |
matching.set_lu() ; |
| 237 |
cout << "Matching matrix :" << endl ; |
| 238 |
cout << matching << endl ; |
| 239 |
|
| 240 |
// Second member for the matching matrix : |
| 241 |
Tbl sec_member (4) ; |
| 242 |
sec_member.annule_hard() ; |
| 243 |
// Boundary on the left : |
| 244 |
for (int i=0 ; i<nr ; i++) |
| 245 |
sec_member.set(0) += (i%2==0) ? -sp_left(i) : sp_left(i) ; |
| 246 |
// Matching : |
| 247 |
for (int i=0 ; i<nr ; i++) |
| 248 |
sec_member.set(1) += (i%2==0) ? -sp_left(i)-sp_right(i) : -sp_left(i)+sp_right(i) ; |
| 249 |
// Matching of the derivative : |
| 250 |
for (int i=0 ; i<nr ; i++) |
| 251 |
sec_member.set(2) += (i%2==0) ? i*i*(sp_left(i)+sp_right(i)) : i*i*(sp_left(i)-sp_right(i)) ; |
| 252 |
//Boundary on the right : |
| 253 |
for (int i=0 ; i<nr ; i++) |
| 254 |
sec_member.set(3) -= sp_right(i) ; |
| 255 |
|
| 256 |
cout << "Second member of the matching system: " << endl ; |
| 257 |
cout << sec_member << endl ; |
| 258 |
|
| 259 |
Tbl coef_sh (matching.inverse(sec_member)) ; |
| 260 |
cout << "Coefficients of the homogeneous solutions :" << endl ; |
| 261 |
cout << coef_sh << endl ; |
| 262 |
|
| 263 |
// Coefficients of the result : |
| 264 |
Tbl res_left (sp_left + coef_sh(0) * coef_sh_moins_left + coef_sh(1) * coef_sh_plus_left) ; |
| 265 |
Tbl res_right (sp_right + coef_sh(2) * coef_sh_plus_right + coef_sh(3) * coef_sh_moins_right) ; |
| 266 |
|
| 267 |
cout << "Coefficients of the solution : " << endl ; |
| 268 |
cout << res_left << endl ; |
| 269 |
cout << res_right << endl ; |
| 270 |
|
| 271 |
// Output in a file for plotting purposes : |
| 272 |
char name_out[30] ; |
| 273 |
sprintf(name_out, "plot_multi_homogeneous_%i.dat", nr) ; |
| 274 |
ofstream fiche (name_out) ; |
| 275 |
|
| 276 |
int resolution = 200 ; |
| 277 |
double x=-1 ; |
| 278 |
double step = 2./resolution ; |
| 279 |
|
| 280 |
// We will also compute the maximum difference between the solution and its numerical value : |
| 281 |
double error_max = 0 ; |
| 282 |
|
| 283 |
double xi ; |
| 284 |
for (int i=0 ; i<resolution+1 ; i++) { |
| 285 |
|
| 286 |
// in which domain ? |
| 287 |
if (x<0) |
| 288 |
xi = 2*x+1 ; |
| 289 |
else |
| 290 |
xi = 2*x-1 ; |
| 291 |
|
| 292 |
// One computes the solution at the current point |
| 293 |
double val_func = 0 ; |
| 294 |
for (int j=0 ; j<nr ; j++) |
| 295 |
if (x<0) |
| 296 |
val_func += res_left(j)*cheby(j, xi) ; |
| 297 |
else |
| 298 |
val_func += res_right(j)*cheby(j, xi) ; |
| 299 |
fiche << x << " " << val_func << " " << f_solution(x) << endl ; |
| 300 |
double error = fabs(val_func-f_solution(x)) ; |
| 301 |
if (error > error_max) |
| 302 |
error_max = error ; |
| 303 |
|
| 304 |
x += step ; |
| 305 |
} |
| 306 |
|
| 307 |
cout << "Error max : " << endl ; |
| 308 |
cout << error_max << endl ; |
| 309 |
|
| 310 |
return 0 ; |
| 311 |
} |