Mercurial > hg > octave-lyh
view doc/interpreter/quad.txi @ 17289:bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Macro handles options ("on") or properties ("position") more elegantly
than @code{"text"}.
* doc/interpreter/macros.texi: Add new @qcode macro.
* doc/interpreter/tips.txi: Add documentation about @qcode macro.
* doc/interpreter/basics.txi, doc/interpreter/container.txi,
doc/interpreter/emacs.txi, doc/interpreter/errors.txi,
doc/interpreter/eval.txi, doc/interpreter/expr.txi,
doc/interpreter/external.txi, doc/interpreter/func.txi,
doc/interpreter/grammar.txi, doc/interpreter/image.txi,
doc/interpreter/install.txi, doc/interpreter/interp.txi,
doc/interpreter/io.txi, doc/interpreter/matrix.txi,
doc/interpreter/numbers.txi, doc/interpreter/oop.txi,
doc/interpreter/package.txi, doc/interpreter/plot.txi,
doc/interpreter/quad.txi, doc/interpreter/sparse.txi,
doc/interpreter/strings.txi, doc/interpreter/system.txi,
doc/interpreter/vectorize.txi, libinterp/corefcn/balance.cc,
libinterp/corefcn/bitfcns.cc, libinterp/corefcn/cellfun.cc,
libinterp/corefcn/conv2.cc, libinterp/corefcn/data.cc,
libinterp/corefcn/debug.cc, libinterp/corefcn/defaults.cc,
libinterp/corefcn/dirfns.cc, libinterp/corefcn/dlmread.cc,
libinterp/corefcn/error.cc, libinterp/corefcn/file-io.cc,
libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/help.cc,
libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc,
libinterp/corefcn/load-path.cc, libinterp/corefcn/load-save.cc,
libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lu.cc,
libinterp/corefcn/luinc.cc, libinterp/corefcn/matrix_type.cc,
libinterp/corefcn/oct-hist.cc, libinterp/corefcn/pager.cc,
libinterp/corefcn/pr-output.cc, libinterp/corefcn/pt-jit.cc,
libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc,
libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc,
libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sparse.cc,
libinterp/corefcn/spparms.cc, libinterp/corefcn/str2double.cc,
libinterp/corefcn/svd.cc, libinterp/corefcn/symtab.cc,
libinterp/corefcn/syscalls.cc, libinterp/corefcn/toplev.cc,
libinterp/corefcn/tril.cc, libinterp/corefcn/typecast.cc,
libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc,
libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/chol.cc,
libinterp/dldfcn/colamd.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc,
libinterp/dldfcn/symbfact.cc, libinterp/octave-value/ov-base.cc,
libinterp/octave-value/ov-fcn-handle.cc,
libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-java.cc,
libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-struct.cc,
libinterp/octave-value/ov-usr-fcn.cc, libinterp/parse-tree/oct-parse.in.yy,
libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-eval.cc,
libinterp/parse-tree/pt-mat.cc, scripts/@ftp/ftp.m,
scripts/deprecated/java_convert_matrix.m, scripts/deprecated/java_debug.m,
scripts/deprecated/java_unsigned_conversion.m, scripts/deprecated/shell_cmd.m,
scripts/general/dblquad.m, scripts/general/display.m,
scripts/general/genvarname.m, scripts/general/idivide.m,
scripts/general/interp1.m, scripts/general/interp2.m,
scripts/general/interp3.m, scripts/general/interpn.m, scripts/general/isa.m,
scripts/general/profexplore.m, scripts/general/profile.m,
scripts/general/quadgk.m, scripts/general/randi.m, scripts/general/structfun.m,
scripts/general/subsindex.m, scripts/general/triplequad.m,
scripts/geometry/griddata.m, scripts/geometry/griddata3.m,
scripts/geometry/griddatan.m, scripts/geometry/voronoi.m, scripts/help/help.m,
scripts/help/lookfor.m, scripts/image/cmpermute.m, scripts/image/colormap.m,
scripts/image/image.m, scripts/image/imagesc.m, scripts/image/imfinfo.m,
scripts/image/imformats.m, scripts/image/imread.m, scripts/image/imshow.m,
scripts/image/imwrite.m, scripts/image/ind2gray.m, scripts/image/lines.m,
scripts/image/rgb2ind.m, scripts/image/spinmap.m, scripts/io/dlmwrite.m,
scripts/io/strread.m, scripts/io/textread.m, scripts/io/textscan.m,
scripts/java/javaclasspath.m, scripts/java/usejava.m,
scripts/miscellaneous/bzip2.m, scripts/miscellaneous/computer.m,
scripts/miscellaneous/copyfile.m, scripts/miscellaneous/debug.m,
scripts/miscellaneous/dos.m, scripts/miscellaneous/edit.m,
scripts/miscellaneous/gzip.m, scripts/miscellaneous/license.m,
scripts/miscellaneous/mkoctfile.m, scripts/miscellaneous/movefile.m,
scripts/miscellaneous/parseparams.m, scripts/miscellaneous/unix.m,
scripts/optimization/fminbnd.m, scripts/optimization/fminsearch.m,
scripts/optimization/fminunc.m, scripts/optimization/fsolve.m,
scripts/optimization/fzero.m, scripts/optimization/glpk.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/optimset.m,
scripts/optimization/pqpnonneg.m, scripts/pkg/pkg.m, scripts/plot/allchild.m,
scripts/plot/ancestor.m, scripts/plot/area.m, scripts/plot/axis.m,
scripts/plot/bar.m, scripts/plot/barh.m, scripts/plot/box.m,
scripts/plot/caxis.m, scripts/plot/cla.m, scripts/plot/clabel.m,
scripts/plot/clf.m, scripts/plot/close.m, scripts/plot/colorbar.m,
scripts/plot/daspect.m, scripts/plot/ezmesh.m, scripts/plot/ezmeshc.m,
scripts/plot/ezsurf.m, scripts/plot/ezsurfc.m, scripts/plot/findall.m,
scripts/plot/findobj.m, scripts/plot/gcbo.m, scripts/plot/gcf.m,
scripts/plot/gco.m, scripts/plot/grid.m, scripts/plot/guihandles.m,
scripts/plot/hdl2struct.m, scripts/plot/hidden.m, scripts/plot/hold.m,
scripts/plot/isonormals.m, scripts/plot/isosurface.m, scripts/plot/legend.m,
scripts/plot/mesh.m, scripts/plot/meshc.m, scripts/plot/meshz.m,
scripts/plot/newplot.m, scripts/plot/orient.m, scripts/plot/pareto.m,
scripts/plot/patch.m, scripts/plot/pbaspect.m, scripts/plot/pcolor.m,
scripts/plot/plot.m, scripts/plot/print.m,
scripts/plot/private/__add_default_menu__.m, scripts/plot/quiver.m,
scripts/plot/quiver3.m, scripts/plot/refreshdata.m, scripts/plot/saveas.m,
scripts/plot/scatter.m, scripts/plot/scatter3.m, scripts/plot/shading.m,
scripts/plot/shrinkfaces.m, scripts/plot/slice.m, scripts/plot/stem.m,
scripts/plot/stem3.m, scripts/plot/struct2hdl.m, scripts/plot/subplot.m,
scripts/plot/surf.m, scripts/plot/surfc.m, scripts/plot/surfl.m,
scripts/plot/tetramesh.m, scripts/plot/uigetfile.m, scripts/plot/uimenu.m,
scripts/plot/uiputfile.m, scripts/plot/waterfall.m, scripts/plot/whitebg.m,
scripts/plot/xlim.m, scripts/plot/ylim.m, scripts/plot/zlim.m,
scripts/polynomial/conv.m, scripts/polynomial/polyout.m,
scripts/polynomial/splinefit.m, scripts/set/ismember.m, scripts/set/powerset.m,
scripts/set/setdiff.m, scripts/set/union.m, scripts/set/unique.m,
scripts/signal/detrend.m, scripts/signal/filter2.m, scripts/signal/freqz.m,
scripts/signal/periodogram.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/sparse/eigs.m, scripts/sparse/svds.m,
scripts/specfun/legendre.m, scripts/special-matrix/gallery.m,
scripts/statistics/base/mean.m, scripts/statistics/base/moment.m,
scripts/statistics/tests/cor_test.m,
scripts/statistics/tests/kolmogorov_smirnov_test.m,
scripts/statistics/tests/kolmogorov_smirnov_test_2.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/prop_test_2.m, scripts/statistics/tests/sign_test.m,
scripts/statistics/tests/t_test.m, scripts/statistics/tests/t_test_2.m,
scripts/statistics/tests/t_test_regression.m,
scripts/statistics/tests/u_test.m, scripts/statistics/tests/var_test.m,
scripts/statistics/tests/welch_test.m,
scripts/statistics/tests/wilcoxon_test.m, scripts/statistics/tests/z_test.m,
scripts/statistics/tests/z_test_2.m, scripts/strings/base2dec.m,
scripts/strings/index.m, scripts/strings/isstrprop.m,
scripts/strings/mat2str.m, scripts/strings/regexptranslate.m,
scripts/strings/rindex.m, scripts/strings/str2num.m, scripts/strings/strcat.m,
scripts/strings/strjust.m, scripts/strings/strmatch.m,
scripts/strings/validatestring.m, scripts/testfun/demo.m,
scripts/testfun/example.m, scripts/testfun/test.m, scripts/time/addtodate.m,
scripts/time/asctime.m, scripts/time/datestr.m, scripts/time/datetick.m,
scripts/time/weekday.m, scripts/ui/errordlg.m, scripts/ui/helpdlg.m,
scripts/ui/inputdlg.m, scripts/ui/listdlg.m, scripts/ui/msgbox.m,
scripts/ui/questdlg.m, scripts/ui/warndlg.m: Use new @qcode macro.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 19 Aug 2013 20:46:38 -0700 |
parents | f2a8592b8fbd |
children |
line wrap: on
line source
@c Copyright (C) 1996-2012 John W. Eaton @c @c This file is part of Octave. @c @c Octave is free software; you can redistribute it and/or modify it @c under the terms of the GNU General Public License as published by the @c Free Software Foundation; either version 3 of the License, or (at @c your option) any later version. @c @c Octave is distributed in the hope that it will be useful, but WITHOUT @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License @c for more details. @c @c You should have received a copy of the GNU General Public License @c along with Octave; see the file COPYING. If not, see @c <http://www.gnu.org/licenses/>. @node Numerical Integration @chapter Numerical Integration Octave comes with several built-in functions for computing the integral of a function numerically (termed quadrature). These functions all solve 1-dimensional integration problems. @menu * Functions of One Variable:: * Orthogonal Collocation:: * Functions of Multiple Variables:: @end menu @node Functions of One Variable @section Functions of One Variable Octave supports five different algorithms for computing the integral @tex $$ \int_a^b f(x) d x $$ @end tex of a function @math{f} over the interval from @math{a} to @math{b}. These are @table @code @item quad Numerical integration based on Gaussian quadrature. @item quadv Numerical integration using an adaptive vectorized Simpson's rule. @item quadl Numerical integration using an adaptive Lobatto rule. @item quadgk Numerical integration using an adaptive Gauss-Konrod rule. @item quadcc Numerical integration using adaptive Clenshaw-Curtis rules. @item trapz, cumtrapz Numerical integration of data using the trapezoidal method. @end table @noindent The best quadrature algorithm to use depends on the integrand. If you have empirical data, rather than a function, the choice is @code{trapz} or @code{cumtrapz}. If you are uncertain about the characteristics of the integrand, @code{quadcc} will be the most robust as it can handle discontinuities, singularities, oscillatory functions, and infinite intervals. When the integrand is smooth @code{quadgk} may be the fastest of the algorithms. @multitable @columnfractions 0.05 0.15 .80 @headitem @tab Function @tab Characteristics @item @tab quad @tab Low accuracy with nonsmooth integrands @item @tab quadv @tab Medium accuracy with smooth integrands @item @tab quadl @tab Medium accuracy with smooth integrands. Slower than quadgk. @item @tab quadgk @tab Medium accuracy (@math{1e^{-6}}--@math{1e^{-9}}) with smooth integrands. @item @tab @tab Handles oscillatory functions and infinite bounds @item @tab quadcc @tab Low to High accuracy with nonsmooth/smooth integrands @item @tab @tab Handles oscillatory functions, singularities, and infinite bounds @end multitable Here is an example of using @code{quad} to integrate the function @tex $$ f(x) = x \sin (1/x) \sqrt {|1 - x|} $$ from $x = 0$ to $x = 3$. @end tex @ifnottex @example @var{f}(@var{x}) = @var{x} * sin (1/@var{x}) * sqrt (abs (1 - @var{x})) @end example @noindent from @var{x} = 0 to @var{x} = 3. @end ifnottex This is a fairly difficult integration (plot the function over the range of integration to see why). The first step is to define the function: @example @group function y = f (x) y = x .* sin (1./x) .* sqrt (abs (1 - x)); endfunction @end group @end example Note the use of the `dot' forms of the operators. This is not necessary for the @code{quad} 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. The second step is to call quad with the limits of integration: @example @group [q, ier, nfun, err] = quad ("f", 0, 3) @result{} 1.9819 @result{} 1 @result{} 5061 @result{} 1.1522e-07 @end group @end example Although @code{quad} returns a nonzero value for @var{ier}, 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.). The function @qcode{"f"} can be the string name of a function, a function handle, or an inline function. These options make it quite easy to do integration without having to fully define a function in an m-file. For example: @example @group # Verify integral (x^3) = x^4/4 f = inline ("x.^3"); quadgk (f, 0, 1) @result{} 0.25000 # Verify gamma function = (n-1)! for n = 4 f = @@(x) x.^3 .* exp (-x); quadcc (f, 0, Inf) @result{} 6.0000 @end group @end example @DOCSTRING(quad) @DOCSTRING(quad_options) @DOCSTRING(quadv) @DOCSTRING(quadl) @DOCSTRING(quadgk) @DOCSTRING(quadcc) 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} function can integrate these values as shown in the following example where "data" has been collected on the cosine function over the range [0, pi/2). @example @group x = 0:0.1:pi/2; # Uniformly spaced points y = cos (x); trapz (x, y) @result{} 0.99666 @end group @end example 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. @example @group x = 0:0.1:pi/2; # Uniformly spaced points y = sin (x); trapz (x, y) @result{} 0.92849 @end group @end example 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. @example @group x = linspace (0, pi/2, 16); # Uniformly spaced, but including endpoint y = sin (x); trapz (x, y) @result{} 0.99909 @end group @end example 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}. @DOCSTRING(trapz) @DOCSTRING(cumtrapz) @node Orthogonal Collocation @section Orthogonal Collocation @DOCSTRING(colloc) Here is an example of using @code{colloc} to generate weight matrices for solving the second order differential equation @tex $u^\prime - \alpha u^{\prime\prime} = 0$ with the boundary conditions $u(0) = 0$ and $u(1) = 1$. @end tex @ifnottex @var{u}' - @var{alpha} * @var{u}'' = 0 with the boundary conditions @var{u}(0) = 0 and @var{u}(1) = 1. @end ifnottex First, we can generate the weight matrices for @var{n} points (including the endpoints of the interval), and incorporate the boundary conditions in the right hand side (for a specific value of @tex $\alpha$). @end tex @ifnottex @var{alpha}). @end ifnottex @example @group n = 7; alpha = 0.1; [r, a, b] = colloc (n-2, "left", "right"); at = a(2:n-1,2:n-1); bt = b(2:n-1,2:n-1); rhs = alpha * b(2:n-1,n) - a(2:n-1,n); @end group @end example Then the solution at the roots @var{r} is @example @group u = [ 0; (at - alpha * bt) \ rhs; 1] @result{} [ 0.00; 0.004; 0.01 0.00; 0.12; 0.62; 1.00 ] @end group @end example @node Functions of Multiple Variables @section Functions of Multiple Variables Octave does not have built-in functions for computing the integral of functions of multiple variables directly. It is possible, however, to compute the integral of a function of multiple variables using the existing functions for one-dimensional integrals. To illustrate how the integration can be performed, we will integrate the function @tex $$ f(x, y) = \sin(\pi x y)\sqrt{x y} $$ @end tex @ifnottex @example f(x, y) = sin(pi*x*y)*sqrt(x*y) @end example @end ifnottex for @math{x} and @math{y} between 0 and 1. The first approach creates a function that integrates @math{f} with respect to @math{x}, and then integrates that function with respect to @math{y}. Because @code{quad} is written in Fortran it cannot be called recursively. This means that @code{quad} cannot integrate a function that calls @code{quad}, and hence cannot be used to perform the double integration. Any of the other integrators, however, can be used which is what the following code demonstrates. @example @group function q = g(y) q = ones (size (y)); for i = 1:length (y) f = @@(x) sin (pi*x.*y(i)) .* sqrt (x.*y(i)); q(i) = quadgk (f, 0, 1); endfor endfunction I = quadgk ("g", 0, 1) @result{} 0.30022 @end group @end example The above process can be simplified with the @code{dblquad} and @code{triplequad} functions for integrals over two and three variables. For example: @example @group I = dblquad (@@(x, y) sin (pi*x.*y) .* sqrt (x.*y), 0, 1, 0, 1) @result{} 0.30022 @end group @end example @DOCSTRING(dblquad) @DOCSTRING(triplequad) The above mentioned approach works, but is fairly slow, and that problem increases exponentially with the dimensionality of the integral. Another possible solution is to use Orthogonal Collocation as described in the previous section (@pxref{Orthogonal Collocation}). The integral of a function @math{f(x,y)} for @math{x} and @math{y} between 0 and 1 can be approximated using @math{n} points by @tex $$ \int_0^1 \int_0^1 f(x,y) d x d y \approx \sum_{i=1}^n \sum_{j=1}^n q_i q_j f(r_i, r_j), $$ @end tex @ifnottex the sum over @code{i=1:n} and @code{j=1:n} of @code{q(i)*q(j)*f(r(i),r(j))}, @end ifnottex where @math{q} and @math{r} is as returned by @code{colloc (n)}. The generalization to more than two variables is straight forward. The following code computes the studied integral using @math{n=8} points. @example @group f = @@(x,y) sin (pi*x*y') .* sqrt (x*y'); n = 8; [t, ~, ~, q] = colloc (n); I = q'*f(t,t)*q; @result{} 0.30022 @end group @end example @noindent It should be noted that the number of points determines the quality of the approximation. If the integration needs to be performed between @math{a} and @math{b}, instead of 0 and 1, then a change of variables is needed.