File: Functions-of-One-Variable.html

package info (click to toggle)
octave 7.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,464 kB
  • sloc: cpp: 332,823; ansic: 71,320; fortran: 20,963; objc: 8,562; sh: 8,115; yacc: 4,882; lex: 4,438; perl: 1,554; java: 1,366; awk: 1,257; makefile: 652; xml: 173
file content (727 lines) | stat: -rw-r--r-- 40,975 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Functions of One Variable (GNU Octave (version 7.3.0))</title>

<meta name="description" content="Functions of One Variable (GNU Octave (version 7.3.0))">
<meta name="keywords" content="Functions of One Variable (GNU Octave (version 7.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Numerical-Integration.html" rel="up" title="Numerical Integration">
<link href="Orthogonal-Collocation.html" rel="next" title="Orthogonal Collocation">
<style type="text/css">
<!--
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<div class="section" id="Functions-of-One-Variable">
<div class="header">
<p>
Next: <a href="Orthogonal-Collocation.html" accesskey="n" rel="next">Orthogonal Collocation</a>, Up: <a href="Numerical-Integration.html" accesskey="u" rel="up">Numerical Integration</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Functions-of-One-Variable-1"></span><h3 class="section">23.1 Functions of One Variable</h3>

<p>Octave supports five different adaptive quadrature algorithms for computing
the integral
of a function <em class='math'>f</em> over the interval from <em class='math'>a</em> to <em class='math'>b</em>.  These are
</p>
<dl compact="compact">
<dt><span><code>quad</code></span></dt>
<dd><p>Numerical integration based on Gaussian quadrature.
</p>
</dd>
<dt><span><code>quadv</code></span></dt>
<dd><p>Numerical integration using an adaptive vectorized Simpson&rsquo;s rule.
</p>
</dd>
<dt><span><code>quadl</code></span></dt>
<dd><p>Numerical integration using an adaptive Lobatto rule.
</p>
</dd>
<dt><span><code>quadgk</code></span></dt>
<dd><p>Numerical integration using an adaptive Gauss-Konrod rule.
</p>
</dd>
<dt><span><code>quadcc</code></span></dt>
<dd><p>Numerical integration using adaptive Clenshaw-Curtis rules.
</p>
<p>In addition, the following functions are also provided:
</p>
</dd>
<dt><span><code>integral</code></span></dt>
<dd><p>A compatibility wrapper function that will choose between <code>quadv</code> and
<code>quadgk</code> depending on the integrand and options chosen.
</p>
</dd>
<dt><span><code>trapz, cumtrapz</code></span></dt>
<dd><p>Numerical integration of data using the trapezoidal method.
</p></dd>
</dl>

<p>The best quadrature algorithm to use depends on the integrand.  If you have
empirical data, rather than a function, the choice is <code>trapz</code> or
<code>cumtrapz</code>.  If you are uncertain about the characteristics of the
integrand, <code>quadcc</code> will be the most robust as it can handle
discontinuities, singularities, oscillatory functions, and infinite intervals.
When the integrand is smooth <code>quadgk</code> may be the fastest of the
algorithms.
</p>
<table>
<thead><tr><th width="5%"></th><th width="15%">Function</th><th width="80%">Characteristics</th></tr></thead>
<tr><td width="5%"></td><td width="15%">quad</td><td width="80%">Low accuracy with nonsmooth integrands</td></tr>
<tr><td width="5%"></td><td width="15%">quadv</td><td width="80%">Medium accuracy with smooth integrands</td></tr>
<tr><td width="5%"></td><td width="15%">quadl</td><td width="80%">Medium accuracy with smooth integrands.  Slower than quadgk.</td></tr>
<tr><td width="5%"></td><td width="15%">quadgk</td><td width="80%">Medium accuracy (1e-6 &ndash; 1e-9) with smooth integrands.</td></tr>
<tr><td width="5%"></td><td width="15%"></td><td width="80%">Handles oscillatory functions and infinite bounds</td></tr>
<tr><td width="5%"></td><td width="15%">quadcc</td><td width="80%">Low to High accuracy with nonsmooth/smooth integrands</td></tr>
<tr><td width="5%"></td><td width="15%"></td><td width="80%">Handles oscillatory functions, singularities, and infinite bounds</td></tr>
</table>


<p>Here is an example of using <code>quad</code> to integrate the function
</p>
<div class="example">
<pre class="example">  <var>f</var>(<var>x</var>) = <var>x</var> * sin (1/<var>x</var>) * sqrt (abs (1 - <var>x</var>))
</pre></div>

<p>from <var>x</var> = 0 to <var>x</var> = 3.
</p>
<p>This is a fairly difficult integration (plot the function over the range
of integration to see why).
</p>
<p>The first step is to define the function:
</p>
<div class="example">
<pre class="example">function y = f (x)
  y = x .* sin (1./x) .* sqrt (abs (1 - x));
endfunction
</pre></div>

<p>Note the use of the &lsquo;dot&rsquo; forms of the operators.  This is not necessary for
the <code>quad</code> integrator, but is required by the other integrators.  In any
case, it makes it much easier to generate a set of points for plotting because
it is possible to call the function with a vector argument to produce a vector
result.
</p>
<p>The second step is to call quad with the limits of integration:
</p>
<div class="example">
<pre class="example">[q, ier, nfun, err] = quad (&quot;f&quot;, 0, 3)
     &rArr; 1.9819
     &rArr; 1
     &rArr; 5061
     &rArr; 1.1522e-07
</pre></div>

<p>Although <code>quad</code> returns a nonzero value for <var>ier</var>, the result
is reasonably accurate (to see why, examine what happens to the result
if you move the lower bound to 0.1, then 0.01, then 0.001, etc.).
</p>
<p>The function <code>&quot;f&quot;</code> can be the string name of a function or a
function handle.  These options make it quite easy to do integration
without having to fully define a function in an m-file.  For example:
</p>
<div class="example">
<pre class="example"># Verify gamma function = (n-1)! for n = 4
f = @(x) x.^3 .* exp (-x);
quadcc (f, 0, Inf)
     &rArr; 6.0000
</pre></div>

<span id="XREFquad"></span><dl class="def">
<dt id="index-quad"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quad</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>)</em><a href='#index-quad' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quad-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quad</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>)</em><a href='#index-quad-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quad-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quad</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>, <var>sing</var>)</em><a href='#index-quad-2' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quad-3"><span class="category">: </span><span><em>[<var>q</var>, <var>ier</var>, <var>nfun</var>, <var>err</var>] =</em> <strong>quad</strong> <em>(&hellip;)</em><a href='#index-quad-3' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Numerically evaluate the integral of <var>f</var> from <var>a</var> to <var>b</var> using
Fortran routines from <small>QUADPACK</small><!-- /@w -->.
</p>
<p><var>f</var> is a function handle, inline function, or a string containing the
name of the function to evaluate.  The function must have the form <code>y =
f (x)</code> where <var>y</var> and <var>x</var> are scalars.
</p>
<p><var>a</var> and <var>b</var> are the lower and upper limits of integration.  Either
or both may be infinite.
</p>
<p>The optional argument <var>tol</var> is a vector that specifies the desired
accuracy of the result.  The first element of the vector is the desired
absolute tolerance, and the second element is the desired relative
tolerance.  To choose a relative test only, set the absolute
tolerance to zero.  To choose an absolute test only, set the relative
tolerance to zero.  Both tolerances default to <code>sqrt (eps)</code> or
approximately 1.5e-8.
</p>
<p>The optional argument <var>sing</var> is a vector of values at which the
integrand is known to be singular.
</p>
<p>The result of the integration is returned in <var>q</var>.
</p>
<p><var>ier</var> contains an integer error code (0 indicates a successful
integration).
</p>
<p><var>nfun</var> indicates the number of function evaluations that were
made.
</p>
<p><var>err</var> contains an estimate of the error in the solution.
</p>
<p>The function <code>quad_options</code> can set other optional parameters for
<code>quad</code>.
</p>
<p>Note: because <code>quad</code> is written in Fortran it cannot be called
recursively.  This prevents its use in integrating over more than one
variable by routines <code>dblquad</code> and <code>triplequad</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFquad_005foptions">quad_options</a>, <a href="#XREFquadv">quadv</a>, <a href="#XREFquadl">quadl</a>, <a href="#XREFquadgk">quadgk</a>, <a href="#XREFquadcc">quadcc</a>, <a href="#XREFtrapz">trapz</a>, <a href="Functions-of-Multiple-Variables.html#XREFdblquad">dblquad</a>, <a href="Functions-of-Multiple-Variables.html#XREFtriplequad">triplequad</a>.
</p></dd></dl>


<span id="XREFquad_005foptions"></span><dl class="def">
<dt id="index-quad_005foptions"><span class="category">: </span><span><em></em> <strong>quad_options</strong> <em>()</em><a href='#index-quad_005foptions' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quad_005foptions-1"><span class="category">: </span><span><em>val =</em> <strong>quad_options</strong> <em>(<var>opt</var>)</em><a href='#index-quad_005foptions-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quad_005foptions-2"><span class="category">: </span><span><em></em> <strong>quad_options</strong> <em>(<var>opt</var>, <var>val</var>)</em><a href='#index-quad_005foptions-2' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Query or set options for the function <code>quad</code>.
</p>
<p>When called with no arguments, the names of all available options and
their current values are displayed.
</p>
<p>Given one argument, return the value of the option <var>opt</var>.
</p>
<p>When called with two arguments, <code>quad_options</code> sets the option
<var>opt</var> to value <var>val</var>.
</p>
<p>Options include
</p>
<dl compact="compact">
<dt><span><code>&quot;absolute tolerance&quot;</code></span></dt>
<dd><p>Absolute tolerance; may be zero for pure relative error test.
</p>
</dd>
<dt><span><code>&quot;relative tolerance&quot;</code></span></dt>
<dd><p>Non-negative relative tolerance.  If the absolute tolerance is zero,
the relative tolerance must be greater than or equal to
<code>max&nbsp;(50*eps,&nbsp;<span class="nolinebreak">0.5e-28)</span></code><!-- /@w -->.
</p>
</dd>
<dt><span><code>&quot;single precision absolute tolerance&quot;</code></span></dt>
<dd><p>Absolute tolerance for single precision; may be zero for pure relative
error test.
</p>
</dd>
<dt><span><code>&quot;single precision relative tolerance&quot;</code></span></dt>
<dd><p>Non-negative relative tolerance for single precision.  If the absolute
tolerance is zero, the relative tolerance must be greater than or equal to
<code>max&nbsp;(50*eps,&nbsp;<span class="nolinebreak">0.5e-28)</span></code><!-- /@w -->.
</p></dd>
</dl>
</dd></dl>


<span id="XREFquadv"></span><dl class="def">
<dt id="index-quadv"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadv</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>)</em><a href='#index-quadv' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadv-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadv</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>)</em><a href='#index-quadv-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadv-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadv</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>, <var>trace</var>)</em><a href='#index-quadv-2' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadv-3"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadv</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>, <var>trace</var>, <var>p1</var>, <var>p2</var>, &hellip;)</em><a href='#index-quadv-3' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadv-4"><span class="category">: </span><span><em>[<var>q</var>, <var>nfun</var>] =</em> <strong>quadv</strong> <em>(&hellip;)</em><a href='#index-quadv-4' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>Numerically evaluate the integral of <var>f</var> from <var>a</var> to <var>b</var>
using an adaptive Simpson&rsquo;s rule.
</p>
<p><var>f</var> is a function handle, inline function, or string containing the name
of the function to evaluate.  <code>quadv</code> is a vectorized version of
<code>quad</code> and the function defined by <var>f</var> must accept a scalar or
vector as input and return a scalar, vector, or array as output.
</p>
<p><var>a</var> and <var>b</var> are the lower and upper limits of integration.  Both
limits must be finite.
</p>
<p>The optional argument <var>tol</var> defines the absolute tolerance used to stop
the adaptation procedure.  The default value is 1e-6.
</p>
<p>The algorithm used by <code>quadv</code> involves recursively subdividing the
integration interval and applying Simpson&rsquo;s rule on each subinterval.
If <var>trace</var> is true then after computing each of these partial
integrals display: (1) the total number of function evaluations,
(2) the left end of the subinterval, (3) the length of the subinterval,
(4) the approximation of the integral over the subinterval.
</p>
<p>Additional arguments <var>p1</var>, etc., are passed directly to the function
<var>f</var>.  To use default values for <var>tol</var> and <var>trace</var>, one may pass
empty matrices ([]).
</p>
<p>The result of the integration is returned in <var>q</var>.
</p>
<p>The optional output <var>nfun</var> indicates the total number of function
evaluations performed.
</p>
<p>Note: <code>quadv</code> is written in Octave&rsquo;s scripting language and can be
used recursively in <code>dblquad</code> and <code>triplequad</code>, unlike the
<code>quad</code> function.
</p>
<p><strong>See also:</strong> <a href="#XREFquad">quad</a>, <a href="#XREFquadl">quadl</a>, <a href="#XREFquadgk">quadgk</a>, <a href="#XREFquadcc">quadcc</a>, <a href="#XREFtrapz">trapz</a>, <a href="Functions-of-Multiple-Variables.html#XREFdblquad">dblquad</a>, <a href="Functions-of-Multiple-Variables.html#XREFtriplequad">triplequad</a>, <a href="#XREFintegral">integral</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral2">integral2</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral3">integral3</a>.
</p></dd></dl>


<span id="XREFquadl"></span><dl class="def">
<dt id="index-quadl"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadl</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>)</em><a href='#index-quadl' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadl-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadl</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>)</em><a href='#index-quadl-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadl-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadl</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>, <var>trace</var>)</em><a href='#index-quadl-2' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadl-3"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadl</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>, <var>trace</var>, <var>p1</var>, <var>p2</var>, &hellip;)</em><a href='#index-quadl-3' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadl-4"><span class="category">: </span><span><em>[<var>q</var>, <var>nfun</var>] =</em> <strong>quadl</strong> <em>(&hellip;)</em><a href='#index-quadl-4' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>Numerically evaluate the integral of <var>f</var> from <var>a</var> to <var>b</var> using
an adaptive Lobatto rule.
</p>
<p><var>f</var> is a function handle, inline function, or string containing the name
of the function to evaluate.  The function <var>f</var> must be vectorized and
return a vector of output values when given a vector of input values.
</p>
<p><var>a</var> and <var>b</var> are the lower and upper limits of integration.  Both
limits must be finite.
</p>
<p>The optional argument <var>tol</var> defines the absolute tolerance with which
to perform the integration.  The default value is 1e-6.
</p>
<p>The algorithm used by <code>quadl</code> involves recursively subdividing the
integration interval.  If <var>trace</var> is defined then for each subinterval
display: (1) the total number of function evaluations, (2) the left end of
the subinterval, (3) the length of the subinterval, (4) the approximation of
the integral over the subinterval.
</p>
<p>Additional arguments <var>p1</var>, etc., are passed directly to the function
<var>f</var>.  To use default values for <var>tol</var> and <var>trace</var>, one may pass
empty matrices ([]).
</p>
<p>The result of the integration is returned in <var>q</var>.
</p>
<p>The optional output <var>nfun</var> indicates the total number of function
evaluations performed.
</p>
<p>Reference: W. Gander and W. Gautschi, <cite>Adaptive Quadrature -
Revisited</cite>, BIT Vol. 40, No. 1, March 2000, pp. 84&ndash;101.
<a href="https://www.inf.ethz.ch/personal/gander/">https://www.inf.ethz.ch/personal/gander/</a>
</p>
<p><strong>See also:</strong> <a href="#XREFquad">quad</a>, <a href="#XREFquadv">quadv</a>, <a href="#XREFquadgk">quadgk</a>, <a href="#XREFquadcc">quadcc</a>, <a href="#XREFtrapz">trapz</a>, <a href="Functions-of-Multiple-Variables.html#XREFdblquad">dblquad</a>, <a href="Functions-of-Multiple-Variables.html#XREFtriplequad">triplequad</a>, <a href="#XREFintegral">integral</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral2">integral2</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral3">integral3</a>.
</p></dd></dl>


<span id="XREFquadgk"></span><dl class="def">
<dt id="index-quadgk"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadgk</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>)</em><a href='#index-quadgk' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadgk-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadgk</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>abstol</var>)</em><a href='#index-quadgk-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadgk-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadgk</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>abstol</var>, <var>trace</var>)</em><a href='#index-quadgk-2' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadgk-3"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadgk</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>prop</var>, <var>val</var>, &hellip;)</em><a href='#index-quadgk-3' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadgk-4"><span class="category">: </span><span><em>[<var>q</var>, <var>err</var>] =</em> <strong>quadgk</strong> <em>(&hellip;)</em><a href='#index-quadgk-4' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>Numerically evaluate the integral of <var>f</var> from <var>a</var> to <var>b</var>
using adaptive Gauss-Kronrod quadrature.
</p>
<p><var>f</var> is a function handle, inline function, or string containing the name
of the function to evaluate.  The function <var>f</var> must be vectorized and
return a vector of output values when given a vector of input values.
</p>
<p><var>a</var> and <var>b</var> are the lower and upper limits of integration.  Either
or both limits may be infinite or contain weak end singularities.  Variable
transformation will be used to treat any infinite intervals and weaken the
singularities.  For example:
</p>
<div class="example">
<pre class="example">quadgk (@(x) 1 ./ (sqrt (x) .* (x + 1)), 0, Inf)
</pre></div>

<p>Note that the formulation of the integrand uses the element-by-element
operator <code>./</code> and all user functions to <code>quadgk</code> should do the
same.
</p>
<p>The optional argument <var>tol</var> defines the absolute tolerance used to stop
the integration procedure.  The default value is 1e-10 (1e-5 for single).
</p>
<p>The algorithm used by <code>quadgk</code> involves subdividing the integration
interval and evaluating each subinterval.  If <var>trace</var> is true then after
computing each of these partial integrals display: (1) the number of
subintervals at this step, (2) the current estimate of the error <var>err</var>,
(3) the current estimate for the integral <var>q</var>.
</p>
<p>The behavior of the algorithm can be configured by passing arguments
to <code>quadgk</code> as pairs <code>&quot;<var>prop</var>&quot;, <var>val</var></code>.  Valid properties
are
</p>
<dl compact="compact">
<dt><span><code>AbsTol</code></span></dt>
<dd><p>Define the absolute error tolerance for the quadrature.  The default
absolute tolerance is 1e-10 (1e-5 for single).
</p>
</dd>
<dt><span><code>RelTol</code></span></dt>
<dd><p>Define the relative error tolerance for the quadrature.  The default
relative tolerance is 1e-6 (1e-4 for single).
</p>
</dd>
<dt><span><code>MaxIntervalCount</code></span></dt>
<dd><p><code>quadgk</code> initially subdivides the interval on which to perform the
quadrature into 10 intervals.  Subintervals that have an unacceptable error
are subdivided and re-evaluated.  If the number of subintervals exceeds 650
subintervals at any point then a poor convergence is signaled and the
current estimate of the integral is returned.  The property
<code>&quot;MaxIntervalCount&quot;</code> can be used to alter the number of subintervals
that can exist before exiting.
</p>
</dd>
<dt><span><code>WayPoints</code></span></dt>
<dd><p>Discontinuities in the first derivative of the function to integrate can be
flagged with the <code>&quot;WayPoints&quot;</code> property.  This forces the ends of a
subinterval to fall on the breakpoints of the function and can result in
significantly improved estimation of the error in the integral, faster
computation, or both.  For example,
</p>
<div class="example">
<pre class="example">quadgk (@(x) abs (1 - x.^2), 0, 2, &quot;Waypoints&quot;, 1)
</pre></div>

<p>signals the breakpoint in the integrand at <code><var>x</var> = 1</code>.
</p>
</dd>
<dt><span><code>Trace</code></span></dt>
<dd><p>If logically true <code>quadgk</code> prints information on the convergence of the
quadrature at each iteration.
</p></dd>
</dl>

<p>If any of <var>a</var>, <var>b</var>, or <var>waypoints</var> is complex then the
quadrature is treated as a contour integral along a piecewise continuous
path defined by
<code>[<var>a</var>, <var>waypoints</var>(1), <var>waypoints</var>(2), &hellip;, <var>b</var>]</code>.
In this case the integral is assumed to have no edge singularities.  For
example,
</p>
<div class="example">
<pre class="example">quadgk (@(z) log (z), 1+1i, 1+1i, &quot;WayPoints&quot;,
        [-1+1i, -1-1i, +1-1i])
</pre></div>

<p>integrates <code>log (z)</code> along the square defined by
<code>[1+1i, -1+1i, -1-1i, +1-1i]</code>.
</p>
<p>The result of the integration is returned in <var>q</var>.
</p>
<p><var>err</var> is an approximate bound on the error in the integral
<code>abs&nbsp;(<var>q</var>&nbsp;<span class="nolinebreak">-</span>&nbsp;<var>I</var>)</code><!-- /@w -->, where <var>I</var> is the exact value of the
integral.
</p>
<p>Reference: L.F. Shampine,
<cite>&quot;Vectorized adaptive quadrature in <small>MATLAB</small>&quot;</cite>, Journal of
Computational and Applied Mathematics, pp. 131&ndash;140, Vol 211, Issue 2,
Feb 2008.
</p>

<p><strong>See also:</strong> <a href="#XREFquad">quad</a>, <a href="#XREFquadv">quadv</a>, <a href="#XREFquadl">quadl</a>, <a href="#XREFquadcc">quadcc</a>, <a href="#XREFtrapz">trapz</a>, <a href="Functions-of-Multiple-Variables.html#XREFdblquad">dblquad</a>, <a href="Functions-of-Multiple-Variables.html#XREFtriplequad">triplequad</a>, <a href="#XREFintegral">integral</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral2">integral2</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral3">integral3</a>.
</p></dd></dl>


<span id="XREFquadcc"></span><dl class="def">
<dt id="index-quadcc"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadcc</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>)</em><a href='#index-quadcc' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadcc-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadcc</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>)</em><a href='#index-quadcc-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadcc-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>quadcc</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>tol</var>, <var>sing</var>)</em><a href='#index-quadcc-2' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-quadcc-3"><span class="category">: </span><span><em>[<var>q</var>, <var>err</var>, <var>nr_points</var>] =</em> <strong>quadcc</strong> <em>(&hellip;)</em><a href='#index-quadcc-3' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Numerically evaluate the integral of <var>f</var> from <var>a</var> to <var>b</var> using
doubly-adaptive Clenshaw-Curtis quadrature.
</p>
<p><var>f</var> is a function handle, inline function, or string containing the name
of the function to evaluate.  The function <var>f</var> must be vectorized and
must return a vector of output values if given a vector of input values.
For example,
</p>
<div class="example">
<pre class="example">f = @(x) x .* sin (1./x) .* sqrt (abs (1 - x));
</pre></div>

<p>which uses the element-by-element &ldquo;dot&rdquo; form for all operators.
</p>
<p><var>a</var> and <var>b</var> are the lower and upper limits of integration.  Either or
both limits may be infinite.  <code>quadcc</code> handles an infinite limit by
substituting the variable of integration with <code>x = tan (pi/2*u)</code>.
</p>
<p>The optional argument <var>tol</var> is a 1- or 2-element vector that specifies the
desired accuracy of the result.  The first element of the vector is the desired
absolute tolerance, and the second element is the desired relative tolerance.
To choose a relative test only, set the absolute tolerance to zero.  To choose
an absolute test only, set the relative tolerance to zero.  The default
absolute tolerance is 1e-10 (1e-5 for single), and the default relative
tolerance is 1e-6 (1e-4 for single).
</p>
<p>The optional argument <var>sing</var> contains a list of points where the integrand
has known singularities, or discontinuities in any of its derivatives, inside
the integration interval.  For the example above, which has a discontinuity at
x=1, the call to <code>quadcc</code> would be as follows
</p>
<div class="example">
<pre class="example">int = quadcc (f, a, b, [], [ 1 ]);
</pre></div>

<p>The result of the integration is returned in <var>q</var>.
</p>
<p><var>err</var> is an estimate of the absolute integration error.
</p>
<p><var>nr_points</var> is the number of points at which the integrand was evaluated.
</p>
<p>If the adaptive integration did not converge, the value of <var>err</var> will be
larger than the requested tolerance.  Therefore, it is recommended to verify
this value for difficult integrands.
</p>
<p><code>quadcc</code> is capable of dealing with non-numeric values of the integrand
such as <code>NaN</code> or <code>Inf</code>.  If the integral diverges, and <code>quadcc</code>
detects this, then a warning is issued and <code>Inf</code> or <code>-Inf</code> is
returned.
</p>
<p>Note: <code>quadcc</code> is a general purpose quadrature algorithm and, as such,
may be less efficient for a smooth or otherwise well-behaved integrand than
other methods such as <code>quadgk</code>.
</p>
<p>The algorithm uses Clenshaw-Curtis quadrature rules of increasing
degree in each interval and bisects the interval if either the function does
not appear to be smooth or a rule of maximum degree has been reached.  The
error estimate is computed from the L2-norm of the difference between two
successive interpolations of the integrand over the nodes of the respective
quadrature rules.
</p>
<p>Reference: P. Gonnet, <cite>Increasing the Reliability of Adaptive
Quadrature Using Explicit Interpolants</cite>, ACM Transactions on
Mathematical Software, Vol. 37, Issue 3, Article No. 3, 2010.
</p>
<p><strong>See also:</strong> <a href="#XREFquad">quad</a>, <a href="#XREFquadv">quadv</a>, <a href="#XREFquadl">quadl</a>, <a href="#XREFquadgk">quadgk</a>, <a href="#XREFtrapz">trapz</a>, <a href="Functions-of-Multiple-Variables.html#XREFdblquad">dblquad</a>, <a href="Functions-of-Multiple-Variables.html#XREFtriplequad">triplequad</a>.
</p></dd></dl>


<span id="XREFintegral"></span><dl class="def">
<dt id="index-integral"><span class="category">: </span><span><em><var>q</var> =</em> <strong>integral</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>)</em><a href='#index-integral' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-integral-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>integral</strong> <em>(<var>f</var>, <var>a</var>, <var>b</var>, <var>prop</var>, <var>val</var>, &hellip;)</em><a href='#index-integral-1' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>Numerically evaluate the integral of <var>f</var> from <var>a</var> to <var>b</var> using
adaptive quadrature.
</p>
<p><code>integral</code> is a wrapper for <code>quadcc</code> (general real-valued, scalar
integrands and limits), <code>quadgk</code> (integrals with specified integration
paths), and <code>quadv</code> (array-valued integrands) that is intended to
provide <small>MATLAB</small> compatibility.  More control of the numerical
integration may be achievable by calling the various quadrature functions
directly.
</p>
<p><var>f</var> is a function handle, inline function, or string containing the name
of the function to evaluate.  The function <var>f</var> must be vectorized and
return a vector of output values when given a vector of input values.
</p>
<p><var>a</var> and <var>b</var> are the lower and upper limits of integration.  Either
or both limits may be infinite or contain weak end singularities.  If either
or both limits are complex, <code>integral</code> will perform a straight line
path integral.  Alternatively, a complex domain path can be specified using
the <code>&quot;Waypoints&quot;</code> option (see below).
</p>
<p>Additional optional parameters can be specified using
<code>&quot;<var>property</var>&quot;, <var>value</var></code> pairs.  Valid properties are:
</p>
<dl compact="compact">
<dt><span><code>Waypoints</code></span></dt>
<dd><p>Specifies points to be used in defining subintervals of the quadrature
algorithm, or if <var>a</var>, <var>b</var>, or <var>waypoints</var> are complex then
the quadrature is calculated as a contour integral along a piecewise
continuous path.  For more detail, see <a href="#XREFquadgk"><code>quadgk</code></a>.
</p>
</dd>
<dt><span><code>ArrayValued</code></span></dt>
<dd><p><code>integral</code> expects <var>f</var> to return a scalar value unless
<var>arrayvalued</var> is specified as true.  This option will cause
<code>integral</code> to perform the integration over the entire array and return
<var>q</var> with the same dimensions as returned by <var>f</var>.  For more detail
see <a href="#XREFquadv"><code>quadv</code></a>.
</p>
</dd>
<dt><span><code>AbsTol</code></span></dt>
<dd><p>Define the absolute error tolerance for the quadrature.  The default
absolute tolerance is 1e-10 (1e-5 for single).
</p>
</dd>
<dt><span><code>RelTol</code></span></dt>
<dd><p>Define the relative error tolerance for the quadrature.  The default
relative tolerance is 1e-6 (1e-4 for single).
</p></dd>
</dl>

<p>Adaptive quadrature is used to minimize the estimate of error until the
following is satisfied:
</p>
<div class="example">
<pre class="example">  <var>error</var> &lt;= max (<var>AbsTol</var>, <var>RelTol</var>*|<var>q</var>|).
</pre></div>


<p>Known <small>MATLAB</small> incompatibilities:
</p>
<ol>
<li> If tolerances are left unspecified, and any integration limits or waypoints
are of type <code>single</code>, then Octave&rsquo;s integral functions automatically
reduce the default absolute and relative error tolerances as specified
above.  If tighter tolerances are desired they must be specified.
<small>MATLAB</small> leaves the tighter tolerances appropriate for <code>double</code>
inputs in place regardless of the class of the integration limits.

</li><li> As a consequence of using <code>quadcc</code>, <code>quadgk</code>, and <code>quadv</code>,
certain option combinations are not supported.  Currently,
<code>&quot;ArrayValued&quot;</code> cannot be combined with <code>&quot;RelTol&quot;</code> or
<code>&quot;Waypoints&quot;</code>.
</li></ol>


<p><strong>See also:</strong> <a href="Functions-of-Multiple-Variables.html#XREFintegral2">integral2</a>, <a href="Functions-of-Multiple-Variables.html#XREFintegral3">integral3</a>, <a href="#XREFquad">quad</a>, <a href="#XREFquadgk">quadgk</a>, <a href="#XREFquadv">quadv</a>, <a href="#XREFquadl">quadl</a>, <a href="#XREFquadcc">quadcc</a>, <a href="#XREFtrapz">trapz</a>, <a href="Functions-of-Multiple-Variables.html#XREFdblquad">dblquad</a>, <a href="Functions-of-Multiple-Variables.html#XREFtriplequad">triplequad</a>.
</p></dd></dl>


<p>Sometimes one does not have the function, but only the raw (x, y) points from
which to perform an integration.  This can occur when collecting data in an
experiment.  The <code>trapz</code> function can integrate these values as shown in
the following example where &quot;data&quot; has been collected on the cosine function
over the range [0, pi/2).
</p>
<div class="example">
<pre class="example">x = 0:0.1:pi/2;  # Uniformly spaced points
y = cos (x);
trapz (x, y)
     &rArr; 0.99666
</pre></div>

<p>The answer is reasonably close to the exact value of 1.  Ordinary quadrature
is sensitive to the characteristics of the integrand.  Empirical integration
depends not just on the integrand, but also on the particular points chosen to
represent the function.  Repeating the example above with the sine function
over the range [0, pi/2) produces far inferior results.
</p>
<div class="example">
<pre class="example">x = 0:0.1:pi/2;  # Uniformly spaced points
y = sin (x);
trapz (x, y)
     &rArr; 0.92849
</pre></div>

<p>However, a slightly different choice of data points can change the result
significantly.  The same integration, with the same number of points, but
spaced differently produces a more accurate answer.
</p>
<div class="example">
<pre class="example">x = linspace (0, pi/2, 16);  # Uniformly spaced, but including endpoint
y = sin (x);
trapz (x, y)
     &rArr; 0.99909
</pre></div>

<p>In general there may be no way of knowing the best distribution of points ahead
of time.  Or the points may come from an experiment where there is no freedom
to select the best distribution.  In any case, one must remain aware of this
issue when using <code>trapz</code>.
</p>
<span id="XREFtrapz"></span><dl class="def">
<dt id="index-trapz"><span class="category">: </span><span><em><var>q</var> =</em> <strong>trapz</strong> <em>(<var>y</var>)</em><a href='#index-trapz' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-trapz-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>trapz</strong> <em>(<var>x</var>, <var>y</var>)</em><a href='#index-trapz-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-trapz-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>trapz</strong> <em>(&hellip;, <var>dim</var>)</em><a href='#index-trapz-2' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>Numerically evaluate the integral of points <var>y</var> using the trapezoidal
method.
</p>
<p><code>trapz&nbsp;(<var>y</var>)</code><!-- /@w --> computes the integral of <var>y</var> along the first
non-singleton dimension.  When the argument <var>x</var> is omitted an equally
spaced <var>x</var> vector with unit spacing (1) is assumed.
<code>trapz (<var>x</var>, <var>y</var>)</code> evaluates the integral with respect to the
spacing in <var>x</var> and the values in <var>y</var>.  This is useful if the points
in <var>y</var> have been sampled unevenly.
</p>
<p>If the optional <var>dim</var> argument is given, operate along this dimension.
</p>
<p>Application Note: If <var>x</var> is not specified then unit spacing will be
used.  To scale the integral to the correct value you must multiply by the
actual spacing value (deltaX).  As an example, the integral of <em class='math'>x^3</em>
over the range [0, 1] is <em class='math'>x^4/4</em> or 0.25.  The following code uses
<code>trapz</code> to calculate the integral in three different ways.
</p>
<div class="example">
<pre class="example">x = 0:0.1:1;
y = x.^3;
## No scaling
q = trapz (y)
  &rArr; q = 2.5250
## Approximation to integral by scaling
q * 0.1
  &rArr; 0.25250
## Same result by specifying <var>x</var>
trapz (x, y)
  &rArr; 0.25250
</pre></div>


<p><strong>See also:</strong> <a href="#XREFcumtrapz">cumtrapz</a>.
</p></dd></dl>


<span id="XREFcumtrapz"></span><dl class="def">
<dt id="index-cumtrapz"><span class="category">: </span><span><em><var>q</var> =</em> <strong>cumtrapz</strong> <em>(<var>y</var>)</em><a href='#index-cumtrapz' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-cumtrapz-1"><span class="category">: </span><span><em><var>q</var> =</em> <strong>cumtrapz</strong> <em>(<var>x</var>, <var>y</var>)</em><a href='#index-cumtrapz-1' class='copiable-anchor'> &para;</a></span></dt>
<dt id="index-cumtrapz-2"><span class="category">: </span><span><em><var>q</var> =</em> <strong>cumtrapz</strong> <em>(&hellip;, <var>dim</var>)</em><a href='#index-cumtrapz-2' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Cumulative numerical integration of points <var>y</var> using the trapezoidal
method.
</p>
<p><code>cumtrapz&nbsp;(<var>y</var>)</code><!-- /@w --> computes the cumulative integral of <var>y</var>
along the first non-singleton dimension.  Where <code>trapz</code> reports only
the overall integral sum, <code>cumtrapz</code> reports the current partial sum
value at each point of <var>y</var>.
</p>
<p>When the argument <var>x</var> is omitted an equally spaced <var>x</var> vector with
unit spacing (1) is assumed.  <code>cumtrapz (<var>x</var>, <var>y</var>)</code> evaluates
the integral with respect to the spacing in <var>x</var> and the values in
<var>y</var>.  This is useful if the points in <var>y</var> have been sampled
unevenly.
</p>
<p>If the optional <var>dim</var> argument is given, operate along this dimension.
</p>
<p>Application Note: If <var>x</var> is not specified then unit spacing will be
used.  To scale the integral to the correct value you must multiply by the
actual spacing value (deltaX).
</p>
<p><strong>See also:</strong> <a href="#XREFtrapz">trapz</a>, <a href="Sums-and-Products.html#XREFcumsum">cumsum</a>.
</p></dd></dl>


</div>
<hr>
<div class="header">
<p>
Next: <a href="Orthogonal-Collocation.html">Orthogonal Collocation</a>, Up: <a href="Numerical-Integration.html">Numerical Integration</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>