SpaceQuester

Untitled

Feb 17th, 2017
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include "math.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <locale.h>
  6.  
  7. #define N 5
  8. #define K 1
  9.  
  10. const int w_rand_min = 5;
  11. const int w_rand_max = 5;
  12.  
  13. const int theta_rand_min = 0;
  14. const int theta_rand_max = 2 * M_PI;
  15.  
  16. double A[N][N];
  17. double w[N];
  18. double theta[N];
  19.  
  20. double kuramoto(int i, double theta[N])
  21. {
  22. double sum = 0;
  23.  
  24. for (int j = 0; j < N; j++)
  25. sum += A[i][j] * sin(theta[j] - theta[i]);
  26.  
  27. return w[i] + sum * (double)K / N;
  28. }
  29.  
  30. void RungeKutta(double dt, double theta[N], double theta_next[N])
  31. {
  32. double k[N][4];
  33.  
  34. // k1
  35. for (int i = 0; i < N; i++)
  36. k[i][0] = kuramoto(i, theta) * dt;
  37.  
  38. double theta_k1[N];
  39. for (int i = 0; i < N; i++)
  40. theta_k1[i] = theta[i] + k[i][0] / 2;
  41.  
  42. // k2
  43. for (int i = 0; i < N; i++)
  44. k[i][1] = kuramoto(i, theta_k1) * dt;
  45.  
  46. double theta_k2[N];
  47. for (int i = 0; i < N; i++)
  48. theta_k2[i] = theta[i] + k[i][1] / 2;
  49.  
  50. // k3
  51. for (int i = 0; i < N; i++)
  52. k[i][2] = kuramoto(i, theta_k2) * dt;
  53.  
  54. double theta_k3[N];
  55. for (int i = 0; i < N; i++)
  56. theta_k3[i] = theta[i] + k[i][2] / 2;
  57.  
  58. // k4
  59. for (int i = 0; i < N; i++)
  60. k[i][3] = kuramoto(i, theta_k3) * dt;
  61.  
  62. for (int i = 0; i < N; i++)
  63. theta_next[i] = theta[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  64. }
  65.  
  66. void CopyArray(double source[N], double target[N])
  67. {
  68. for (int i = 0; i < N; i++)
  69. target[i] = source[i];
  70. }
  71.  
  72. void main()
  73. {
  74. /*for (int i = 0; i < N; i++)
  75. {
  76. w[i] = ((double)rand() / RAND_MAX) * (w_rand_max - w_rand_min) + w_rand_min;
  77. }*/
  78.  
  79. w[0] = 5;
  80. w[1] = 5;
  81. w[2] = 5;
  82. w[3] = 5;
  83. w[4] = 5;
  84.  
  85. for (int i = 0; i < N; i++)
  86. {
  87. theta[i] = ((double)rand() / RAND_MAX) * (theta_rand_max - theta_rand_min) + theta_rand_min;
  88. }
  89.  
  90. /*theta[0] = 0;
  91. theta[1] = M_PI / 2;
  92. theta[2] = M_PI;
  93. theta[3] = ((double)3 / 2) * M_PI;
  94. theta[4] = 2 * M_PI;*/
  95.  
  96. /*{
  97. A[0][0] = 0;
  98. A[0][1] = 1;
  99. A[0][2] = 0;
  100. A[0][3] = 0;
  101. A[0][4] = 1;
  102.  
  103. A[1][0] = 1;
  104. A[1][1] = 0;
  105. A[1][2] = 1;
  106. A[1][3] = 0;
  107. A[1][4] = 0;
  108.  
  109. A[2][0] = 0;
  110. A[2][1] = 1;
  111. A[2][2] = 0;
  112. A[2][3] = 1;
  113. A[2][4] = 0;
  114.  
  115. A[3][0] = 0;
  116. A[3][1] = 0;
  117. A[3][2] = 1;
  118. A[3][3] = 0;
  119. A[3][4] = 1;
  120.  
  121. A[4][0] = 1;
  122. A[4][1] = 0;
  123. A[4][2] = 0;
  124. A[4][3] = 1;
  125. A[4][4] = 0;
  126. }*/
  127.  
  128. {
  129. A[0][0] = 1;
  130. A[0][1] = 1;
  131. A[0][2] = 1;
  132. A[0][3] = 1;
  133. A[0][4] = 1;
  134.  
  135. A[1][0] = 1;
  136. A[1][1] = 1;
  137. A[1][2] = 1;
  138. A[1][3] = 1;
  139. A[1][4] = 1;
  140.  
  141. A[2][0] = 1;
  142. A[2][1] = 1;
  143. A[2][2] = 1;
  144. A[2][3] = 1;
  145. A[2][4] = 1;
  146.  
  147. A[3][0] = 1;
  148. A[3][1] = 1;
  149. A[3][2] = 1;
  150. A[3][3] = 1;
  151. A[3][4] = 1;
  152.  
  153. A[4][0] = 1;
  154. A[4][1] = 1;
  155. A[4][2] = 1;
  156. A[4][3] = 1;
  157. A[4][4] = 1;
  158. }
  159.  
  160. const double t_start = 0;
  161. const double t_max = 10;
  162. const double dt = 0.001;
  163.  
  164. double t = t_start;
  165.  
  166. FILE *fp;
  167. fp = fopen("results.txt", "w+");
  168. //setlocale(LC_NUMERIC, "French_Canada.1252");
  169.  
  170. while (t <= t_max+dt)
  171. {
  172. fprintf(fp, "%f\t", t);
  173. for (int i = 0; i < N; i++)
  174. {
  175. fprintf(fp, i == N - 1 ? "%f" : "%f\t", theta[i]);
  176. }
  177. fprintf(fp, "\n");
  178.  
  179. double theta_next[N];
  180.  
  181. RungeKutta(dt, theta, theta_next);
  182. CopyArray(theta_next, theta);
  183.  
  184. t += dt;
  185. }
  186.  
  187. fclose(fp);
  188. }
Add Comment
Please, Sign In to add comment