Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MathNet.Numerics.LinearAlgebra;
  11. using System.Windows.Forms.DataVisualization.Charting;
  12.  
  13. namespace SystemsOfLinearEquations
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. void Button1_Click(object sender, EventArgs e)
  23. {
  24. Close();
  25. }
  26.  
  27. public void ClearForm()
  28. {
  29. chart1.Series.Clear();
  30. richTextBox1.Clear();
  31. }
  32.  
  33. double F(double x)
  34. {
  35. return Math.Log(x) / (Math.Sin(2.0 * x) + 1.5) - Math.Cos(x / 5.0);
  36. }
  37.  
  38. double IF(double x, Vector<double> A)
  39. {
  40. double temp = 0;
  41. double differences = 1;
  42. double differences1 = x;
  43. double result = differences * A[0] + differences1 * A[1];
  44. for (int row = 2; row < A.Count; row++)
  45. {
  46. temp = 2 * x * differences1 - differences;
  47. result += temp * A[row];
  48. differences = differences1;
  49. differences1 = temp;
  50. }
  51. return result;
  52. }
  53.  
  54. void Chebyshev(double[] xValues)
  55. {
  56. Vector<double> X = Vector<double>.Build.DenseOfArray(xValues);
  57. Vector<double> A = Vector<double>.Build.Dense(X.Count);
  58. Vector<double> Y = Vector<double>.Build.Dense(X.Count);
  59. Matrix<double> N = Matrix<double>.Build.Dense(X.Count, X.Count, 0);
  60.  
  61. for (int row = 0; row < X.Count; row++)
  62. {
  63. // Y reikšmės apskaičiavimas
  64.  
  65. Y[row] = F(X[row]);
  66.  
  67. // N matricos eilutės apskaičiavimas
  68.  
  69. N[row, 0] = 1;
  70. N[row, 1] = X[row];
  71. for (int column = 2; column < X.Count; column++)
  72. {
  73. N[row, column] = 2 * X[row] * N[row, column - 1] - N[row, column - 2];
  74. }
  75. }
  76.  
  77. // A reikšmės apskaičiavimas
  78. // A = N.Inverse() * Y;
  79. Matrix<double> Nn = N.Inverse();
  80. for (int row = 0; row < X.Count; row++)
  81. {
  82. for (int column = 0; column < X.Count; column++) {
  83. A[row] += Nn[row, column] * Y[column];
  84. }
  85. }
  86.  
  87. Series Original = chart1.Series.Add("Original");
  88. Series Interpolated = chart1.Series.Add("Interpolated");
  89. Series Difference = chart1.Series.Add("Difference");
  90. Series Points = chart1.Series.Add("Points");
  91.  
  92. Original.ChartType = SeriesChartType.Line;
  93. Interpolated.ChartType = SeriesChartType.Line;
  94. Difference.ChartType = SeriesChartType.Line;
  95. Points.ChartType = SeriesChartType.Point;
  96.  
  97. Original.BorderWidth = 3;
  98. Interpolated.BorderWidth = 3;
  99. Difference.BorderWidth = 3;
  100.  
  101. for (int i = 0; i < X.Count; i++)
  102. {
  103. Points.Points.AddXY(X[i], F(X[i]));
  104. }
  105.  
  106. for (double x = 2; x <= 10; x += 0.1)
  107. {
  108. double originalY = F(x);
  109. double interpolatedY = IF(x, A);
  110. double differenceY = originalY - interpolatedY;
  111.  
  112. Original.Points.AddXY(x, originalY);
  113. Interpolated.Points.AddXY(x, interpolatedY);
  114. Difference.Points.AddXY(x, differenceY);
  115. }
  116. }
  117.  
  118. void Button2_Click(object sender, EventArgs e)
  119. {
  120. ClearForm();
  121. Chebyshev(new double[] { 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  122. }
  123.  
  124. void Button3_Click(object sender, EventArgs e)
  125. {
  126. ClearForm();
  127. double a = 2;
  128. double b = 10;
  129. int n = 10;
  130. double[] xValues = new double[n]; // Mazgai ties Čiobyševo abscisėmis
  131. for (int i = 0; i < n; i++){
  132. xValues[i] = (b - a) / 2 * Math.Cos(Math.PI * (2 * i + 1) / (2 * n)) + (b + a) / 2;
  133. }
  134. Chebyshev(xValues.ToArray());
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement