SpaceQuester

Untitled

Feb 16th, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 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 5
  9.  
  10. const int w_rand_min = 5;
  11. const int w_rand_max = 6;
  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[i] - theta[j]);
  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. w[i] = ((double)rand() / RAND_MAX) * (w_rand_max - w_rand_min) + w_rand_min;
  76.  
  77. for (int i = 0; i < N; i++)
  78. theta[i] = ((double)rand() / RAND_MAX) * (theta_rand_max - theta_rand_min) + theta_rand_min;
  79.  
  80. /*{
  81. A[0][0] = 0;
  82. A[0][1] = 1;
  83. A[0][2] = 0;
  84. A[0][3] = 0;
  85. A[0][4] = 1;
  86.  
  87. A[1][0] = 1;
  88. A[1][1] = 0;
  89. A[1][2] = 1;
  90. A[1][3] = 0;
  91. A[1][4] = 0;
  92.  
  93. A[2][0] = 0;
  94. A[2][1] = 1;
  95. A[2][2] = 0;
  96. A[2][3] = 1;
  97. A[2][4] = 0;
  98.  
  99. A[3][0] = 0;
  100. A[3][1] = 0;
  101. A[3][2] = 1;
  102. A[3][3] = 0;
  103. A[3][4] = 1;
  104.  
  105. A[4][0] = 1;
  106. A[4][1] = 0;
  107. A[4][2] = 0;
  108. A[4][3] = 1;
  109. A[4][4] = 0;
  110. }*/
  111.  
  112. {
  113. A[0][0] = 0;
  114. A[0][1] = 1;
  115. A[0][2] = 1;
  116. A[0][3] = 1;
  117. A[0][4] = 1;
  118.  
  119. A[1][0] = 1;
  120. A[1][1] = 0;
  121. A[1][2] = 1;
  122. A[1][3] = 1;
  123. A[1][4] = 1;
  124.  
  125. A[2][0] = 1;
  126. A[2][1] = 1;
  127. A[2][2] = 0;
  128. A[2][3] = 1;
  129. A[2][4] = 1;
  130.  
  131. A[3][0] = 1;
  132. A[3][1] = 1;
  133. A[3][2] = 1;
  134. A[3][3] = 0;
  135. A[3][4] = 1;
  136.  
  137. A[4][0] = 1;
  138. A[4][1] = 1;
  139. A[4][2] = 1;
  140. A[4][3] = 1;
  141. A[4][4] = 0;
  142. }
  143.  
  144. const double t_max = 25;
  145. const double t_start = 0;
  146. const double dt = 0.001;
  147.  
  148. double t = t_start;
  149.  
  150. FILE *fp;
  151. fp = fopen("results.txt", "w+");
  152. //setlocale(LC_NUMERIC, "French_Canada.1252");
  153.  
  154. while (t <= t_max)
  155. {
  156. fprintf(fp, "%f\t", t);
  157. for (int i = 0; i < N; i++)
  158. {
  159. fprintf(fp, "%f\t", theta[i]);
  160. }
  161. fprintf(fp, "\n");
  162.  
  163. double theta_next[N];
  164.  
  165. RungeKutta(dt, theta, theta_next);
  166. CopyArray(theta_next, theta);
  167.  
  168. t += dt;
  169. }
  170.  
  171. fclose(fp);
  172. }
Add Comment
Please, Sign In to add comment