#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;
const int N = 5;
const double A = 1;
const double B = 3;
const double eps = pow(10, -6);
void run(double* Below, double* Diag1, double* Above, double* Right, int n) {
n--; // тк начинаем с х0 а не с х1
Above[0] /= Diag1[0];
Right[0] /= Diag1[0];
for (int i = 1; i < n; i++) {
Above[i] /= Diag1[i] - Below[i] * Above[i - 1];
Right[i] = (Right[i] - Below[i] * Right[i - 1]) / (Diag1[i] - Below[i] * Above[i - 1]);
}
Right[n] = (Right[n] - Below[n] * Right[n - 1]) / (Diag1[n] - Below[n] * Above[n - 1]);
for (int i = n; i-- > 0;) {
Right[i] -= Above[i] * Right[i + 1];
}
}
double spl(double* h, double* x, double* y, double* gamma, double var, int i) //значение сплайна в данной точке
{
double S;
S = y[i] * (x[i + 1] - var) / h[i + 1];
S += y[i + 1] * (var - x[i]) / h[i + 1];
S += gamma[i] * (pow(x[i + 1] - var, 3) - h[i + 1] * h[i + 1] * (x[i + 1] - var)) / (6 * h[i + 1]);
S += gamma[i + 1] * (pow(var - x[i], 3) - h[i + 1] * h[i + 1] * (var - x[i])) / (6 * h[i + 1]);
return S;
}
int main() {
int n = 4;
double a[4] = { 0, -1, -1, -1 };
double b[4] = { 4, 4, 4, 4 };
double c[4] = { -1, -1, -1, 0 };
double d[4] = { 5, 5, 10, 23 };
// results { 2, 3, 5, 7 }
run(a, b, c, d, n);
for (int i = 0; i < n; i++) {
cout << d[i] << endl;
}
cout << endl << "n= " << n << endl << "n is not changed hooray !!";
double x_in[N], y_in[N];
double h[N - 1];
double gamma[N];
gamma[0] = A;
gamma[N] = B;
//ввод данных интерполяции, вычисление шагов
for (int i = 0; i < N; i++)
{
cin >> x_in[i];
}
for (int i = 0; i < N - 1; i++)
{
h[i] = x_in[i+1] - x_in[i];
}
double Diag[N - 1], F[N-1], BelowDiag[N - 1], AboveDiag[N - 1];
for (int i = 0; i < N - 1; i++)
{
Diag[i] = 2 * (h[i] + h[i + 1]);
F[i] = 6 * ((y_in[i + 1] - y_in[i]) / h[i + 1] - (y_in[i] - y_in[i - 1]) / h[i]);
}
for (int i = 1; i < N - 1; i++)
{
BelowDiag[i] = h[i + 1];
}
BelowDiag[0] = 0;
for (int i = 0; i < N; i++)
{
AboveDiag[i] = h[i + 1];
}
AboveDiag[N] = 0;
//вывод
run(BelowDiag, Diag, AboveDiag, F, N - 1);
/*
for (int i = 1; i < N; i++)
{
cout << "h[" << i << "] " << h[i] << endl;
}
for (int i = 1; i < N; i++)
{
cout << "D[" << i << "] " << Diag[i] << endl;
cout << "F[" << i << "] " << F[i] << endl;
}
*/
system("pause");
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.