Advertisement
Guest User

Untitled

a guest
May 26th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 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.  
  7. namespace ConsoleApp1
  8. {
  9. class Program
  10. {
  11. //ilosc punktow
  12. public static int ilosc_pkt = 5-1;
  13. //2
  14. public static int stopien_wielomianu = 5;
  15. //punkt do liczenia
  16. public static double punkt = 2;
  17. //punkty z mojego przykladu na sztywno
  18. //public static double[] pkt_x = { -1, -0.5, 0, 0.5, 1 };
  19. //public static double[] pkt_y = { 2.65, 1.8, 1.42, 1.8, 2.65 };
  20. public static double[] pkt_x = {1,2,3,4};
  21. public static double[] pkt_y = {6,19,40,69};
  22. static void Main(string[] args)
  23. {
  24.  
  25. //macierz ktora bede wypelniac sn
  26. double[,] macierz_moja_sn = new double[ilosc_pkt,ilosc_pkt];
  27. double[] macierz_moja_T = new double[ilosc_pkt];
  28. double[] macierz_wsp_a = new double[ilosc_pkt];
  29.  
  30. for(int i = 0; i < macierz_moja_sn.GetLength(0);i++)
  31. {
  32. for(int j = 0; j < macierz_moja_sn.GetLength(1);j++)
  33. {
  34. macierz_moja_sn[i, j] = policz_sumke_skj(i, j);
  35. }
  36. }
  37. foreach(double n in macierz_moja_sn)
  38. {
  39. Console.WriteLine(n);
  40. }
  41. Console.ReadKey();
  42. //tutaj licze t
  43. for(int a = 0;a < macierz_moja_T.GetLength(0);a++)
  44. {
  45. macierz_moja_T[a] = policz_sumke_tk(a);
  46. }
  47. //policzyc gaussem wspolczynniki
  48. macierz_wsp_a=GaussElimination(macierz_moja_sn, macierz_moja_T, macierz_moja_T.GetLength(0));
  49.  
  50. Console.WriteLine("--------------wspolczynniki----------");
  51. for(int b = 0; b < macierz_wsp_a.Length;b++)
  52. {
  53. Console.WriteLine(macierz_wsp_a[b]);
  54. }
  55. Console.ReadKey();
  56. //konczymy impreze
  57.  
  58. Console.WriteLine(koniec(macierz_wsp_a, punkt));
  59. Console.ReadKey();
  60.  
  61.  
  62. }
  63. //zaczynam metody statyczne do wspolczynnikow
  64. // policzmy teraz sume zeby wypelnic macierz
  65. public static double policz_sumke_skj(int i, int j )
  66. {
  67. //dobrze juz liczy skj
  68. double var = 0;
  69. int potega = i + j;
  70. for(int k = 0; k <=ilosc_pkt;k++)
  71. {
  72. var += Math.Pow(pkt_x[k], potega);
  73. }
  74. return var;
  75. }
  76. public static double policz_sumke_tk(int j)
  77. {
  78. //dobrze juz liczy
  79. double var = 0;
  80. for(int i =0;i<=ilosc_pkt;i++)
  81. {
  82. var += Math.Pow(pkt_x[i], j) * pkt_y[i];
  83. }
  84. Console.WriteLine("tk" + var);
  85. Console.ReadKey();
  86. return var;
  87. }
  88. //koncowa wartosc do policzenia
  89. public static double koniec(double[]macierz,double pkt)
  90. {
  91. int stp = stopien_wielomianu;
  92. double wartosc_w_pkt = 0;
  93. for(int i = 0; i < macierz.Length;i++)
  94. {
  95. //macierz tutaj robi za wspolczynniki
  96. wartosc_w_pkt += macierz[i] * Math.Pow(pkt, i);
  97. }
  98. return wartosc_w_pkt;
  99. }
  100. //elimacja gaussa
  101. public static double[] GaussElimination(double[,] A, double[] b, int n)
  102. {
  103. double[] x = new double[n];
  104.  
  105. double[,] tmpA = new double[n, n + 1];
  106.  
  107. for (int i = 0; i < n; i++)
  108. {
  109. for (int j = 0; j < n; j++)
  110. {
  111. tmpA[i, j] = A[i, j];
  112. }
  113. tmpA[i, n] = b[i];
  114. }
  115.  
  116. double tmp = 0;
  117.  
  118. for (int k = 0; k < n - 1; k++)
  119. {
  120. for (int i = k + 1; i < n; i++)
  121. {
  122. tmp = tmpA[i, k] / tmpA[k, k];
  123. for (int j = k; j < n + 1; j++)
  124. {
  125. tmpA[i, j] -= tmp * tmpA[k, j];
  126. }
  127. }
  128. }
  129.  
  130. for (int k = n - 1; k >= 0; k--)
  131. {
  132. tmp = 0;
  133. for (int j = k + 1; j < n; j++)
  134. {
  135. tmp += tmpA[k, j] * x[j];
  136. }
  137. x[k] = (tmpA[k, n] - tmp) / tmpA[k, k];
  138. }
  139. return x;
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement