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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/* roots/test_funcs.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <math.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.h>
#include "test.h"
gsl_function create_function (double (*f)(double, void *))
{
gsl_function F ;
F.function = f;
F.params = 0;
return F ;
}
gsl_function_fdf create_fdf (double (*f)(double, void *),
double (*df)(double, void *),
void (*fdf)(double, void *, double *, double *))
{
gsl_function_fdf FDF ;
FDF.f = f ;
FDF.df = df ;
FDF.fdf = fdf ;
FDF.params = 0 ;
return FDF ;
}
/* f(x) = x^{20} - 1 */
/* f'(x) = 20x^{19} */
/* zero at x = 1 or -1 */
double
func1 (double x, void *p)
{
return pow (x, 20.0) - 1;
}
double
func1_df (double x, void * p)
{
return 20.0 * pow (x, 19.0);
}
void
func1_fdf (double x, void * p, double *y, double *yprime)
{
*y = func1 (x, p);
*yprime = 20.0 * pow (x, 19.0);
}
/* f(x) = sqrt(abs(x))*sgn(x) */
/* f'(x) = 1 / sqrt(abs(x) */
/* zero at x = 0 */
double
func2 (double x, void * p)
{
double delta;
if (x > 0)
delta = 1.0;
else if (x < 0)
delta = -1.0;
else
delta = 0.0;
return sqrt (fabs (x)) * delta;
}
double
func2_df (double x, void * p)
{
return 1 / sqrt (fabs (x));
}
void
func2_fdf (double x, void * p, double *y, double *yprime)
{
*y = func2 (x, p);
*yprime = 1 / sqrt (fabs (x));
}
/* f(x) = x^2 - 1e-8 */
/* f'(x) = 2x */
/* zero at x = sqrt(1e-8) or -sqrt(1e-8) */
double
func3 (double x, void * p)
{
return pow (x, 2.0) - 1e-8;
}
double
func3_df (double x, void * p)
{
return 2 * x;
}
void
func3_fdf (double x, void * p, double *y, double *yprime)
{
*y = func3 (x, p);
*yprime = 2 * x;
}
/* f(x) = x exp(-x) */
/* f'(x) = exp(-x) - x exp(-x) */
/* zero at x = 0 */
double
func4 (double x, void * p)
{
return x * exp (-x);
}
double
func4_df (double x, void * p)
{
return exp (-x) - x * exp (-x);
}
void
func4_fdf (double x, void * p, double *y, double *yprime)
{
*y = func4 (x, p);
*yprime = exp (-x) - x * exp (-x);
}
/* f(x) = 1/(1+exp(x)) */
/* f'(x) = -exp(x) / (1 + exp(x))^2 */
/* no roots! */
double
func5 (double x, void * p)
{
return 1 / (1 + exp (x));
}
double
func5_df (double x, void * p)
{
return -exp (x) / pow (1 + exp (x), 2.0);
}
void
func5_fdf (double x, void * p, double *y, double *yprime)
{
*y = func5 (x, p);
*yprime = -exp (x) / pow (1 + exp (x), 2.0);
}
/* f(x) = (x - 1)^7 */
/* f'(x) = 7 * (x - 1)^6 */
/* zero at x = 1 */
double
func6 (double x, void * p)
{
return pow (x - 1, 7.0);
}
double
func6_df (double x, void * p)
{
return 7.0 * pow (x - 1, 6.0);
}
void
func6_fdf (double x, void * p, double *y, double *yprime)
{
*y = func6 (x, p);
*yprime = 7.0 * pow (x - 1, 6.0);
}
/* sin(x) packaged up nicely. */
double
sin_f (double x, void * p)
{
return sin (x);
}
double
sin_df (double x, void * p)
{
return cos (x);
}
void
sin_fdf (double x, void * p, double *y, double *yprime)
{
*y = sin (x);
*yprime = cos (x);
}
/* cos(x) packaged up nicely. */
double
cos_f (double x, void * p)
{
return cos (x);
}
double
cos_df (double x, void * p)
{
return -sin (x);
}
void
cos_fdf (double x, void * p, double *y, double *yprime)
{
*y = cos (x);
*yprime = -sin (x);
}
|