Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Interpolation
  9. {
  10. class Program
  11. {
  12. #region Нужные переменные
  13.  
  14. enum Variant
  15. {
  16. A = 9, B = 29, C = 25
  17. }
  18.  
  19. // номер варианта
  20. static Variant variant;
  21.  
  22. const int N = 6;
  23. // -----------------------------------------
  24. //матрица разделённых разностей размерность 6 X 7
  25. static double[,] divided_differences = new double[N,N + 1];
  26. static double x = 1;
  27. static double f;
  28.  
  29. // для работы с файлами
  30. public static StreamWriter sw = new StreamWriter("C:\\Users\\Diana\\Desktop\\Learning\\Численные методы\\Interpolation_Output.txt");
  31. #endregion
  32.  
  33. #region Вычисление значений функций и производных
  34.  
  35. // функция
  36. public static double Function(double cur_x)
  37. {
  38. switch (variant)
  39. {
  40. case (Variant.A):
  41. {
  42. return (Math.Pow(Math.E, cur_x) - 6*cur_x + 3);
  43. }
  44. break;
  45.  
  46. case (Variant.B):
  47. {
  48. return (Math.Pow(3, cur_x) + 2*cur_x*cur_x - 3*cur_x + 1);
  49. }
  50. break;
  51.  
  52. case (Variant.C):
  53. {
  54. return (Math.Pow(3, cur_x) + 2 - cur_x);
  55. }
  56. default:
  57. return Int32.MaxValue;
  58. break;
  59. }
  60. }
  61.  
  62. // шестая производная
  63. public static double Sixth_Derivative(double cur_x)
  64. {
  65. switch (variant)
  66. {
  67. case (Variant.A):
  68. {
  69. return (Math.Pow(Math.E, cur_x));
  70. }
  71. break;
  72.  
  73. case (Variant.B):
  74. {
  75. return (Math.Pow(Math.Log(3), 6) * Math.Pow(3, cur_x));
  76. }
  77. break;
  78. case (Variant.C):
  79. {
  80. return (Math.Pow(Math.Log(3), 6) * Math.Pow(3, cur_x));
  81. }
  82. default:
  83. return Int32.MaxValue;
  84. break;
  85. }
  86. }
  87. #endregion
  88.  
  89. #region Вывод матрицы
  90.  
  91. public static void Output_Matrix()
  92. {
  93. int j = 0;
  94. for (int i = 0; i < N; i++)
  95. {
  96. while (j < N + 1)
  97. {
  98. sw.Write(String.Format("{0,11:0.0000000}", divided_differences[i, j]) + "\t");
  99. j++;
  100. }
  101. sw.WriteLine();
  102. j = 0;
  103. }
  104. }
  105. #endregion
  106.  
  107. #region Интерполяционная формула Ньютона
  108.  
  109. // матрица разделённых разностей
  110. public static void Divided_Differences_Matrix()
  111. {
  112. // заполняем первый и второй столбцы
  113. // первый столбик - значения иксов от 1 до 2 с шагом 0.2
  114. // второй столбик - значения функции от каждого значения из первого столбца соответственно
  115. for (int i = 0; i < N; i++, x += 0.2)
  116. {
  117. divided_differences[i, 0] = x;
  118. divided_differences[i, 1] = Function(x);
  119. }
  120.  
  121. // заполняем по столбцам (в матрице 7 столбцов и 6 строк)
  122. for (int column = 2; column < divided_differences.GetLength(1); column++)
  123. for (int str = 0; str < divided_differences.GetLength(0) - column + 1; str++)
  124. divided_differences[str, column] = (divided_differences[str + 1, column - 1] - divided_differences[str, column - 1])
  125. / (divided_differences[column + str - 1, 0] - divided_differences[str, 0]);
  126. }
  127.  
  128. // многочлен Ньютона
  129. public static double NewtonPolinomial(double cur_x)
  130. {
  131. double newton_polinomial = 0;
  132. // добавляем f(1) к многочлену
  133. newton_polinomial += divided_differences[0, 1];
  134.  
  135. // для каждого значения из первой строки таблицы разделённых разностей
  136. // добавляем соответствующий многочлен
  137. for (int column = 2; column < divided_differences.GetLength(1); column++)
  138. newton_polinomial += divided_differences[0, column] * PolinomialForEachColumn(column, cur_x);
  139.  
  140. return newton_polinomial;
  141. }
  142.  
  143. // подсчёт полинома для каждого столбца матрицы разделённых разностей
  144. public static double PolinomialForEachColumn(int cur_column, double cur_x)
  145. {
  146. int count_of_polinomials = 0;
  147. int str = 0;
  148. double new_polinomial = 1;
  149.  
  150. while (count_of_polinomials != cur_column - 1)
  151. {
  152. new_polinomial *= (cur_x - divided_differences[str, 0]);
  153. str++;
  154. count_of_polinomials++;
  155. }
  156.  
  157. return new_polinomial;
  158. }
  159.  
  160. // для оценки погрешности
  161. public static double GetWPolinomial(double cur_x)
  162. {
  163. double w_polinomial = 1;
  164.  
  165. for (int i = 0; i < N - 1; i++)
  166. w_polinomial *= cur_x - divided_differences[i, 0];
  167.  
  168. return Math.Abs(w_polinomial);
  169. }
  170.  
  171. // нахождение максимального значения шестой производной на отрезке от 1 до 2
  172. public static double MaxSixthDerivative()
  173. {
  174. double cur_x = 1;
  175. double max_sixth_derivative = Int32.MinValue;
  176. double sixth_derivative = 0;
  177. for (int i = 0; i < N - 1; i++, cur_x += 0.2)
  178. {
  179. sixth_derivative = Math.Abs(Sixth_Derivative(cur_x));
  180. if (sixth_derivative > max_sixth_derivative)
  181. max_sixth_derivative = sixth_derivative;
  182. }
  183.  
  184. return sixth_derivative;
  185. }
  186.  
  187. #endregion
  188.  
  189. static void Main(string[] args)
  190. {
  191. // менять вариант вручную! A - 9, B - 29
  192. variant = Variant.C;
  193.  
  194. // --------- Интерполяционная формула Ньютона ------------
  195. sw.WriteLine("ИНТЕРПОЛЯЦИОННАЯ ФОРМУЛА НЬЮТОНА");
  196.  
  197. // вывод матрицы разделённых разностей
  198. Divided_Differences_Matrix();
  199. sw.WriteLine("Матрица разделённых разностей имеет вид:");
  200. Output_Matrix();
  201. sw.WriteLine();
  202. x = 1.1;
  203. // вывод таблицы x - f(x) - Pn(x) - Delta - Оценка
  204. sw.WriteLine("x - f(x) - Pn(x) - Delta - Оценка");
  205. for (int i = 0; i < N - 1; i++, x += 0.2)
  206. {
  207. double function = Function(x);
  208. double polinomial_newton = NewtonPolinomial(x);
  209. double delta = Math.Abs(Function(1 + (i + 0.5) * 0.2) - NewtonPolinomial(1 + (i + 0.5) * 0.2));
  210. double error_of_estimate = (MaxSixthDerivative() * GetWPolinomial(x)) / 720;
  211. sw.Write(x + "\t" + String.Format("{0,11:0.00000}", function) + "\t"
  212. + String.Format("{0,11:0.00000}", polinomial_newton) + "\t"
  213. + String.Format("{0,15:#.########E-00}", delta) + "\t"
  214. + String.Format("{0,15:#.########E-00}", error_of_estimate));
  215. sw.WriteLine();
  216. }
  217. // -------------------------------------------------------
  218.  
  219.  
  220.  
  221. sw.Close();
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement