-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_equation_simulation.cpp
More file actions
50 lines (38 loc) · 1.03 KB
/
logistic_equation_simulation.cpp
File metadata and controls
50 lines (38 loc) · 1.03 KB
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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;
double r, K, N_0, h;
string filename = "logistic_equation_simulation_by_for_loop.csv";
ofstream writing_file;
double dN_dt(double N) {
return r * N * (1 - N / K);
}
// ロジスティック方程式の厳密解
double N(double t) {
return N_0 * K / (N_0 + (K - N_0) * exp(-r * t));
}
// 二乗和誤差を計算
double calculate_rse(double N_t, double t) {
return sqrt(pow((N_t - N(t)), 2));
}
int main(void) {
writing_file.open(filename, ios::out);
cin >> r >> K >> N_0 >> h;
double error_sum = 0;
double N_t1 = N_0;;
for(int i=0; i<10/h + 1; i++) {
if(i == 0) continue;
double t = h * i;
double N_t = N_t1 + h * dN_dt(N_t1);
N_t1 = N_t;
writing_file << t << ", " << N_t << "\n";
// 二乗和誤差を求める。各点の二乗和誤差を足していく。
error_sum += calculate_rse(N_t, t);
}
error_sum = error_sum / 10 * h;
cout << "error_sum:" << error_sum << "\n";;
writing_file.close();
}