-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
52 lines (40 loc) · 1.05 KB
/
code.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
// Euler's method
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
//Declaring initial condition variables
double x = 0, z = 1;
double h = 0.01;
//Declaring the euler function
void euler(double w){
//Declaring file name as an automated process.
//You can do this manually if this seems difficult to understand.
//You have to run the program more than once if you do so.
std::string filename = "euler_for_w_" + std::to_string(int(w)) + ".dat";
ofstream fout(filename);
fout << "t \t x \t z" << endl;
//Initiating limit
double lim = 20;
cout << "Writing to file \"" << filename << "\" for w = " << w << endl;
double x1, z1;
for(double t=0; t <= lim; t += h){
fout << t << "\t" << x << "\t" << z << endl;
x1 = x + z*h;
z1 = z -1*w*w*x*h;
//Swapping variable values
x = x1;
z = z1;
}
cout << "Done for w = "<< w << endl << endl;
fout.close();
}
int main(){
euler(1);
euler(4);
euler(9);
euler(16);
cout << "Process Completed! \n \nCheck the above files.";
return 0;
}