Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include "trkf45_1\rkf45.h"
  4.  
  5. void f(float t, float *x, float *dx) {
  6. dx[0] = -155 * x[0] - 750 * x[1] + sin(1 + t);
  7. dx[1] = x[0] - cos(1 - t) + t + 1;
  8. return;
  9. }
  10.  
  11. int main() {
  12. int n = 2;
  13. float x0[2] = { 5, -1 };
  14. float t = 0, tout = 0;
  15. float re = 0.0001, ae = 0.0001;
  16. int iflag = 1;
  17. float work[15];
  18. int iwork[30];
  19. float h = 0.04;
  20. printf("rkf45, eps = 0.0001, h = %.2f\n", h);
  21. while (tout < 1.01) {
  22. RKF45(f, n, x0, &t, &tout, &re, &ae, &iflag, work, iwork);
  23. printf("t = %.2f, x = { %f; %f }, iflag = %d\n", t, x0[0], x0[1], iflag);
  24. tout += h;
  25. }
  26.  
  27. printf("\nRK 2nd degree of accuracy, h = 0.02\n");
  28. x0[0] = 5; x0[1] = -1;
  29. t = 0;
  30. h = 0.02;
  31. float z16[2], z1[2], dx[2];
  32.  
  33. while (t < 1.01) {
  34. f(t, x0, dx);
  35. z16[0] = x0[0] + h / 6 * dx[0];
  36. z16[1] = x0[1] + h / 6 * dx[1];
  37.  
  38. float dx2[2];
  39. f(t, z16, dx2);
  40. z1[0] = x0[0] + h * ( -2 * dx[0] + 3 * dx2[0]);
  41. z1[1] = x0[1] + h * (-2 * dx[1] + 3 * dx2[1]);
  42.  
  43. printf("t = %.2f, x = { %f; %f }\n", t, z1[0], z1[1]);
  44.  
  45. x0[0] = z1[0];
  46. x0[1] = z1[1];
  47. t += h;
  48. }
  49.  
  50. printf("\nRK 2nd degree of accuracy, h = 0.013\n");
  51. x0[0] = 5; x0[1] = -1;
  52. t = 0;
  53. h = 0.013;
  54. while (t < 1.005) {
  55. f(t, x0, dx);
  56. z16[0] = x0[0] + h / 6 * dx[0];
  57. z16[1] = x0[1] + h / 6 * dx[1];
  58.  
  59. float dx2[2];
  60. f(t, z16, dx2);
  61. z1[0] = x0[0] + h * (-2 * dx[0] + 3 * dx2[0]);
  62. z1[1] = x0[1] + h * (-2 * dx[1] + 3 * dx2[1]);
  63. printf("t = %.3f, x = { %f; %f }\n", t, z1[0], z1[1]);
  64. x0[0] = z1[0];
  65. x0[1] = z1[1];
  66. t += h;
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement