Guest User

code orbit

a guest
Dec 5th, 2015
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. // planet
  2. // understanding newton's laws
  3.  
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <iomanip>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. #define D 2 // number of dimensions
  13. struct particle {
  14. double x[D] ; // (x,y) coordinates
  15. double v[D] ; // velocity
  16. double a[D] ; // accelleration
  17. };
  18.  
  19. void showstate(particle &a, particle &b, double t);
  20. double displacement_sun(particle a);
  21. double displacement_earth(particle a, particle b);
  22. void positionstep(particle &a, double dt);
  23. void velocitystep(particle &a, double dt);
  24. void accelerationa(particle &a);
  25. void accelerationb(particle &a, particle &b);
  26.  
  27.  
  28.  
  29. int main()
  30. {
  31. particle a, b;
  32. double r, rs, re, t=0.0, dt=0.001;
  33.  
  34. a.x[0]=1;
  35. a.x[1]=0;
  36. a.v[0]=0;
  37. a.v[1]=1;
  38. a.a[0]=0;
  39. a.a[1]=0;
  40.  
  41. b.x[0]=1.1;
  42. b.x[1]=0;
  43. b.v[0]=0;
  44. b.v[1]=1.1;
  45. b.a[0]=0;
  46. b.a[1]=0;
  47.  
  48. for (int i=1; i<=10000; i++)
  49. {
  50. positionstep(a, dt*0.5);
  51. positionstep(b, dt*0.5);
  52. accelerationa(a);
  53. accelerationb(a, b);
  54. velocitystep(a, dt);
  55. velocitystep(b, dt);
  56. positionstep(a, dt*0.5);
  57. positionstep(b, dt*0.5);
  58. t += dt;
  59. showstate(a,b,t);
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. void showstate(particle &a, particle &b, double t)
  66. {
  67. cout << t << "\t" << a.x[0] << "\t" << a.x[1] << "\t" << a.v[0] << "\t" << a.v[1]
  68. << "\t" << b.x[0] << "\t" << b.x[1] << "\t" << b.v[0] << "\t" << b.v[1] << endl;
  69. }
  70.  
  71. double displacement_sun(particle a)
  72. {
  73. double r;
  74. r = sqrt((a.x[0])*(a.x[0])+(a.x[1])*(a.x[1]));
  75. return r;
  76. }
  77.  
  78. double displacement_earth(particle a, particle b)
  79. {
  80. double r;
  81. r = sqrt(((b.x[0])-(a.x[0]))*((b.x[0])-(a.x[0]))+((b.x[1])-(a.x[1]))*((b.x[1])-(a.x[1])));
  82. return r;
  83. }
  84.  
  85. void accelerationa(particle &a)
  86. {
  87. double G=1, M=1, r;
  88. r = displacement_sun(a);
  89. a.a[0]=-G*M*(a.x[0])/(r*r*r);
  90. a.a[1]=-G*M*(a.x[1])/(r*r*r);
  91. }
  92.  
  93. void accelerationb(particle &a, particle &b)
  94. {
  95. int i;
  96. double G=1, M=1, m=0.1, rs, re;
  97. rs = displacement_sun(a);
  98. re = displacement_earth(a, b);
  99. for (i=0; i<=D; i++)
  100. {
  101. if (sqrt(b.x[i]*b.x[i])>sqrt(a.x[i]*a.x[i]))
  102. {
  103. b.a[i]=-G*M*(b.x[i])/(rs*rs*rs)-G*m*(b.x[i]-a.x[i])/(re*re*re);
  104. }
  105. if (sqrt(b.x[i]*b.x[i])<sqrt(a.x[i]*a.x[i]))
  106. {
  107. b.a[i]=-G*M*(b.x[i])/(rs*rs*rs)+G*m*(b.x[i]-a.x[i])/(re*re*re);
  108. }
  109. }
  110.  
  111. }
  112.  
  113. void velocitystep(particle &a, double dt)
  114. {
  115. a.v[0]+=(a.a[0])*dt;
  116. a.v[1]+=(a.a[1])*dt;
  117. }
  118.  
  119. void positionstep(particle &a, double dt)
  120. {
  121. a.x[0]+=+(a.v[0])*dt;
  122. a.x[1]+=+(a.v[1])*dt;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment