-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwardeuler.cpp
69 lines (54 loc) · 1.58 KB
/
forwardeuler.cpp
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
// Forward euler implemented
#include<cmath>
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void forwardeuler(int NN, double T){
//Calculating steplength h and time
double N = NN;
double totaltime = T;
double h = totaltime/((double) N);
double time = 0.0;
//Initializing pi and D
double pi = acos(-1);
double D = 4*pi*pi;
//Initializing the scaled positions, kin.energy, pot.energy and ang.mom.
double x = 1.;
double y = 0.;
double vx = 0;
double vy = 2*pi;
double pot = 0;
double kin = 0;
double ang = 0;
//Initializing files
ofstream myfile;
myfile.open ("EulerValues.txt");
ofstream myfile2;
myfile2.open("ConservedEulerValues.txt");
while(time < totaltime){
//Calculate r and r3
double r = sqrt(x*x + y*y);
double rt = r*r*r;
//calculating the i+1 position and i+2 velocity
x += vx*h;
y += vy*h;
vx -= (h*D*x)/(rt);
vy -= (h*D*y)/(rt);
//Calculating the kinetick, potential and angular momentum
double Mearth = 5.97*pow(10,24);
double vtot = sqrt(vx*vx + vy*vy);
double radis = sqrt(x*x + y*y);
kin = 0.5*(vx*vx + vy*vy)*Mearth;
pot = Mearth*D/radis;
ang = Mearth*radis*vtot;
//write to file
myfile << x <<" " << y << endl;
myfile2 << kin << " " << pot << " " << ang << endl;
//set new timestep
time += h;
}//end while
//close the file
myfile.close();
myfile2.close();
}//End Euler function