-
Notifications
You must be signed in to change notification settings - Fork 0
/
aproksymator_na_bazie.c
238 lines (209 loc) · 6.13 KB
/
aproksymator_na_bazie.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "makespl.h"
#include "piv_ge_solver.h"
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
/* UWAGA: liczbę używanych f. bazowych można ustawić przez wartość
zmiennej środowiskowej APPROX_BASE_SIZE
*/
/*
* Funkcje bazowe: n - liczba funkcji a,b - granice przedzialu aproksymacji i
* - numer funkcji x - wspolrzedna dla ktorej obliczana jest wartosc funkcji
*/
double
fi(double a, double b, int n, int i, double x)
{
double h = (b - a) / (n - 1);
double h3 = h * h * h;
int hi [5] = {i - 2, i - 1, i, i + 1, i + 2};
double hx [5];
int j;
for (j = 0; j < 5; j++)
hx[j] = a + h * hi[j];
if ((x < hx[0]) || (x > hx[4]))
return 0;
else if (x >= hx[0] && x <= hx[1])
return 1 / h3 * (x - hx[0]) * (x - hx[0]) * (x - hx[0]);
else if (x > hx[1] && x <= hx[2])
return 1 / h3 * (h3 + 3 * h * h * (x - hx[1]) + 3 * h * (x - hx[1]) * (x - hx[1]) - 3 * (x - hx[1]) * (x - hx[1]) * (x - hx[1]));
else if (x > hx[2] && x <= hx[3])
return 1 / h3 * (h3 + 3 * h * h * (hx[3] - x) + 3 * h * (hx[3] - x) * (hx[3] - x) - 3 * (hx[3] - x) * (hx[3] - x) * (hx[3] - x));
else /* if (x > hx[3]) && (x <= hx[4]) */
return 1 / h3 * (hx[4] - x) * (hx[4] - x) * (hx[4] - x);
}
/* Pierwsza pochodna fi */
double
dfi(double a, double b, int n, int i, double x)
{
double h = (b - a) / (n - 1);
double h3 = h * h * h;
int hi [5] = {i - 2, i - 1, i, i + 1, i + 2};
double hx [5];
int j;
for (j = 0; j < 5; j++)
hx[j] = a + h * hi[j];
if ((x < hx[0]) || (x > hx[4]))
return 0;
else if (x >= hx[0] && x <= hx[1])
return 3 / h3 * (x - hx[0]) * (x - hx[0]);
else if (x > hx[1] && x <= hx[2])
return 1 / h3 * (3 * h * h + 6 * h * (x - hx[1]) - 9 * (x - hx[1]) * (x - hx[1]));
else if (x > hx[2] && x <= hx[3])
return 1 / h3 * (-3 * h * h - 6 * h * (hx[3] - x) + 9 * (hx[3] - x) * (hx[3] - x));
else /* if (x > hx[3]) && (x <= hx[4]) */
return -3 / h3 * (hx[4] - x) * (hx[4] - x);
}
/* Druga pochodna fi */
double
d2fi(double a, double b, int n, int i, double x)
{
double h = (b - a) / (n - 1);
double h3 = h * h * h;
int hi [5] = {i - 2, i - 1, i, i + 1, i + 2};
double hx [5];
int j;
for (j = 0; j < 5; j++)
hx[j] = a + h * hi[j];
if ((x < hx[0]) || (x > hx[4]))
return 0;
else if (x >= hx[0] && x <= hx[1])
return 6 / h3 * (x - hx[0]);
else if (x > hx[1] && x <= hx[2])
return 1 / h3 * (6 * h - 18 * (x - hx[1]));
else if (x > hx[2] && x <= hx[3])
return 1 / h3 * (6 * h -18 * (hx[3] - x));
else /* if (x > hx[3]) && (x <= hx[4]) */
return 6 / h3 * (hx[4] - x);
}
/* Trzecia pochodna fi */
double
d3fi(double a, double b, int n, int i, double x)
{
double h = (b - a) / (n - 1);
double h3 = h * h * h;
int hi [5] = {i - 2, i - 1, i, i + 1, i + 2};
double hx [5];
int j;
for (j = 0; j < 5; j++)
hx[j] = a + h * hi[j];
if ((x < hx[0]) || (x > hx[4]))
return 0;
else if (x >= hx[0] && x <= hx[1])
return 6 / h3;
else if (x > hx[1] && x <= hx[2])
return -18 / h3;
else if (x > hx[2] && x <= hx[3])
return 18 / h3;
else /* if (x > hx[3]) && (x <= hx[4]) */
return -6 / h3;
}
/* Pomocnicza f. do rysowania bazy */
double
xfi(double a, double b, int n, int i, FILE *out)
{
double h = (b - a) / (n - 1);
double h3 = h * h * h;
int hi [5] = {i - 2, i - 1, i, i + 1, i + 2};
double hx [5];
int j;
for (j = 0; j < 5; j++)
hx[j] = a + h * hi[j];
fprintf( out, "# nb=%d, i=%d: hi=[", n, i );
for( j= 0; j < 5; j++ )
fprintf( out, " %d", hi[j] );
fprintf( out, "] hx=[" );
for( j= 0; j < 5; j++ )
fprintf( out, " %g", hx[j] );
fprintf( out, "]\n" );
}
void
make_spl(points_t * pts, spline_t * spl)
{
matrix_t *eqs= NULL;
double *x = pts->x;
double *y = pts->y;
double a = x[0];
double b = x[pts->n - 1];
int i, j, k;
int nb = pts->n - 3 > 10 ? 10 : pts->n - 3;
char *nbEnv= getenv( "APPROX_BASE_SIZE" );
if( nbEnv != NULL && atoi( nbEnv ) > 0 )
nb = atoi( nbEnv );
eqs = make_matrix(nb, nb + 1);
#ifdef DEBUG
#define TESTBASE 500
{
FILE *tst = fopen("debug_base_plot.txt", "w");
double dx = (b - a) / (TESTBASE - 1);
for( j= 0; j < nb; j++ )
xfi( a, b, nb, j, tst );
for (i = 0; i < TESTBASE; i++) {
fprintf(tst, "%g", a + i * dx);
for (j = 0; j < nb; j++) {
fprintf(tst, " %g", fi (a, b, nb, j, a + i * dx));
fprintf(tst, " %g", dfi (a, b, nb, j, a + i * dx));
fprintf(tst, " %g", d2fi(a, b, nb, j, a + i * dx));
fprintf(tst, " %g", d3fi(a, b, nb, j, a + i * dx));
}
fprintf(tst, "\n");
}
fclose(tst);
}
#endif
for (j = 0; j < nb; j++) {
for (i = 0; i < nb; i++)
for (k = 0; k < pts->n; k++)
add_to_entry_matrix(eqs, j, i, fi(a, b, nb, i, x[k]) * fi(a, b, nb, j, x[k]));
for (k = 0; k < pts->n; k++)
add_to_entry_matrix(eqs, j, nb, y[k] * fi(a, b, nb, j, x[k]));
}
#ifdef DEBUG
write_matrix(eqs, stdout);
#endif
if (piv_ge_solver(eqs)) {
spl->n = 0;
return;
}
#ifdef DEBUG
write_matrix(eqs, stdout);
#endif
if (alloc_spl(spl, nb) == 0) {
for (i = 0; i < spl->n; i++) {
double xx = spl->x[i] = a + i*(b-a)/(spl->n-1);
xx+= 10.0*DBL_EPSILON; // zabezpieczenie przed ulokowaniem punktu w poprzednim przedziale
spl->f[i] = 0;
spl->f1[i] = 0;
spl->f2[i] = 0;
spl->f3[i] = 0;
for (k = 0; k < nb; k++) {
double ck = get_entry_matrix(eqs, k, nb);
spl->f[i] += ck * fi (a, b, nb, k, xx);
spl->f1[i] += ck * dfi (a, b, nb, k, xx);
spl->f2[i] += ck * d2fi(a, b, nb, k, xx);
spl->f3[i] += ck * d3fi(a, b, nb, k, xx);
}
}
}
#ifdef DEBUG
{
FILE *tst = fopen("debug_spline_plot.txt", "w");
double dx = (b - a) / (TESTBASE - 1);
for (i = 0; i < TESTBASE; i++) {
double yi= 0;
double dyi= 0;
double d2yi= 0;
double d3yi= 0;
double xi= a + i * dx;
for( k= 0; k < nb; k++ ) {
yi += get_entry_matrix(eqs, k, nb) * fi(a, b, nb, k, xi);
dyi += get_entry_matrix(eqs, k, nb) * dfi(a, b, nb, k, xi);
d2yi += get_entry_matrix(eqs, k, nb) * d2fi(a, b, nb, k, xi);
d3yi += get_entry_matrix(eqs, k, nb) * d3fi(a, b, nb, k, xi);
}
fprintf(tst, "%g %g %g %g %g\n", xi, yi, dyi, d2yi, d3yi );
}
fclose(tst);
}
#endif
free_matrix(eqs);
}