0 / 0 / 0
Регистрация: 24.09.2023
Сообщений: 6
|
|
1
|
Неправильное построение arctg в гнуплоте по точкам
20.12.2023, 22:58. Показов 5002. Ответов 0
Здравствуйте, у меня задание построить в gnuplot функцию арктангенса, используя сплайн интерполяцию. Проблема в том что все точки строятся правильно кроме точек в диапозоне от 0 до 1. Я не понимаю почему именно в этих точках проблема. Где я накосячил.
C++ | 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
| #include <iostream>
#include <cmath>
#include <fstream>
void SLAUSolution(int M, double** A1, double* b1) // 11 A1 = 31x31 B1 = 31
{
double** A = new double* [M];
for (int i = 0; i < M; ++i) {
A[i] = new double[M];
}
double* b = new double[M];
for (int i = 0; i < M; ++i) {
b[i] = b1[i];
for (int j = 0; j < M; ++j) {
A[i][j] = A1[i][j];
}
}
for (int i = 0; i < M - 1; i++) {
int newi = i;
for (int k = i + 1; k < M; ++k) {
if (fabs(A[k][i]) > fabs(A[newi][i])) {
newi = k;
}
}
for (int k = i; k < M; ++k) {
double t = A[newi][k];
A[newi][k] = A[i][k];
A[i][k] = t;
}
{
double t = b[i];
b[i] = b[newi];
b[newi] = t;
}
double a0 = A[i][i];
if (fabs(a0) < 1e-10) {
std::cout << "Matrix is degenerate" << std::endl;
return;
}
b[i] /= a0;
for (int j = i; j < M; j++) {
A[i][j] /= a0;
}
for (int k = i + 1; k < M; k++) {
double ak = A[k][i];
for (int j = i + 1; j < M; j++) { A[k][j] -= ak * A[i][j]; }
A[k][i] = 0;
b[k] -= ak * b[i];
}
}
b[M - 1] /= A[M - 1][M - 1];
A[M - 1][M - 1] = 1;
double* xi = new double[M];
for (int i = M - 1; i >= 0; --i) {
xi[i] = b[i];
for (int j = i + 1; j < M; ++j) {
xi[i] -= A[i][j] * xi[j];
}
}
std::ofstream OUT("slausolution.txt");
OUT << M << std::endl;
for (int i = 0; i < M; ++i) {
OUT << xi[i] << std::endl;
}
OUT.close();
// CHECK THE SOLUTION
double* d;
d = new double[M];
for (int i = 0; i < M; ++i) {
d[i] = -b1[i];
for (int j = 0; j < M; ++j) {
d[i] += A1[i][j] * xi[j];
}
std::cout << d[i] << std::endl;
}
return;
}
void CalculateSplineCoefficients(unsigned N, double* X, double* Y) {
unsigned N1 = 3 * N - 2; // размер 31
double** A = new double* [N1];
double* b = new double[N1]; //размер 31
for (unsigned i = 0; i < N1; i++) // зануляем массивы
{
A[i] = new double[N1];
for (unsigned j = 0; j < N1; j++)
{
A[i][j] = 0; // 31 на 31
}
b[i] = 0;
}
for (unsigned s = 0; s <= N - 2; s++)
{
b[s] = Y[s];
A[s][s] = (X[s + 1] - X[s]) * (X[s + 1] - X[s]) / 6;
A[s][2 * N - 1 + s] = 1;
}
for (unsigned s = N - 1; s <= 2 * N - 3; s++)
{
unsigned i = s + 2 - N;
b[s] = Y[i + 1];
A[s][i + 1] = (X[i + 1] - X[i]) * (X[i + 1] - X[i]) / 6;
A[s][N + i] = X[i + 1] - X[i];
A[s][2 * N - 1 + i] = 1;
}
for (unsigned s = 2 * N - 2; s <= 3 * N - 5; s++)
{
unsigned i = s + 3 - 2 * N;
b[s] = 0;
A[s][N + i] = 1;
A[s][N + i - 1] = -1;
A[s][i] = (X[i - 1] - X[i + 1]) / 2;
}
A[3 * N - 4][0] = 1;
A[3 * N - 4][1] = -1;
A[3 * N - 3][N - 2] = 1;
A[3 * N - 3][N - 1] = -1;
SLAUSolution(N1, A, b);
std::ifstream INP("slausolution.txt");
unsigned L;
INP >> L;
double* xi = new double[L];
for (unsigned i = 0; i < L; ++i) {
INP >> xi[i];
}
INP.close();
std::ofstream OUT("splineresult.txt");
unsigned K = 10;
for (unsigned i = 0; i < N - 1; ++i) {
double Mi = xi[i], Miplus = xi[i + 1], Ci = xi[N + i], di = xi[2 * N - 1 + i];
for (unsigned k = 0; k < K; ++k)
{
double zk = X[i] * (1 - k * 1 / K) + X[i + 1] * k * 1 / K; //координата х в файле а внизу у
double fzk = Mi * (X[i + 1] - zk) * (X[i + 1] - zk) * (X[i + 1] - zk) / 6 / (X[i + 1] - X[i]) + Miplus * (zk - X[i]) * (zk - X[i]) * (zk - X[i]) / 6 / (X[i + 1] - X[i]) + Ci * (zk - X[i]) + di;
OUT << zk << " " << fzk << std::endl;
}
}
OUT.close();
return;
}
void Test(const char* fname) {
std::ofstream OFOF(fname);
unsigned N = 11;
OFOF << N << std::endl;
for (double i = 0; i < N; i += 1) {
OFOF << i << " " << std::atan(i) << std::endl;
}
OFOF.close();
return;
}
int main()
{
unsigned N;
const char fname[] = "Krik";
Test(fname);
std::ifstream AFAF(fname);
AFAF >> N;
double* X = new double[N];
double* Y = new double[N];
for (unsigned i = 0; i < N; ++i)
{
AFAF >> X[i] >> Y[i];
}
CalculateSplineCoefficients(N, X, Y);
return 0;
double** A = new double* [2];
A[0] = new double[2];
A[1] = new double[2];
double* b = new double[2];
A[0][0] = 0;
A[0][1] = 1;
A[1][0] = 1;
A[1][1] = 0;
b[0] = 1;
b[1] = -1;
SLAUSolution(2, A, b);
return 0;
} |
|
0
|