Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gsl_try_2.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <gsl/gsl_rng.h>
61 #include <gsl/gsl_randist.h>
62 #include <gsl/gsl_matrix.h>
63 #include <gsl/gsl_vector.h>
64 #include <gsl/gsl_blas.h>
65 #include <gsl/gsl_multifit_nlinear.h>
66 
67 #define N 18 /* number of data points to fit */
68 #define TMAX (40.0) /* time variable in [0,TMAX] */
69 
70 struct INDATA {
71  double x;
72  double y;
73  double wt;
74 };
75 
76 struct INDATA indata[] = {
77  { (96740.0-96739.4), 1.039670408E-02, 1.0 },
78  { (96740.2-96739.4), 1.489661168E-02, 1.0 },
79  { (96740.3-96739.4), 5.147212651E-03, 1.0 },
80  { (96740.5-96739.4), 7.694352418E-03, 1.0 },
81  { (96740.7-96739.4), 2.027618140E-02, 1.0 },
82  { (96740.8-96739.4), 3.102330491E-02, 1.0 },
83  { (96741.0-96739.4), 6.541901827E-02, 1.0 },
84  { (96741.1-96739.4), 1.259476393E-01, 1.0 },
85  { (96741.3-96739.4), 1.189766824E-01, 1.0 },
86  { (96741.4-96739.4), 6.505819410E-02, 1.0 },
87  { (96741.6-96739.4), 4.259039834E-02, 1.0 },
88  { (96741.7-96739.4), 3.023725376E-02, 1.0 },
89  { (96741.9-96739.4), 1.395332254E-02, 1.0 },
90  { (96742.1-96739.4), 1.104300097E-02, 1.0 },
91  { (96742.2-96739.4), 4.119268619E-03, 1.0 },
92  { (96742.4-96739.4), 3.464561305E-04, 1.0 },
93  { (96742.5-96739.4), -3.346168669E-03, 1.0 },
94  { (96742.7-96739.4), 1.483750762E-03, 1.0 }
95 };
96 
97 
98 struct data {
99  size_t n;
100  double * t;
101  double * y;
102 };
103 
104 int
105 expb_f (const gsl_vector * x, void *data,
106  gsl_vector * f)
107 {
108  size_t n = ((struct data *)data)->n;
109  double *t = ((struct data *)data)->t;
110  double *y = ((struct data *)data)->y;
111 
112  double A = gsl_vector_get (x, 0);
113  double lambda = gsl_vector_get (x, 1);
114  double b = gsl_vector_get (x, 2);
115 
116  size_t i;
117 
118  for (i = 0; i < n; i++)
119  {
120  /* Model Yi = A * exp(-lambda * t_i) + b */
121  double Yi = A * exp (-lambda * t[i]) + b;
122  gsl_vector_set (f, i, Yi - y[i]);
123  }
124 
125  return GSL_SUCCESS;
126 }
127 
128 int
129 expb_df (const gsl_vector * x, void *data,
130  gsl_matrix * J)
131 {
132  size_t n = ((struct data *)data)->n;
133  double *t = ((struct data *)data)->t;
134 
135  double A = gsl_vector_get (x, 0);
136  double lambda = gsl_vector_get (x, 1);
137 
138  size_t i;
139 
140  for (i = 0; i < n; i++)
141  {
142  /* Jacobian matrix J(i,j) = dfi / dxj, */
143  /* where fi = (Yi - yi)/sigma[i], */
144  /* Yi = A * exp(-lambda * t_i) + b */
145  /* and the xj are the parameters (A,lambda,b) */
146  double e = exp(-lambda * t[i]);
147  gsl_matrix_set (J, i, 0, e);
148  gsl_matrix_set (J, i, 1, -t[i] * A * e);
149  gsl_matrix_set (J, i, 2, 1.0);
150  }
151 
152  return GSL_SUCCESS;
153 }
154 
155 void
156 callback(const size_t iter, void *params,
157  const gsl_multifit_nlinear_workspace *w)
158 {
159  gsl_vector *f = gsl_multifit_nlinear_residual(w);
160  gsl_vector *x = gsl_multifit_nlinear_position(w);
161  double rcond;
162 
163  /* compute reciprocal condition number of J(x) */
164  gsl_multifit_nlinear_rcond(&rcond, w);
165 
166  fprintf(stderr, "iter %2zu: A = %.4f, lambda = %.4f, b = %.4f, cond(J) = %8.4f, |f(x)| = %.4f\n",
167  iter,
168  gsl_vector_get(x, 0),
169  gsl_vector_get(x, 1),
170  gsl_vector_get(x, 2),
171  1.0 / rcond,
172  gsl_blas_dnrm2(f));
173 }
174 
175 int
176 main (void)
177 {
178  const gsl_multifit_nlinear_type *T = gsl_multifit_nlinear_trust;
179  gsl_multifit_nlinear_workspace *w;
180  gsl_multifit_nlinear_fdf fdf;
181  gsl_multifit_nlinear_parameters fdf_params =
182  gsl_multifit_nlinear_default_parameters();
183  const size_t n = N;
184  const size_t p = 3;
185 
186  gsl_vector *f;
187  gsl_matrix *J;
188  gsl_matrix *covar = gsl_matrix_alloc (p, p);
189  double t[N], y[N], weights[N];
190  struct data d = { n, t, y };
191  double x_init[3] = { 1.0, 1.0, 0.0 }; /* starting values */
192 // double x_init[3] = { 0.125, 1.9, 0.78125 }; /* starting values */
193  gsl_vector_view x = gsl_vector_view_array (x_init, p);
194  gsl_vector_view wts = gsl_vector_view_array(weights, n);
195  gsl_rng * r;
196  double chisq, chisq0;
197  int status, info;
198  size_t i;
199 
200  const double xtol = 1e-8;
201  const double gtol = 1e-8;
202  const double ftol = 0.0;
203 
204  gsl_rng_env_setup();
205  r = gsl_rng_alloc(gsl_rng_default);
206 
207  /* define the function to be minimized */
208  fdf.f = expb_f;
209  fdf.df = expb_df; /* set to NULL for finite-difference Jacobian */
210  fdf.fvv = NULL; /* not using geodesic acceleration */
211  fdf.n = n;
212  fdf.p = p;
213  fdf.params = &d;
214 
215  /* this is the data to be fitted */
216 // for (i = 0; i < n; i++)
217 // {
218 // double ti = i * TMAX / (n - 1.0);
219 // double yi = 1.0 + 5 * exp (-0.1 * ti);
220 // double si = 0.1 * yi;
221 // double dy = gsl_ran_gaussian(r, si);
222 //
223 // t[i] = ti;
224 // y[i] = yi + dy;
225 // weights[i] = 1.0 / (si * si);
226 // printf ("data: %g %g %g\n", ti, y[i], si);
227 // };
228 
229  for(i=0;i<n;i++)
230  {
231  t[i] = indata[i].x;
232  y[i] = indata[i].y;
233  weights[i] = indata[i].wt;
234 
235  printf ("data: %g %g %g\n", t[i], y[i], weights[i]);
236  }
237 
238  /* allocate workspace with default parameters */
239  w = gsl_multifit_nlinear_alloc (T, &fdf_params, n, p);
240 
241  /* initialize solver with starting point and weights */
242  gsl_multifit_nlinear_winit (&x.vector, &wts.vector, &fdf, w);
243 
244  /* compute initial cost function */
245  f = gsl_multifit_nlinear_residual(w);
246  gsl_blas_ddot(f, f, &chisq0);
247 
248  /* solve the system with a maximum of 100 iterations */
249  status = gsl_multifit_nlinear_driver(100, xtol, gtol, ftol,
250  callback, NULL, &info, w);
251 
252  /* compute covariance of best fit parameters */
253  J = gsl_multifit_nlinear_jac(w);
254  gsl_multifit_nlinear_covar (J, 0.0, covar);
255 
256  /* compute final cost */
257  gsl_blas_ddot(f, f, &chisq);
258 
259 #define FIT(i) gsl_vector_get(w->x, i)
260 #define ERR(i) sqrt(gsl_matrix_get(covar,i,i))
261 
262  fprintf(stderr, "summary from method '%s/%s'\n",
263  gsl_multifit_nlinear_name(w),
264  gsl_multifit_nlinear_trs_name(w));
265  fprintf(stderr, "number of iterations: %zu\n",
266  gsl_multifit_nlinear_niter(w));
267  fprintf(stderr, "function evaluations: %zu\n", fdf.nevalf);
268  fprintf(stderr, "Jacobian evaluations: %zu\n", fdf.nevaldf);
269  fprintf(stderr, "reason for stopping: %s\n",
270  (info == 1) ? "small step size" : "small gradient");
271  fprintf(stderr, "initial |f(x)| = %f\n", sqrt(chisq0));
272  fprintf(stderr, "final |f(x)| = %f\n", sqrt(chisq));
273 
274  {
275  double dof = n - p;
276  double c = GSL_MAX_DBL(1, sqrt(chisq / dof));
277 
278  fprintf(stderr, "chisq/dof = %g\n", chisq / dof);
279 
280  fprintf (stderr, "A = %.5f +/- %.5f\n", FIT(0), c*ERR(0));
281  fprintf (stderr, "lambda = %.5f +/- %.5f\n", FIT(1), c*ERR(1));
282  fprintf (stderr, "b = %.5f +/- %.5f\n", FIT(2), c*ERR(2));
283  }
284 
285  fprintf (stderr, "status = %s\n", gsl_strerror (status));
286 
287  gsl_multifit_nlinear_free (w);
288  gsl_matrix_free (covar);
289  gsl_rng_free (r);
290 
291  return 0;
292 }
int main(void)
Definition: gsl_try_2.c:176
double * y
Definition: gaussian_fit.c:64
size_t n
Definition: gaussian_fit.c:65
double y
Definition: gsl_try_2.c:72
struct INDATA indata[]
Definition: gsl_try_2.c:76
double x
Definition: gsl_try_2.c:71
int expb_f(const gsl_vector *x, void *data, gsl_vector *f)
Definition: gsl_try_2.c:105
#define N
Definition: gsl_try_2.c:67
double * t
Definition: gaussian_fit.c:63
#define ERR(i)
#define FIT(i)
int expb_df(const gsl_vector *x, void *data, gsl_matrix *J)
Definition: gsl_try_2.c:129
void callback(const size_t iter, void *params, const gsl_multifit_nlinear_workspace *w)
Definition: gaussian_fit.c:158
double wt
Definition: gsl_try_2.c:73