-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonlinear.cpp
More file actions
171 lines (128 loc) · 2.84 KB
/
nonlinear.cpp
File metadata and controls
171 lines (128 loc) · 2.84 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
#include "nonlinear.h"
#include <stdio.h>
static const unsigned short N_1 = N+1;
static void contract(Vector[N_1], unsigned short);
static Vector reflect(Vector[N_1], unsigned short, double, bool);
Vector Simplex::minimize(Data *data)
{
Vector amoeba[N_1], test, second_test;
double eval[N_1], eval_test, factor, second_eval;
unsigned short i,j,hi,low, second;
bool redo;
//set initial guess
for (i=0; i<N; i++)
amoeba[N][i] = 1;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
{
if (j==i)
amoeba[i][j] = amoeba[N][j] + LAMBDA;
else
amoeba[i][j] = amoeba[N][j];
}
for (i=0; i<N_1; i++)
eval[i] = data->chi2(amoeba[i]);
for(;;)
{
if (redo == YES)
for (i=0; i<N_1; i++)
eval[i] = data->chi2(amoeba[i]);
for (i=hi=low=0; i<N_1; i++)
{
if (eval[i] > eval[hi])
hi = i;
if (eval[i] < eval[low])
low = i;
}
for (i=0; i<N_1; i++)
if (i!=low && i!=hi)
{
second = i;
break;
}
for (i=second; i<N_1; i++)
if (i!=low && i!=hi && eval[i] > eval[second])
second = i;
if (eval[hi] - eval[low] < TOL)
return amoeba[low];
test = reflect(amoeba,hi,1,YES);
eval_test = data->chi2(test);
redo = NO;
if (eval_test < eval[low])
{
second_test = reflect(amoeba,hi,1.5,NO);
second_eval = data->chi2(second_test);
if (second_eval < eval_test)
{
amoeba[hi] = second_test;
eval[hi] = second_eval;
continue;
}
}
else if (eval_test > eval[second])
{
test = reflect(amoeba,hi,.5,NO);
eval_test = data->chi2(test);
if (eval_test > eval[hi])
{
contract(amoeba,low);
redo = YES;
continue;
}
}
amoeba[hi] = test;
eval[hi] = eval_test;
}
}
static Vector reflect(Vector amoeba[N_1], unsigned short point, double mag, bool redo)
{
static Vector average;
if (redo == YES)
{
average = 0;
for (unsigned short i=0; i<N_1; i++)
if (i != point)
average = average + amoeba[i];
average = average / N;
}
return (average - amoeba[point])*mag + average;
}
static void contract(Vector amoeba[N_1], unsigned short low)
{
for (unsigned short i=0; i<N_1; i++)
if (i != low)
amoeba[i] = (amoeba[i] + amoeba[low])/2;
}
Simplex::Simplex()
{
LAMBDA = 1e-2;
TOL = 1e-5;
}
Simplex::Simplex(double tol, double lambda)
{
LAMBDA = lambda;
TOL = tol;
}
double Data::chi2(Vector param)
{
double result = 0, diff;
for (unsigned long i=0; i<Npts; i++)
{
diff = func(param,x[i]) - y[i];
result += diff*diff;
}
return result;
}
char *number::operator () ()
{
static char string[100];
sprintf(string,"%f%c%f\0",val,plus_minus,err);
return string;
}
number make_number(double _val, double _err)
{
number result;
result.val = _val;
result.err = _err;
return result;
}