Guest User

Untitled

a guest
Apr 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. const float G=-6.673E-11;//stores the gravitational constant
  6.  
  7. double funcx(double M, double x, double y);//the functions for the velocities
  8. double funcy(double M, double x, double y);
  9.  
  10. int main()
  11. {
  12. FILE *path;
  13. double x,y,t,T,dt,vx,vy,M,k1x,k2x,k3x,k4x,k1y,k2y,k3y,k4y,k1vx,k2vx,k3vx,k4vx,k1vy,k2vy,k3vy,k4vy;
  14.  
  15. printf("Calculating a basic orbit of a moving body\n");
  16.  
  17. printf("\nEnter the mass of the large object: ");
  18. scanf("%lf",&M);
  19. printf("\nEnter the initial coordinates of the moving object\n");
  20. printf("x = ");
  21. scanf("%lf",&x);
  22. printf("y = ");
  23. scanf("%lf",&y);
  24. printf("\nEnter the initial velocities of the moving object\n");
  25. printf("x velocity = ");
  26. scanf("%lf",&vx);
  27. printf("y velocity = ");
  28. scanf("%lf",&vy);
  29. printf("\nEnter the time over which to map the orbit: ");
  30. scanf("%lf",&T);
  31. printf("Enter the size of the time interval to calculate over: ");
  32. scanf("%lf",&dt);
  33.  
  34. path=fopen("path.txt","w");
  35.  
  36. for(t=0;t<T;t+=dt)
  37. {
  38. k1vx=funcx(M,x,y);
  39. k1x=vx;
  40. k2vx=funcx(M,(x+(k1x*dt/2)),y);
  41. k2x=vx*k1vx*(dt/2);
  42. k3vx=funcx(M,(x+(k2x*dt/2)),y);
  43. k3x=vx*k2vx*(dt/2);
  44. k4vx=funcx(M,(x+(k3x*dt)),y);
  45. k4x=vx*k3x*dt;
  46.  
  47. k1vy=funcy(M,x,y);
  48. k1y=vy;
  49. k2vy=funcy(M,x,(y+(k1y*dt/2)));
  50. k2y=vy*k1vy*(dt/2);
  51. k3vy=funcy(M,x,(y+(k2y*dt/2)));
  52. k3y=vy*k2vy*(dt/2);
  53. k4vy=funcy(M,x,(y+(k3y*dt)));
  54. k4y=vy*k3vy*dt;
  55.  
  56. vx+=(dt/6)*(k1vx+(2*k2vx)+(2*k3vx)+k4vx);
  57. vy+=(dt/6)*(k1vy+(2*k2vy)+(2*k3vy)+k4vy);
  58.  
  59. x+=dt*vx;
  60. y+=dt*vy;
  61.  
  62. fprintf(path,"%f%c%f%c%f%c%f\n",x,9,y,9,vx,9,vy);
  63. }
  64.  
  65. return 0;
  66. }
  67. double funcx(double M, double x, double y)
  68. {
  69. return (M*G*x)/pow((x*x)+(y*y),3/(double)2);
  70. }
  71. double funcy(double M, double x, double y)
  72. {
  73. return (M*G*y)/pow((x*x)+(y*y),3/(double)2);
  74. }
Add Comment
Please, Sign In to add comment