3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_roots.h>
28 gsl_function create_function (double (*f)(double, void *))
36 gsl_function_fdf create_fdf (double (*f)(double, void *),
37 double (*df)(double, void *),
38 void (*fdf)(double, void *, double *, double *))
40 gsl_function_fdf FDF ;
48 /* f(x) = x^{20} - 1 */
49 /* f'(x) = 20x^{19} */
50 /* zero at x = 1 or -1 */
53 func1 (double x, void *p)
55 return pow (x, 20.0) - 1;
59 func1_df (double x, void * p)
61 return 20.0 * pow (x, 19.0);
65 func1_fdf (double x, void * p, double *y, double *yprime)
68 *yprime = 20.0 * pow (x, 19.0);
71 /* f(x) = sqrt(abs(x))*sgn(x) */
72 /* f'(x) = 1 / sqrt(abs(x) */
75 func2 (double x, void * p)
86 return sqrt (fabs (x)) * delta;
90 func2_df (double x, void * p)
92 return 1 / sqrt (fabs (x));
96 func2_fdf (double x, void * p, double *y, double *yprime)
99 *yprime = 1 / sqrt (fabs (x));
103 /* f(x) = x^2 - 1e-8 */
105 /* zero at x = sqrt(1e-8) or -sqrt(1e-8) */
107 func3 (double x, void * p)
109 return pow (x, 2.0) - 1e-8;
113 func3_df (double x, void * p)
119 func3_fdf (double x, void * p, double *y, double *yprime)
125 /* f(x) = x exp(-x) */
126 /* f'(x) = exp(-x) - x exp(-x) */
129 func4 (double x, void * p)
135 func4_df (double x, void * p)
137 return exp (-x) - x * exp (-x);
141 func4_fdf (double x, void * p, double *y, double *yprime)
144 *yprime = exp (-x) - x * exp (-x);
147 /* f(x) = 1/(1+exp(x)) */
148 /* f'(x) = -exp(x) / (1 + exp(x))^2 */
151 func5 (double x, void * p)
153 return 1 / (1 + exp (x));
157 func5_df (double x, void * p)
159 return -exp (x) / pow (1 + exp (x), 2.0);
163 func5_fdf (double x, void * p, double *y, double *yprime)
166 *yprime = -exp (x) / pow (1 + exp (x), 2.0);
169 /* f(x) = (x - 1)^7 */
170 /* f'(x) = 7 * (x - 1)^6 */
173 func6 (double x, void * p)
175 return pow (x - 1, 7.0);
179 func6_df (double x, void * p)
181 return 7.0 * pow (x - 1, 6.0);
185 func6_fdf (double x, void * p, double *y, double *yprime)
188 *yprime = 7.0 * pow (x - 1, 6.0);
191 /* sin(x) packaged up nicely. */
193 sin_f (double x, void * p)
199 sin_df (double x, void * p)
205 sin_fdf (double x, void * p, double *y, double *yprime)
211 /* cos(x) packaged up nicely. */
213 cos_f (double x, void * p)
219 cos_df (double x, void * p)
225 cos_fdf (double x, void * p, double *y, double *yprime)