This repository has been archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubicS.cpp
143 lines (123 loc) · 2.92 KB
/
CubicS.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
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
#include "CubicS.h"
CubicS::CubicS()
{
}
CubicS::CubicS(double* x, double* y, int64_t n)
:x(x), y(y), n(n)
{
A = new double[n - 2];
B = new double[n - 2];
C = new double[n - 2];
D = new double[n - 2];
E = new double[n - 2];
H = new double[n - 1];
M = new double[n];
}
CubicS::~CubicS()
{
delete[] A;
delete[] B;
delete[] C;
delete[] D;
delete[] E;
delete[] H;
delete[] M;
}
void CubicS::caculate(double* a, double* b, double* c, double* d)
{
if (
a == nullptr ||
b == nullptr ||
c == nullptr ||
d == nullptr
){
return;
}
if (
this->x == nullptr ||
this->y == nullptr ||
this->n < 2
) {
return;
}
this->a = a;
this->b = b;
this->c = c;
this->d = d;
if (this->n == 2) {
this->a[0] = 0;
this->b[0] = 0;
this->c[0] = (this->y[1] - this->y[0]) / (this->x[1] - this->x[0]);
this->d[0] = this->y[0] - this->x[0] * this->c[0];
return;
}//只有两点,无需三次插值
for (int64_t i = 0; i < this->n - 1; i++) {
this->H[i] = this->x[i + 1] - this->x[i];
}//h[i]赋值
for (int64_t i = 0; i < this->n - 2; i++) {
this->A[i] = this->H[i];
this->B[i] = 2 * (this->H[i] + this->H[i + 1]);
this->C[i] = this->H[i + 1];
}//自然边界下左侧矩阵前后有常数,舍去
for (int64_t i = 0; i < this->n - 2; i++) {
this->D[i] = 6 * ((this->y[i + 2] - this->y[i + 1]) / this->H[i + 1] - (this->y[i + 1] - this->y[i]) / this->H[i]);
}//自然边界下右侧矩阵前后有常数,舍去
this->TDMA(this->n - 2);//矩阵求解
if (this->n == 3) {
this->E[0] = (6 * ((this->y[2] - this->y[1]) / this->H[1] - (this->y[1] - this->y[0]) / this->H[0])) / (2 * (this->H[0] + this->H[1]));
}//只有三点时直接求解
this->M[0] = 0;
this->M[n - 1] = 0;
for (int64_t i = 1; i < n - 1; i++) {
this->M[i] = this->E[i - 1];
}//二阶导数
for (int64_t i = 0; i < n - 1; i++) {
this->a[i] = (this->M[i + 1] - this->M[i]) / (6 * this->H[i]);
this->b[i] = this->M[i] / 2;
this->c[i] = (this->y[i + 1] - this->y[i]) / this->H[i] - this->H[i] * this->M[i] / 2 - this->H[i] * (this->M[i + 1] - this->M[i]) / 6;
this->d[i] = this->y[i];
}//求解系数
}
void CubicS::TDMA(int64_t n)
{
if (n < 1) {
return;
}
this->C[0] /= this->B[0];
this->D[0] /= this->B[0];
for (int64_t i = 1; i < n; i++) {
double temp = this->B[i] - this->A[i] * this->C[i - 1];
this->C[i] /= temp;
this->D[i] = (this->D[i] - this->A[i] * this->D[i - 1]) / temp;
}
this->E[n - 1] = this->D[n - 1];
for (int64_t i = n - 2; i >= 0; i--) {
this->E[i] = this->D[i] - this->C[i] * this->E[i + 1];
}
}
double CubicS::result(double t)
{
if (
a == nullptr ||
b == nullptr ||
c == nullptr ||
d == nullptr
) {
return 0;
}
if (
this->x == nullptr ||
this->y == nullptr ||
this->n < 2
) {
return 0;
}
for (int64_t i = 0; i < this->n - 1; i++) {
if (t >= this->x[i] && t < this->x[i + 1]) {
double delta = t - this->x[i];
double res = ((this->a[i] * delta + this->b[i]) * delta + this->c[i]) * delta + this->d[i];
return res;
}
}
return 0;
}