1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <cmath>
#include <iostream>
#include <fstream>
using namespace std;
int size();
void initial( double& tstop, double& t, double& dt , double y0[]);
void math( double& t, double rhs[], double y0[]) ;
int main()
{
std :: ofstream myfile;
myfile.open ("ass101.dat");
double G,Msun,x,vx,vy;
double xn,yn,vxn,vyn;
double t, tstop, dt, thalf;
double *y, *y1 , *rhs, *y0 ;
double *k1, *k2, *k3, *k4 ;
int neqns;
neqns = size();
y=new double[neqns];
rhs=new double[neqns];
y1=new double[neqns];
k1=new double[neqns];
k2=new double[neqns];
k3=new double[neqns];
k4=new double[neqns];
initial(tstop, t, dt , y);
while(t<tstop)
{
math(t, rhs, y ) ;
for(int i=0; i < neqns; i++) k1[i] = dt * rhs[i];
math(t, rhs, k1 ) ;
for(int i=0; i < neqns; i++) k2[i] = (.5 * k1[i]);
math(t, rhs, k2 ) ;
for(int i=0; i < neqns; i++) k3[i] = (.5 * k2[i]);
math(t, rhs, k3 ) ;
for(int i=0; i < neqns; i++) k4[i] = ( k3[i] );
for(int i=0; i < neqns; i++) y[i] = y[i] + (k1[i] + 2.0*k2[i] + 2.0*k3[i] + k4[i] ) / 6.0;
myfile << y[0] << " " << y[1] << " " << t << "\n";
t=t+dt;
}
cout << "\n" << " X = " << y[0] << " " << " Y = " << y[1] << "\n" "\n";
delete [] y; delete [] rhs; delete [] y1;
return(0);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int size()
{
return(4);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void initial( double& tstop, double& t, double& dt , double y0[])
{
y0[0] = 2.0; // x
y0[1] = 0.0; // y
y0[2] = -2.5; // vx
y0[3] = 3.25; // vy
t = 0.0;
dt = .001/365.0;
tstop = 2.286637;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void math(double& t, double rhs[], double y0[])
{
const double Msun = 1;
const double G = 39.47;
double x, y, vx, vy;
x = y0[0];
y = y0[1];
vx = y0[2];
vy = y0[3];
rhs[0] = vx;
rhs[1] = vy;
rhs[2] = - G*Msun*x/ pow(( pow(x,2) + pow(y,2) ),1.5);
rhs[3] = - G*Msun*y/ pow(( pow(x,2) + pow(y,2) ),1.5);
|