File: Providing-the-function-to-solve.html

package info (click to toggle)
gsl-ref-html 1.14-1
  • links: PTS
  • area: non-free
  • in suites: squeeze
  • size: 4,628 kB
  • ctags: 3,088
  • sloc: makefile: 33
file content (186 lines) | stat: -rw-r--r-- 7,638 bytes parent folder | download
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
<html lang="en">
<head>
<title>Providing the function to solve - GNU Scientific Library -- Reference Manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Scientific Library -- Reference Manual">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="One-dimensional-Root_002dFinding.html" title="One dimensional Root-Finding">
<link rel="prev" href="Initializing-the-Solver.html" title="Initializing the Solver">
<link rel="next" href="Search-Bounds-and-Guesses.html" title="Search Bounds and Guesses">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The GSL Team.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``GNU General Public License'' and ``Free Software
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
and with the Back-Cover Text being (a) (see below).  A copy of the
license is included in the section entitled ``GNU Free Documentation
License''.

(a) The Back-Cover Text is: ``You have the freedom to copy and modify this
GNU Manual.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Providing-the-function-to-solve"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Search-Bounds-and-Guesses.html">Search Bounds and Guesses</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Initializing-the-Solver.html">Initializing the Solver</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="One-dimensional-Root_002dFinding.html">One dimensional Root-Finding</a>
<hr>
</div>

<h3 class="section">33.4 Providing the function to solve</h3>

<p><a name="index-root-finding_002c-providing-a-function-to-solve-2360"></a>
You must provide a continuous function of one variable for the root
finders to operate on, and, sometimes, its first derivative.  In order
to allow for general parameters the functions are defined by the
following data types:

<div class="defun">
&mdash; Data Type: <b>gsl_function</b><var><a name="index-gsl_005ffunction-2361"></a></var><br>
<blockquote><p>This data type defines a general function with parameters.

          <dl>
<dt><code>double (* function) (double </code><var>x</var><code>, void * </code><var>params</var><code>)</code><dd>this function should return the value
<!-- {$f(x,\hbox{\it params})$} -->
f(x,params) for argument <var>x</var> and parameters <var>params</var>

          <br><dt><code>void * params</code><dd>a pointer to the parameters of the function
</dl>
        </p></blockquote></div>

   <p>Here is an example for the general quadratic function,

<pre class="example">     f(x) = a x^2 + b x + c
</pre>
   <p class="noindent">with a = 3, b = 2, c = 1.  The following code
defines a <code>gsl_function</code> <code>F</code> which you could pass to a root
finder:

<pre class="example">     struct my_f_params { double a; double b; double c; };
     
     double
     my_f (double x, void * p) {
        struct my_f_params * params
          = (struct my_f_params *)p;
        double a = (params-&gt;a);
        double b = (params-&gt;b);
        double c = (params-&gt;c);
     
        return  (a * x + b) * x + c;
     }
     
     gsl_function F;
     struct my_f_params params = { 3.0, 2.0, 1.0 };
     
     F.function = &amp;my_f;
     F.params = &amp;params;
</pre>
   <p class="noindent">The function f(x) can be evaluated using the following macro,

<pre class="example">     #define GSL_FN_EVAL(F,x)
         (*((F)-&gt;function))(x,(F)-&gt;params)
</pre>
   <div class="defun">
&mdash; Data Type: <b>gsl_function_fdf</b><var><a name="index-gsl_005ffunction_005ffdf-2362"></a></var><br>
<blockquote><p>This data type defines a general function with parameters and its first
derivative.

          <dl>
<dt><code>double (* f) (double </code><var>x</var><code>, void * </code><var>params</var><code>)</code><dd>this function should return the value of
<!-- {$f(x,\hbox{\it params})$} -->
f(x,params) for argument <var>x</var> and parameters <var>params</var>

          <br><dt><code>double (* df) (double </code><var>x</var><code>, void * </code><var>params</var><code>)</code><dd>this function should return the value of the derivative of <var>f</var> with
respect to <var>x</var>,
<!-- {$f'(x,\hbox{\it params})$} -->
f'(x,params), for argument <var>x</var> and parameters <var>params</var>

          <br><dt><code>void (* fdf) (double </code><var>x</var><code>, void * </code><var>params</var><code>, double * </code><var>f</var><code>, double * </code><var>df</var><code>)</code><dd>this function should set the values of the function <var>f</var> to
<!-- {$f(x,\hbox{\it params})$} -->
f(x,params)
and its derivative <var>df</var> to
<!-- {$f'(x,\hbox{\it params})$} -->
f'(x,params)
for argument <var>x</var> and parameters <var>params</var>.  This function
provides an optimization of the separate functions for f(x) and
f'(x)&mdash;it is always faster to compute the function and its
derivative at the same time.

          <br><dt><code>void * params</code><dd>a pointer to the parameters of the function
</dl>
        </p></blockquote></div>

   <p>Here is an example where
<!-- {$f(x) = \exp(2x)$} -->
f(x) = 2\exp(2x):

<pre class="example">     double
     my_f (double x, void * params)
     {
        return exp (2 * x);
     }
     
     double
     my_df (double x, void * params)
     {
        return 2 * exp (2 * x);
     }
     
     void
     my_fdf (double x, void * params,
             double * f, double * df)
     {
        double t = exp (2 * x);
     
        *f = t;
        *df = 2 * t;   /* uses existing value */
     }
     
     gsl_function_fdf FDF;
     
     FDF.f = &amp;my_f;
     FDF.df = &amp;my_df;
     FDF.fdf = &amp;my_fdf;
     FDF.params = 0;
</pre>
   <p class="noindent">The function f(x) can be evaluated using the following macro,

<pre class="example">     #define GSL_FN_FDF_EVAL_F(FDF,x)
          (*((FDF)-&gt;f))(x,(FDF)-&gt;params)
</pre>
   <p class="noindent">The derivative f'(x) can be evaluated using the following macro,

<pre class="example">     #define GSL_FN_FDF_EVAL_DF(FDF,x)
          (*((FDF)-&gt;df))(x,(FDF)-&gt;params)
</pre>
   <p class="noindent">and both the function y = f(x) and its derivative dy = f'(x)
can be evaluated at the same time using the following macro,

<pre class="example">     #define GSL_FN_FDF_EVAL_F_DF(FDF,x,y,dy)
          (*((FDF)-&gt;fdf))(x,(FDF)-&gt;params,(y),(dy))
</pre>
   <p class="noindent">The macro stores f(x) in its <var>y</var> argument and f'(x) in
its <var>dy</var> argument&mdash;both of these should be pointers to
<code>double</code>.

<hr>The GNU Scientific Library - a free numerical library licensed under the GNU GPL<br>Back to the <a href="/software/gsl/">GNU Scientific Library Homepage</a></body></html>