File: Numerical-integration-examples.html

package info (click to toggle)
gsl-ref-html 1.10-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,496 kB
  • ctags: 2,955
  • sloc: makefile: 33
file content (114 lines) | stat: -rw-r--r-- 4,878 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
<html lang="en">
<head>
<title>Numerical integration examples - 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.8">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Numerical-Integration.html" title="Numerical Integration">
<link rel="prev" href="Numerical-integration-error-codes.html" title="Numerical integration error codes">
<link rel="next" href="Numerical-integration-References-and-Further-Reading.html" title="Numerical integration References and Further Reading">
<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 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.2 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 freedom to copy and modify this
GNU Manual, like GNU software.''-->
<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="Numerical-integration-examples"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Numerical-integration-References-and-Further-Reading.html">Numerical integration References and Further Reading</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Numerical-integration-error-codes.html">Numerical integration error codes</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Numerical-Integration.html">Numerical Integration</a>
<hr>
</div>

<h3 class="section">16.12 Examples</h3>

<p>The integrator <code>QAGS</code> will handle a large class of definite
integrals.  For example, consider the following integral, which has a
algebraic-logarithmic singularity at the origin,

<pre class="example">     \int_0^1 x^{-1/2} log(x) dx = -4
</pre>
   <p class="noindent">The program below computes this integral to a relative accuracy bound of
<code>1e-7</code>.

<pre class="example"><pre class="verbatim">     #include &lt;stdio.h>
     #include &lt;math.h>
     #include &lt;gsl/gsl_integration.h>
     
     double f (double x, void * params) {
       double alpha = *(double *) params;
       double f = log(alpha*x) / sqrt(x);
       return f;
     }
     
     int
     main (void)
     {
       gsl_integration_workspace * w 
         = gsl_integration_workspace_alloc (1000);
       
       double result, error;
       double expected = -4.0;
       double alpha = 1.0;
     
       gsl_function F;
       F.function = &amp;f;
       F.params = &amp;alpha;
     
       gsl_integration_qags (&amp;F, 0, 1, 0, 1e-7, 1000,
                             w, &amp;result, &amp;error); 
     
       printf ("result          = % .18f\n", result);
       printf ("exact result    = % .18f\n", expected);
       printf ("estimated error = % .18f\n", error);
       printf ("actual error    = % .18f\n", result - expected);
       printf ("intervals =  %d\n", w->size);
     
       gsl_integration_workspace_free (w);
     
       return 0;
     }
</pre></pre>
   <p class="noindent">The results below show that the desired accuracy is achieved after 8
subdivisions.

<pre class="example">     $ ./a.out
<pre class="verbatim">     result          = -3.999999999999973799
     exact result    = -4.000000000000000000
     estimated error =  0.000000000000246025
     actual error    =  0.000000000000026201
     intervals =  8
</pre></pre>
   <p class="noindent">In fact, the extrapolation procedure used by <code>QAGS</code> produces an
accuracy of almost twice as many digits.  The error estimate returned by
the extrapolation procedure is larger than the actual error, giving a
margin of safety of one order of magnitude.

<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>