-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_leastsq.cpp
More file actions
201 lines (84 loc) · 2.39 KB
/
test_leastsq.cpp
File metadata and controls
201 lines (84 loc) · 2.39 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <stdlib.h>
#include <math.h> /* math functions */
using namespace std;
// function to calculate the average of input array
double avg(double x[],int nx){
float sum;
sum = 0.0;
for( int i = 0; i < nx; i = i + 1 ) {
sum = sum + x[i];
}
return(sum/nx);
}
// function to calculate the pivot point for linear regression
double xpivot(double x[],double sig[],int nx){
double sum1=0.0,sum2=0.0,a;
for(int i=0;i<nx;i = i + 1) {
a = 1./pow(sig[i],2);
sum1 = sum1 + x[i]*a;
sum2 = sum2 + a;
}
return(sum1/sum2);
}
// least squares fitting function
// x and y values set with scatter about random position, now do least squares fit
void leastsq(double x[],double y[],double sigy[],int n,double xpiv,double cop,double sigcop,double mop,double sigmop){
double b,c,a,grad,siggrad,sigintercept,intercept,sum1=0,sum2=0,sum3=0,sum4=0,siga,sigb;
xpiv = xpivot(x,sigy,n);
for(int i=0;i<n;i = i + 1) {
a = 1./pow(sigy[i],2);
c = x[i]-xpiv;
sum1 = sum1 + y[i]*a;
sum2 = sum2 + a;
sum3 = sum3 + y[i]*c*a;
sum4 = sum4 + c*c*a;
}
intercept = sum1/sum2;
sigintercept = sqrt(1/sum2);
grad = sum3/sum4;
siggrad = sqrt(1/sum4);
mop = grad;
sigmop = siggrad;
cop = intercept;
sigcop = sigintercept;
}
/* to do a multiline comment start with the slash
and star then
end the comment with a star and slash
*/
// alternatively just put double
// slash on each commented line
int main ()
{
float *x,*y,*sig;
int np;
long m,c;
// specify parameters of test (number of points intercept, gradient etc)
np = 5;
c = 2.0;
m = 1.0;
/* the above allows you to declare arrays and allocate them later in
the program (dynamic allocation)
*/
x = new float [np];
y = new float [np];
sig = new float [np];
// just test out how to print numbers and assign values to arrays
for( int i = 0; i < np; i = i + 1 ) {
x[i] = 1.0*float(i);
sig[i] = 1.0;
//cout << x[i] << endl;
//cout << "value of i: " << i << endl;
}
/* now set y values as randomly distributed about straight line with scatter
appropriate for error bars
*/
for( int i = 0; i < np; i = i + 1 ) {
y[i] = m*x[i] + c;
y[i] = ((double) rand() / (RAND_MAX))*sig[i] + y[i];
cout << "x and y (post random) " << x[i] << ' ' << y[i] << endl;
}
}
// end of main body of code now define all functions below including the
// average of an array and least squares