-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1_LU.cpp
More file actions
71 lines (57 loc) · 1.95 KB
/
project1_LU.cpp
File metadata and controls
71 lines (57 loc) · 1.95 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
#include <iostream>
#include "armadillo"
#include <fstream>
#include <chrono>
#include <ratio>
#include <ctime>
#include <algorithm>
#include <cstdlib>
using namespace std;
using namespace arma;
inline double f(double x){return 100.0*exp(-10.0*x);}
inline double analytical(double x){return 1.0-(1-exp(-10.0))*x - exp(-10.0*x);}
int main(int argc, char* argv[])
{
int n = atoi(argv[1]); //input number of grid points
//Defining matrix A and vectors b_tilde and u
mat A(n,n);
vec b_tilde(n, 1);
vec u(n,1);
//Populating A
for(int i = 0; i < n; i++) A(i,i) = 2.0;
for(int i = 0; i < n-1; i++) A(i+1, i) = A(i, i+1) = -1.0;
double h = (double) 1/(n+1);
double hh = h*h;
//Analytical solution
for(int i = 0; i < n; i++) u(i) = analytical(h*(i+1));
//Filling b_tilde
for(int i = 0; i < n; i++) b_tilde(i) = hh*f(h*(i+1));
auto start = chrono::high_resolution_clock::now();
/*START algorithm*/
vec v = solve(A, b_tilde, solve_opts::fast);
/*STOP Algorithm */
auto stop = chrono::high_resolution_clock::now();
auto diff = stop-start;
cout << " Runtime of algorithm = " << chrono::duration <double,milli> (diff).count() << "ms" << endl;
A.reset();
ofstream outfile;
outfile.open("proj1data.dat");
if(*argv[2] == 'Y')
{
for(int i = 0; i < n; i++) outfile << (i+1)*h << " " << u(i) << " " << v(i) << endl;
}
else
{
//Alocating memory for storing relative error
double* eps_relative = new double[n];
for(int i = 0; i < n; i++) eps_relative[i] = log10( abs( ( v(i) - u(i) )/u(i) ) );
//Deallocating memory
v.reset();
u.reset();
//Fetching maximum value in array, then deallocating memory
outfile << *max_element(eps_relative, eps_relative + n) << endl;
delete []eps_relative;
}
outfile.close();
return 0;
}