#include "stdafx.h"
#include <iostream>
#include "math.h"
using namespace std;
const int N = 6; //на 1 больше, чем надо!!
const double eps = pow(10, -6);
void Lets_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 Where_am_I(double* x, double dot)
{
for (int i = 1; i < N + 1; i++)
if ((dot > x[i - 1]) && (dot < x[i])) return i;
}
int main() {
//INPUT DATA INITIALIZATION
// Enter your x_in and y_in coordinates
double x_in[] = { 1, 5, 7, 13, 22, 23, 25 }, y_in[] = { 1, -14, 5, 0, 12, 7, 12 };
double h[N - 1];
// Enter the S"(A) and S"(B) values
double gamma[N];
gamma[0] = 12;
gamma[N] = 5;
for (int i = 1; i < N + 1; i++)
{
h[i] = x_in[i] - x_in[i - 1];
cout << "h[" << i << "] " << h[i] << endl;
}
cout << endl;
double Diag[N - 1], F[N - 1], BelowDiag[N - 1], AboveDiag[N - 1];
for (int i = 1; i < N; 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; i++)
{
cout << "F[" << i << "] " << F[i] << endl;
}
cout << endl;
for (int i = 1; i < N; i++)
{
cout << "D[" << i << "] " << Diag[i] << endl;
}
cout << endl;
for (int i = 2; i < N; i++)
{
BelowDiag[i] = h[i];
}
BelowDiag[1] = 0;
for (int i = 1; i < N - 1; i++)
{
AboveDiag[i] = h[i + 1];
}
AboveDiag[N - 1] = 0;
for (int i = 1; i < N; i++)
{
cout << "A[" << i << "] " << AboveDiag[i] << endl;
}
cout << endl;
for (int i = 1; i < N; i++)
{
cout << "B[" << i << "] " << BelowDiag[i] << endl;
}
Lets_Run(BelowDiag, Diag, AboveDiag, F, N - 1);
cout << endl;
cout << "RESULTS: " << endl;
for (int i = 1; i < N; i++)
{
gamma[i] = F[i];
}
for (int i = 0; i < N + 1; i++)
{
cout << "gamma[" << i << "] " << gamma[i] << endl;
}
cout << endl;
double var = 9;
int j;
j = Where_am_I(x_in, var);
cout << j;
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.