-
Notifications
You must be signed in to change notification settings - Fork 33
/
least_squares.C
71 lines (56 loc) · 1.38 KB
/
least_squares.C
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
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
//#include <vector>
#include <boost/serialization/vector.hpp>
#include "least_squares.h"
#include "pario.h"
using namespace std;
vector<double> dw;
vector<double> energy;
void least_squares(vector<double> x, vector<double> y){
double avgx = 0.0;
double avgy = 0.0;
double avgxx = 0.0;
double avgyy = 0.0;
double avgxy = 0.0;
double sserr = 0.0;
double sstot = 0.0;
double beta;
double alpha;
double r2;
int max;
int n; //size of n
int n_low; //n- max
int i;
double f;
max=3; //take the max number of values
n = x.size();
n_low = n - max;
for (i=n_low; i<n; i++) {
avgx+=x[i];
avgy+=y[i];
avgxx+=x[i]*x[i];
avgxy+=x[i]*y[i];
avgyy+=y[i]*y[i];
}
avgx/=max;
avgy/=max;
avgxx/=max;
avgxy/=max;
avgyy/=max;
beta=(avgxy-avgx*avgy)/(avgxx-avgx*avgx);
alpha = avgy-beta*avgx;
//*a = alpha;
for (i=n_low; i<n; i++) {
f=alpha + beta*x[i];
sserr += pow(y[i]-f, 2);
sstot += pow(y[i]-avgy, 2);
}
r2 = 1. - sserr/sstot;
//pout << "beta " << beta << " alpha " << alpha << " r2 " << r2 << endl;
//pout << "Extrapolated energy: " << alpha << " a.u." << endl;
pout << "\n\t\t\tExtrapolated Energy = " << fixed << setprecision(10) << alpha << " a.u." << endl << endl;
}