Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.10 KB | None | 0 0
  1. private void button7_Click(object sender, EventArgs e)
  2.         {
  3.             ClearForm(); // išvalomi programos duomenys
  4.  
  5.             // Automobilio duomenys
  6.             PreparareForm(-100, 800, -400, 300);
  7.             double[] x = { 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, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671 };
  8.             double[] y = { -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -202, -200, -200, -200, -149, -148, -148, -146, -146, -146, -146, -146, -146, -144, -144, -143, -102, -100, -96, -95, -93, -93, -92, -92, -90, -90, -90, -90, -88, -88, -87, -87, -86, -85, -85, -85, -85, -85, -83, -83, -83, -83, -81, -81, -80, -80, -78, -78, -78, -78, -78, -78, -75, -75, -75, -75, -75, -75, -73, -73, -73, -73, -71, -71, -70, -70, -68, -68, -68, -68, -68, -68, -66, -66, -66, -66, -65, -65, -63, -63, -63, -63, -61, -61, -61, -61, -61, -27, -25, -19, -19, -17, -17, -15, -15, -14, -14, -13, -12, -12, -12, -12, -12, -12, -13, -14, -14, -14, -14, -14, -14, -14, -14, -15, -15, -15, -17, -17, -17, -17, -17, -17, -18, -19, -19, -19, -19, -19, -19, -20, -22, -22, -22, -24, -24, -24, -24, -27, -27, -27, -27, -27, -27, -27, -29, -29, -29, -29, -29, -30, -30, -31, -31, -32, -32, -32, -32, -32, -31, -31, -31, -31, -30, -29, -29, -29, -29, -29, -29, -29, -27, -27, -27, -27, -27, -27, -27, -27, -26, -25, -25, -24, -24, -24, -24, -24, -24, -22, -22, -22, -22, -22, -22, -22, -20, -20, -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -15, -15, -15, -15, -15, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -13, -13, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, -13, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, -15, -15, -15, -15, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, -19, -19, -19, -19, -19, -19, -19, -19, -20, -20, -22, -22, -22, -22, -24, -24, -24, -24, -24, -24, -26, -26, -27, -27, -27, -27, -27, -27, -29, -29, -29, -29, -30, -31, -31, -32, -32, -32, -34, -34, -34, -34, -36, -36, -36, -37, -39, -39, -39, -39, -41, -41, -41, -42, -44, -44, -44, -46, -46, -48, -48, -49, -49, -51, -51, -52, -52, -54, -54, -56, -56, -58, -58, -59, -59, -61, -61, -63, -63, -63, -64, -66, -66, -68, -68, -68, -68, -70, -71, -73, -73, -73, -73, -75, -75, -76, -76, -78, -78, -80, -80, -81, -82, -83, -83, -83, -85, -85, -85, -87, -87, -87, -88, -88, -90, -90, -90, -90, -90, -90, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -92, -93, -93, -93, -93, -93, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -95, -96, -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, -97, -98, -98, -98, -99, -99, -99, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -102, -102, -102, -102, -102, -102, -102, -102, -102, -102, -103, -103, -104, -104, -104, -104, -104, -104, -105, -105, -105, -105, -105, -105, -105, -105, -105, -105, -105, -107, -107, -107, -107, -107, -107, -107, -107, -107, -107, -107, -107, -108, -109, -109, -109, -109, -109, -109, -109, -109, -109, -109, -109, -109, -110, -110, -110, -110, -110, -112, -112, -112, -112, -112, -112, -112, -112, -114, -114, -114, -114, -114, -114, -117, -117, -117, -117, -117, -117, -117, -117, -119, -119, -119, -119, -122, -122, -122, -122, -124, -124, -124, -124, -126, -126, -127, -127, -129, -129, -131, -131, -132, -134, -134, -134, -136, -136, -139, -139, -141, -141, -144, -144, -147, -148, -151, -151, -156, -156, -160, -161, -168, -168, -175, -176, -185, -187, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293, -293 };
  9.        
  10.             Yx = chart1.Series.Add("Taškai");
  11.             Yx.ChartType = SeriesChartType.Point;
  12.  
  13.             for (int i = 0; i < x.Length; i++)
  14.             {
  15.                 Yx.Points.AddXY(x[i], y[i]);
  16.                 //richTextBox1.AppendText(String.Format("X: {0,4:f4}   Y: {1,4:f4}\n", x[i], y[i]));
  17.             }
  18.             richTextBox1.AppendText(String.Format("\n"));
  19.             Yx.Color = Color.Red;
  20.             Yx.BorderWidth = 3;
  21.  
  22.             int np = x.Length; // kiekis
  23.  
  24.             Vector<double> xVector = Vector<Double>.Build.DenseOfArray(x);
  25.             Vector<double> yVector = Vector<Double>.Build.DenseOfArray(y);
  26.  
  27.             double[] tempData = { 0, 0 };
  28.             Vector<double> tempVector = Vector<Double>.Build.DenseOfArray(tempData);
  29.  
  30.             double[] t = new double[np];
  31.             t[0] = 0;
  32.             for (int i = 1; i < np; i++)
  33.             {
  34.                 tempVector[0] = xVector[i] - xVector[i - 1];
  35.                 tempVector[1] = yVector[i] - yVector[i - 1];
  36.                 t[i] = t[i - 1] + norm(tempVector);            
  37.             }
  38.  
  39.             Vector<double> tVector = Vector<Double>.Build.DenseOfArray(t);
  40.            
  41.             // T vaizdavimas
  42.             //for (int i = 0; i < np; i++)
  43.             //{
  44.             //    richTextBox1.AppendText(String.Format("T: {0,4:f4} \n", t[i]));
  45.             //}
  46.  
  47.             splainu_parametrinis_interpoliavimas(xVector, yVector, tVector);
  48.         }
  49.  
  50.         private void splainu_parametrinis_interpoliavimas(Vector<double> X, Vector<double> Y, Vector<double> t)
  51.         {
  52.             int np = X.Count;
  53.  
  54.             richTextBox1.AppendText(String.Format("NP: {0,4:f4} \n", np ));
  55.  
  56.             Matrix<double> DDFX = splaino_koeficientai(t, X, 0);
  57.             Matrix<double> DDFY = splaino_koeficientai(t, Y, 0);
  58.  
  59.             //richTextBox1.AppendText(String.Format("DDFX: {0,4:f4} \n", DDFX.ToString()));
  60.             //richTextBox1.AppendText(String.Format("DDFY: {0,4:f4} \n", DDFY.ToString()));
  61.  
  62.             int nnn;
  63.  
  64.             double[] tempData = { 0, 0 };
  65.             Vector<double> tTemp = Vector<Double>.Build.DenseOfArray(tempData);
  66.  
  67.             Vector<double> xTemp = Vector<Double>.Build.DenseOfArray(tempData);
  68.             Vector<double> yTemp = Vector<Double>.Build.DenseOfArray(tempData);
  69.  
  70.             Vector<double> ddfxTemp = Vector<Double>.Build.DenseOfArray(tempData);
  71.             Vector<double> ddfyTemp = Vector<Double>.Build.DenseOfArray(tempData);
  72.  
  73.             Vector<double> SX;
  74.             Vector<double> SY;
  75.  
  76.             Fx = chart1.Series.Add("Splainai");
  77.             Fx.ChartType = SeriesChartType.Line;
  78.             Fx.BorderWidth = 3;
  79.  
  80.             for (int iii = 0; iii < np - 1; iii++)
  81.             {
  82.                 nnn = 6;
  83.  
  84.                 tTemp[0] = t[iii];
  85.                 tTemp[1] = t[iii + 1];
  86.  
  87.                 xTemp[0] = X[iii];
  88.                 xTemp[1] = X[iii + 1];
  89.  
  90.                 yTemp[0] = Y[iii];
  91.                 yTemp[1] = Y[iii + 1];
  92.  
  93.                 ddfxTemp[0] = DDFX[iii, 0];
  94.                 ddfxTemp[1] = DDFX[iii + 1, 0];
  95.  
  96.                 ddfyTemp[0] = DDFY[iii, 0];
  97.                 ddfyTemp[1] = DDFY[iii + 1, 0];
  98.  
  99.                 SX = splainas(tTemp, xTemp, ddfxTemp, nnn);
  100.                 SY = splainas(tTemp, yTemp, ddfyTemp, nnn);
  101.  
  102.                 //richTextBox1.AppendText(String.Format("SX: {0,4:f4} \n", SX.ToString()));
  103.                 //richTextBox1.AppendText(String.Format("SY: {0,4:f4} \n", SY.ToString()));
  104.  
  105.                 for (int i = 0; i < nnn; i++)
  106.                 {
  107.                     Fx.Points.AddXY(SX[i], SY[i]);
  108.                     //richTextBox1.AppendText(String.Format("SX: {0,4:f4} \n", SX[i].ToString()));
  109.                     //richTextBox1.AppendText(String.Format("SY: {0,4:f4} \n", SY[i].ToString()));
  110.                 }
  111.             }      
  112.         }
  113.  
  114.         private Vector<double> splainas(Vector<double> X, Vector<double> Y, Vector<double> DDF, int nnn)
  115.         {
  116.             double d = X[1] - X[0];
  117.  
  118.             Vector<double> sss = Vector<double>.Build.Dense(nnn, 1.0);
  119.  
  120.             for (int i = 1; i < nnn - 1; i++)
  121.             {
  122.                 sss[0] = X[0];
  123.                 sss[nnn - 1] = X[1];
  124.                 sss[i] = sss[i - 1] + ((X[1] - X[0]) / (nnn - 1));
  125.             }
  126.  
  127.             Vector<double> S = Vector<double>.Build.Dense(nnn, 1.0);
  128.  
  129.             for (int i = 0; i < nnn; i++)
  130.             {
  131.                 S[i] = DDF[0] / 2 * Math.Pow((sss[i] - X[0]), 2) + (DDF[1] - DDF[0]) / (6 * d) * Math.Pow((sss[i] - X[0]), 3) + (sss[i] - X[0]) * ((Y[1] - Y[0]) / d - DDF[0] * d / 3 - DDF[1] * d / 6) + Y[0];
  132.             }
  133.  
  134.             return S;
  135.         }
  136.  
  137.         private Matrix<double> splaino_koeficientai(Vector<double> X, Vector<double> Y, int iper)
  138.         {
  139.             int n = X.Count;
  140.  
  141.             Matrix<double> A = Matrix<double>.Build.Dense(n, n, 0.0);
  142.             Matrix<double> b = Matrix<double>.Build.Dense(n, 1, 0.0);
  143.  
  144.             Vector<double> d = Vector<double>.Build.Dense(n - 1, 0.0);
  145.  
  146.             for (int j = 0; j < n - 1; j++)
  147.             {
  148.                d[j] = X[j+1] - X[j];
  149.             }
  150.  
  151.             for (int i = 0; i < n - 2; i++)
  152.             {
  153.                 A[i, i] = d[i] / 6;
  154.                 A[i, i + 1 ] = (d[i] + d[i+1]) / 3;
  155.                 A[i, i + 2 ] = d[i+1] / 6;
  156.  
  157.                 b[i, 0] = (Y[i+2]-Y[i+1]) / d[i+1] - (Y[i+1]-Y[i]) / d[i];
  158.             }
  159.  
  160.             //richTextBox1.AppendText(String.Format("A: {0,4:f4} \n", A.ToString()));
  161.             //richTextBox1.AppendText(String.Format("b: {0,4:f4} \n", b.ToString()));
  162.  
  163.             if (iper == 0) {
  164.                 A[n - 2, 0] = 1.0;
  165.                 A[n - 1, n - 1] = 1.0;
  166.             }
  167.  
  168.            
  169.             A = A.Inverse().Multiply(b);
  170.             //richTextBox1.AppendText(String.Format("A: {0,4:f4} \n", A.ToString()));
  171.             return A;
  172.         }
  173.  
  174.         // normalizacija Pirmai netiesinei lygčių sistemai
  175.         private double norm(Vector<double> x)
  176.         {
  177.             double rez = 0;
  178.             for (int i = 0; i < 2; i++)
  179.             {
  180.                 rez += x.ElementAt(i) * x.ElementAt(i);
  181.             }
  182.             return Math.Sqrt(rez);
  183.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement