-
Notifications
You must be signed in to change notification settings - Fork 0
/
Differentiation_Matrix.cpp
108 lines (103 loc) · 2.3 KB
/
Differentiation_Matrix.cpp
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
#include <iostream>
#include "Differentiation_Matrix.hpp"
#include <cmath>
#include <vector>
#define PI acos(-1)
typedef double decimal;
typedef int integer;
//decimal DEG_to_RAD(decimal d) { return d * PI / 180.0; }
//decimal RAD_to_DEG(decimal r) { return r * 180.0 / PI; }
template<class decimal, class integer>
std::vector<decimal > compute_c
(
integer N
)
{
std::vector<decimal > c(N);
c[0] = c[N - 1] = 2;
for (integer i = 1 ; i < N - 1 ; i++) {
c[i] = 1;
}
return c;
}
template<class decimal, class integer>
std::vector<decimal > define_time_stamps
(
integer N
)
{
std::vector<decimal > T(N);
for (integer k = 0 ; k < N ; k++) {
T[k] = cos(((PI * k ) / (N - 1) ));
//t[k] = -(1.0) *t[k];
}
/*
decimal tf = 2.00;
for (integer i = 0; i < N; i++)
{
t[i] = (tf / 2.0) * (t[i] + 1);
//std::cout << "t[" << i << "]" << " : " << t[i] << std::endl;
}*/
return T;
}
template<class decimal, class integer>
std::vector<decimal > multiply_D_X
(
std::vector<std::vector<decimal > > D, // matrix D
std::vector<decimal > X, // vector X
integer N // length of vector X
)
{
std::vector<decimal > prod(N);
for ( integer i = 0; i < N; i++ )
{
prod[i] = 0.0; // <===== Needs initialising
for ( integer j = 0; j < N; j++ )
{
prod[i] = prod[i] + (D[i][j] * X[j]); // <===== Add terms to sum for ith element
}
}
//D.clear();
//X.clear();
return prod;
}
template<class decimal, class integer>
std::vector<std::vector<decimal > > formulate_differentiation_matrix
(
std::vector<decimal > c, //
std::vector<decimal > t, //
integer N // N = N_ + 1
)
{
std::vector<std::vector<decimal > > D (N , std::vector <decimal > (N));
for (integer k = 0 ; k < N; k++) {
for (integer j = 0 ; j < N ; j++)
{
if (j == k)
{
if (j == 0)
{
D[k][j] = ((2.0 * (N - 1) * (N - 1)) + 1) / 6.0;
} else if (j == (N - 1) )
{
D[k][j] = -1.0 * (((2.0 * (N - 1) * (N - 1)) + 1) / 6.0);
} else
{
D[k][j] = (-1.0 * t[k]) / (2.0 * (1 - (t[k] * t[k])));
}
} else if (j != k)
{
D[k][j] = (c[k] / c[j]) * ( (pow(-1, (j + k))) / (t[k] - t[j]) );
}
//D[j][k] = -D[j][k];
}
}
/*
for (integer k = 0 ; k < N; k++) {
for (integer j = 0 ; j < N ; j++)
{
D[k][j] = (-1.0)*(D[k][j]);
}
}*/
return D;
}