4 #include <gsl/gsl_multiroots.h>
8 #define X(i) gsl_vector_get(x,i)
10 for (j=0; j<N; j++) gsl_matrix_set(f,i,j,J_COL[j]);
12 static int cb_f(const gsl_vector *x, void *params, gsl_vector *f) {
13 double *target = params;
19 for (i=0; i<N; i++) gsl_vector_set(f,i, F(i) - target[i]);
23 static int cb_df(const gsl_vector *x, void *params, gsl_matrix *J) {
30 static int cb_fdf(const gsl_vector *x, void *params,
31 gsl_vector *f, gsl_matrix *J) {
36 static double *scan1double(void) {
41 if (ferror(stdin)) { perror("stdin"); exit(-1); }
42 if (feof(stdin)) exit(0);
43 if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
47 static int main(void) {
48 double target[N], epsilon;
50 gsl_multiroot_function_fdf function;
53 function.fdf = cb_fdf;
55 function.params = target;
57 gsl_vector *startpoint = gsl_vector_alloc(N);
59 gsl_multiroot_fdfsolver *solver
60 = gsl_multiroot_fdfsolver_alloc(gsl_multiroot_fdfsolver_hybrid, N);
63 /* 2N+1 doubles: target, initial guess, epsilon for residual */
65 target[i] = scan1double();
67 gsl_vector_set(startpoint,i,scan1double());
68 epsilon = scan1double();
70 gsl_multiroot_fdfsolver_set(solver, function, startpoint);
73 r = gsl_multiroot_fdfsolver_iterate(solver);
76 r = gsl_multiroot_test_residual(solver->f, epsilon);
77 if (r != GSL_CONTINUE) break;
81 fprintf(stderr,"ERROR %s\n",gsl_strerror(gsl_errno));