Guest User

Untitled

a guest
Apr 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double funcVx(double X,double Y, double A);
  6. double funcVx(double X,double Y, double A)//function for Vx
  7.  
  8. {
  9.     double a;
  10.     a=A*X/(double)(pow((pow(X,2)+pow(Y,2)),1.5));
  11.     return a;
  12. }
  13.  
  14. double funcVy(double X,double Y, double A);
  15. double funcVy(double X,double Y, double A)//function for Vy
  16.  
  17. {
  18.     double a;
  19.     a=A*Y/(double)(pow((pow(X,2)+pow(Y,2)),1.5));
  20.     return a;
  21. }
  22.  
  23. int main()
  24. {
  25.     FILE *file;
  26.     double G,M,A,deltat,t,tmax,x,y,Vx,Vy,Vxk1,Vxk2,Vxk3,Vxk4,Vyk1,Vyk2,Vyk3,Vyk4,xk1,xk2,xk3,xk4,yk1,yk2,yk3,yk4;
  27.     G=6.673E-11;
  28.     t=0;
  29.  
  30.     printf("Please enter the mass of the larger body\n");
  31.     scanf("%lf",&M);
  32.     printf("please enter the initial cartesian coordinates of the smaller body\n");
  33.     printf("x=");
  34.     scanf("%lf",&x);
  35.     printf("y=");
  36.     scanf("%lf",&y);
  37.     printf("Please enter the initial velocity in each direction\n");
  38.     printf("Vx=");
  39.     scanf("%lf",&Vx);
  40.     printf("Vy=");
  41.     scanf("%lf",&Vy);
  42.     printf("Please enter the time over which the problem is to be modelled\n");//MORE ELOQUENT!!!!
  43.     scanf("%lf",&tmax);
  44.     printf("please enter the time step\n");
  45.     scanf("%lf",&deltat);
  46.     A=M*G;
  47.  
  48.     file=fopen("circularmotion.txt","w");
  49.  
  50.     for (t=0;t<tmax;t=t+deltat)
  51.     {
  52.  
  53.     Vxk1=funcVx(x,y,A);//Runge-Kutta for Vx
  54.     xk1=Vx;
  55.  
  56.     Vxk2=funcVx((x+0.5*xk1*deltat),y,A);
  57.     xk2=Vx*Vxk1*0.5*deltat;
  58.  
  59.     Vxk3=funcVx((x+0.5*xk2*deltat),y,A);
  60.     xk3=Vx*Vxk2*0.5*deltat;
  61.  
  62.     Vxk4=funcVx((x+xk3*deltat),y,A);
  63.     xk4=Vx*Vxk3*deltat;
  64.  
  65.  
  66.     Vyk1=funcVy(x,y,A);//Runge-Kutta for Vx
  67.     yk1=Vy;
  68.  
  69.     Vyk2=funcVy(x,(y+0.5*yk1*deltat),A);
  70.     yk2=Vy*Vyk1*0.5*deltat;
  71.  
  72.     Vyk3=funcVy(x,(y+0.5*yk2*deltat),A);
  73.     yk3=Vy*Vyk2*0.5*deltat;
  74.  
  75.     Vyk4=funcVy(x,(y+yk3*deltat),A);
  76.     yk4=Vy*Vyk3*deltat;
  77.  
  78.  
  79.     Vx=Vx+deltat*(Vxk1/(double)6)+(Vxk2/(double)3)+(Vxk3/(double)3)+(Vxk4/(double)6);
  80.     Vy=Vy+deltat*(Vyk1/(double)6)+(Vyk2/(double)3)+(Vyk3/(double)3)+(Vyk4/(double)6);
  81.     x=x+deltat*(xk1/(double)6)+(xk2/(double)3)+(xk3/(double)3)+(xk4/(double)6);
  82.     y=y+deltat*(yk1/(double)6)+(yk2/(double)3)+(yk3/(double)3)+(yk4/(double)6);
  83.  
  84.     fprintf(file,"%g\t%g\t%g\t%g\n",x,y,Vx,Vy);
  85.     printf("x=%g, y=%g, Vx=%g, Vy=%g\n",x,y,Vx,Vy);
  86.  
  87.     }
  88.  
  89. fclose(file);
  90.  
  91.     return 0;
  92. }
Add Comment
Please, Sign In to add comment