Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define A 0
  5. #define B 10
  6. #define K 1
  7.  
  8. double expected_u (double x, double t);
  9. double initial_condition (double x); //t=0
  10. double left_border_condition (double t); //x=A
  11. double right_border_condition (double t); //x=B
  12. double right_part_func (double x, double t);
  13. void explicit_next_layer_calculate (double *prev_layer, double *next_layer, double layer_t, double x_step, double t_step);
  14. void pr_ar (double * arr, int dim);
  15.  
  16. int main (void) {
  17. int input_param = -1;
  18. double end_time;
  19. double x_step, t_step;
  20. int x_dim, t_dim;
  21. while (input_param) {
  22. printf ("Please, type 1 if you want to use steps or type 2 if you want to use count of segments. Type 0 if you want to exit. >>");
  23. scanf ("%d", &input_param);
  24. if (input_param) {
  25. printf ("Type end_time. >>");
  26. scanf ("%lf", &end_time);
  27. switch (input_param)
  28. {
  29. case 1: {
  30. printf ("Type range step (h). >>");
  31. scanf ("%lf", &x_step);
  32. printf ("Type time step (tau). >>");
  33. scanf ("%lf", &t_step);
  34. x_dim = (B - A) / x_step;
  35. t_dim = end_time / t_step;
  36. printf ("t_dim %d", t_dim);
  37. if ((B - A - x_step * x_dim) > 0) {
  38. x_dim += 2;
  39. }
  40. else {
  41. x_dim += 1;
  42. }
  43. if ((end_time - t_step * t_dim) > 0) {
  44. t_dim += 2;
  45. }
  46. else {
  47. t_dim += 1;
  48. }
  49. break;
  50. }
  51. case 2: {
  52. printf ("Type count of segments for x axis. >>");
  53. scanf ("%d", &x_dim);
  54. printf ("Type count of segments for t axis. >>");
  55. scanf ("%d", &t_dim);
  56. x_step = (B - A) / x_dim;
  57. t_step = end_time / t_dim;
  58. x_dim ++;
  59. t_dim ++;
  60. break;
  61. }
  62. default: {
  63. printf ("Invalid command. Type 1 or 2 to continue. Type 0 to exit. >>");
  64. scanf ("%d", &input_param);
  65. }
  66. }
  67. printf ("Input completed. Following values were typed:\n\th = %.3lf\n\ttau = %.3lf\n", x_step, t_step);
  68. printf ("Count of points per each axis:\n\tfor x: %d\n\tfor t: %d\n", x_dim, t_dim);
  69.  
  70.  
  71. printf("x_dim %d", x_dim);
  72. double prev_layer [x_dim];
  73. double next_layer [x_dim];
  74. printf("wtf");
  75. for (int i = 0; i < x_dim; i++) {
  76. prev_layer[i] = initial_condition (A + x_step * i);
  77. //printf("%d\t",i);
  78. }
  79.  
  80. double t = t_step;
  81. while (t < end_time) {
  82. explicit_next_layer_calculate (prev_layer, next_layer, t, x_step, t_step);
  83. memcpy (prev_layer, next_layer, x_dim*(sizeof(double)));
  84. t += t_step;
  85. }
  86.  
  87. if (t-t_step > 0.0) {
  88. explicit_next_layer_calculate (prev_layer, next_layer, end_time, x_step, (B - t - t_step));
  89. }
  90. else {
  91. explicit_next_layer_calculate (prev_layer, next_layer, end_time, x_step, t_step);
  92. }
  93.  
  94. pr_ar(next_layer, x_dim);
  95. }
  96. }
  97.  
  98. return 0;
  99. }
  100.  
  101. void explicit_next_layer_calculate (double *prev_layer, double *next_layer, double layer_t, double x_step, double t_step) {
  102.  
  103. next_layer[0] = left_border_condition(layer_t);
  104. int i = 1;
  105. int x = A + t_step;
  106. while (x + x_step < B) {
  107. next_layer[i] = prev_layer[i-1] + (K * t_step / (x_step * x_step)) * (prev_layer[i-1] - 2 * prev_layer[i] + prev_layer[i+1]) +
  108. t_step * right_part_func(x,layer_t);
  109. i++;
  110. x = x + x_step;
  111. }
  112. next_layer[i] = right_border_condition(layer_t);
  113. }
  114.  
  115. double expected_u (double x, double t) {
  116. return x * x * x + 1 + 4 * t * t;
  117. }
  118.  
  119. double initial_condition (double x){ //t=0
  120. return x * x * x +1;
  121. }
  122.  
  123. double left_border_condition (double t) { //x=A
  124. return A * A * A + 1 + 4 * t * t;
  125. }
  126.  
  127. double right_border_condition (double t) { //x=B
  128. return B * B * B + 1 + 4 * t * t;
  129. }
  130.  
  131. double right_part_func (double x, double t) {
  132. return 8 * t - 6 * x;
  133. }
  134.  
  135. void pr_ar (double * arr, int dim) {
  136. for (int i = 0; i < dim; i++) {
  137. printf ("%.3lf/t", arr[i]);
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement