Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void button2_Click(object sender, EventArgs e)
- {
- ClearForm(); // išvalomi programos duomenys
- Inter = chart1.Series.Add("Interpoliavimo kreivė");
- Inter.ChartType = SeriesChartType.Line;
- chart1.ChartAreas[0].AxisY.Maximum = 17;
- chart1.ChartAreas[0].AxisY.Minimum= -5;
- chart1.ChartAreas[0].AxisX.Maximum = 35;
- chart1.ChartAreas[0].AxisX.Minimum = 0;
- float[] X = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f, 26.0f, 27.0f, 28.0f, 29.0f, 30.0f, 31.0f, 32.0f, 33.0f, 34.0f };
- float[] Y = { 4.4f, 5.4f, 5.9f, 6.3f, 6.5f, 6.8f, 6.9f, 7.0f, 7.1f, 7.2f, 7.3f, 7.5f, 8.0f, 8.5f, 9.0f, 9.4f, 9.7f, 9.8f, 9.9f, 9.9f, 9.8f, 9.7f, 9.5f, 9.3f, 9.2f, 9.0f, 8.7f, 8.5f, 8.2f, 7.9f, 7.8f, 7.7f, 7.6f, 5.8f };
- float[] X22 = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f, 26.0f, 27.0f, 28.0f, 29.0f, 30.0f, 31.0f, 32.0f, 33.0f, 34.0f };
- float[] Y22 = { 2.7f, 1.5f, 1.4f, 1.5f, 0.9f, 0.8f, 0.8f, 1.2f, 2.3f, 1.2f, 1.4f, 1.4f, 1.4f, 1.4f, 1.4f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.5f, 1.4f, 2.8f, 1.6f, 1.2f, 1.1f, 1.2f, 1.8f, 2.4f, 2.1f, 2.4f, 2.9f };
- float[] X2 = new float[X22.Length];
- float[] Y2 = new float[Y22.Length];
- for (int i = 0; i < X22.Length; i++)
- {
- X2[i] = X22[X22.Length - i - 1];
- Y2[i] = Y22[X22.Length - i - 1];
- }
- int nP = X.Length; // interpoliavimo tašku skaičius
- float[] t = new float[nP];
- t[0] = 0;
- for (int i = 1; i < nP; i++)
- {
- Vector<float> temp = Vector<float>.Build.DenseOfArray(new float[] { X[i] - X[i - 1], Y[i] - Y[i - 1] });
- t[i] = t[i - 1] + (float)temp.L2Norm();
- }
- float[] DX = Akima(t, X);
- float[] DY = Akima(t, Y);
- Ermito_splainu_parametrinis_interpoliavimas(X, DX, Y, DY, t);
- nP = X2.Length; // interpoliavimo tašku skaičius
- float[] t2 = new float[nP];
- t2[0] = 0;
- for (int i = 1; i < nP; i++)
- {
- Vector<float> temp = Vector<float>.Build.DenseOfArray(new float[] { X2[i] - X2[i - 1], Y2[i] - Y2[i - 1] });
- t2[i] = t2[i - 1] + (float)temp.L2Norm();
- }
- DX = Akima(t2, X2);
- DY = Akima(t2, Y2);
- Ermito_splainu_parametrinis_interpoliavimas(X2, DX, Y2, DY, t2);
- }
- public void Ermito_splainu_parametrinis_interpoliavimas(float[] X, float[] DX, float[] Y, float[] DY, float[] t) {
- int nP = X.Length; // interpoliavimo tasku skaicius
- for (int iii = 0; iii < nP-1; iii++)
- {
- int nnn = 100;
- float[] ttt = new float[nnn+1];
- for (int i = 0; i <= nnn; i++)
- {
- ttt[i] = t[iii] + i * (t[iii + 1] - t[iii]) / nnn;
- }
- float[] fffX = new float[nnn + 1];
- float[] fffY = new float[nnn + 1];
- for (int j = 0; j <= 1; j++)
- {
- float[] temp = new float[2];
- temp[0] = t[iii];
- temp[1] = t[iii + 1];
- float [] U = Hermite(temp, j, ttt);
- float[] V = Hermite2(temp, j, ttt);
- for (int i = 0; i <= nnn; i++)
- {
- fffY[i] = fffY[i] + U[i] * Y[iii + j] + V[i] * DY[iii + j];
- fffX[i] = fffX[i] + U[i] * X[iii + j] + V[i] * DX[iii + j];
- }
- }
- for (int i = 0; i <= nnn; i++)
- {
- Inter.Points.AddXY(fffX[i], fffY[i]);
- }
- }
- }
- public float[] Akima(float[] X, float[] Y)
- {
- int n = X.Length;
- float[] DY = new float[n];
- for (int i=0;i< n; i++)
- {
- if (i == 0)
- {
- float xim1 = X[0];
- float xi = X[1];
- float xip1 = X[2];
- float yim1 = Y[0];
- float yi = Y[1];
- float yip1 = Y[2];
- DY[i] = (2 * xim1 - xi - xip1) / ((xim1 - xi) * (xim1 - xip1)) * yim1 + (2 * xim1 - xim1 - xip1) / ((xi - xim1) * (xi - xip1)) * yi + (2 * xim1 - xim1 - xi) / ((xip1 - xim1) * (xip1 - xi)) * yip1;
- }
- else if(i== n-1)
- {
- float xim1 = X[n - 3];
- float xi = X[n - 2];
- float xip1 = X[n-1];
- float yim1 = Y[n - 3];
- float yi = Y[n - 2];
- float yip1 = Y[n-1];
- DY[i] = (2 * xip1 - xi - xip1) / ((xim1 - xi) * (xim1 - xip1)) * yim1 + (2 * xip1 - xim1 - xip1) / ((xi - xim1) * (xi - xip1)) * yi + (2 * xip1 - xim1 - xi) / ((xip1 - xim1) * (xip1 - xi)) * yip1;
- }
- else
- {
- float xim1 = X[i - 1];
- float xi = X[i];
- float xip1 = X[i + 1];
- float yim1 = Y[i - 1];
- float yi = Y[i];
- float yip1 = Y[i + 1];
- DY[i] = (2 * xi - xi - xip1) / ((xim1 - xi) * (xim1 - xip1)) * yim1 + (2 * xi - xim1 - xip1) / ((xi - xim1) * (xi - xip1)) * yi + (2 * xi - xim1 - xi) / ((xip1 - xim1) * (xip1 - xi)) * yip1;
- }
- }
- return DY;
- }
- private float[] Hermite(float[] X, int j, float[] x) {
- float[] L = Lagranzo(X, j, x);
- float DL = D_Lagrange(X, j, X[j]);
- float[] U = new float[x.Length];
- float[] V = new float[x.Length];
- for (int i = 0; i < x.Length; i++) {
- U[i] = (float)((1 - 2 * DL * (x[i] - X[j])) * Math.Pow(L[i], 2));
- V[i] = (float)((x[i] - X[j]) * Math.Pow(L[i],2));
- }
- return U;
- }
- private float[] Hermite2(float[] X, int j, float[] x)
- {
- float[] L = Lagranzo(X, j, x);
- float DL = D_Lagrange(X, j, X[j]);
- float[] U = new float[x.Length];
- float[] V = new float[x.Length];
- for (int i = 0; i < x.Length; i++)
- {
- U[i] = (float)((1 - 2 * DL * (x[i] - X[j])) * Math.Pow(L[i], 2));
- V[i] = (float)((x[i] - X[j]) * Math.Pow(L[i], 2));
- }
- return V;
- }
- public float[] Lagranzo(float[] X, int j, float[] x)
- {
- int n = X.Length;
- float[] L = new float[x.Length];
- for (int tt = 0; tt < x.Length; tt++)
- L[tt] = 1;
- for (int k = 0; k < n; k++) {
- if (k != j)
- for (int tt = 0; tt < x.Length; tt++)
- L[tt] = L[tt] * (x[tt] - X[k]) / (X[j] - X[k]);
- }
- return L;
- }
- private float D_Lagrange(float[] X, int j, float x) {
- int n = X.Length;
- float DL = 0; // DL israskos skaitiklis
- for (int i = 0; i < n; i++)
- {
- if (i != j)
- {
- float Lds = 1;
- for (int k = 0; k < n; k++)
- {
- if (k != j && k != i)
- {
- Lds = Lds * (x - X[k]);
- }
- }
- DL = DL + Lds;
- }
- }
- float Ldv = 1; // DL israskos vardiklis
- for (int k = 0; k<n; k++)
- {
- if (k != j)
- {
- Ldv = Ldv * (X[j] - X[k]);
- }
- }
- DL = DL / Ldv;
- return DL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement